supaapps-auth 2.0.0-rc.4 → 2.0.0-rc.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AuthManager.js +1 -1
- package/package.json +1 -1
- package/src/AuthManager.ts +1 -1
- package/tests/AuthManager.test.ts +26 -1
package/dist/AuthManager.js
CHANGED
|
@@ -90,7 +90,7 @@ class AuthManager {
|
|
|
90
90
|
checkAccessToken() {
|
|
91
91
|
return __awaiter(this, arguments, void 0, function* (isInitilization = false) {
|
|
92
92
|
const accessToken = localStorage.getItem('access_token');
|
|
93
|
-
if (accessToken
|
|
93
|
+
if (accessToken && this.isTokenExpired(accessToken)) {
|
|
94
94
|
return this.refreshAccessToken(isInitilization);
|
|
95
95
|
}
|
|
96
96
|
return accessToken;
|
package/package.json
CHANGED
package/src/AuthManager.ts
CHANGED
|
@@ -124,7 +124,7 @@ export class AuthManager {
|
|
|
124
124
|
|
|
125
125
|
public async checkAccessToken(isInitilization: boolean = false): Promise<string> {
|
|
126
126
|
const accessToken = localStorage.getItem('access_token');
|
|
127
|
-
if (accessToken
|
|
127
|
+
if (accessToken && this.isTokenExpired(accessToken)) {
|
|
128
128
|
return this.refreshAccessToken(isInitilization);
|
|
129
129
|
}
|
|
130
130
|
return accessToken;
|
|
@@ -12,7 +12,12 @@ const tokenThatWontExpire2 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxM
|
|
|
12
12
|
const tokenThatExpired = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZmlyc3RfbmFtZSI6IkpvaG4gRG9lIiwibGFzdF9uYW1lIjoiRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJzY29wZXMiOiIvcm9vdC8qIiwiZXhwIjo1MDAsImlkIjoyLCJpc3MiOjEyMywiYXVkIjoidGVzdGluZyJ9.ungpbhHfCM5ZP5oiZ1RnMkJ-NKJI8s3_IPJptjyKHR4';
|
|
13
13
|
|
|
14
14
|
|
|
15
|
+
|
|
16
|
+
|
|
15
17
|
describe('AuthManager Tests', () => {
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
jest.spyOn(localStorage, 'getItem');
|
|
20
|
+
});
|
|
16
21
|
|
|
17
22
|
beforeEach(() => {
|
|
18
23
|
localStorage.clear(); // Clear localStorage before each test
|
|
@@ -72,6 +77,26 @@ describe('AuthManager Tests', () => {
|
|
|
72
77
|
});
|
|
73
78
|
|
|
74
79
|
|
|
80
|
+
describe('AuthManager Tests isolated ', () => {
|
|
81
|
+
it('doesn\'t refresh access token when its not expired', async () => {
|
|
82
|
+
const stateChange = jest.fn();
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
// check that we set localstorage correct
|
|
86
|
+
localStorage.setItem('access_token', tokenThatWontExpire1);
|
|
87
|
+
localStorage.setItem('refresh_token', 'mockRefreshToken');
|
|
88
|
+
|
|
89
|
+
const manager = AuthManager.initialize('http://auth-server.com/', 'example-realm', 'http://myapp.com/callback', stateChange);
|
|
90
|
+
|
|
91
|
+
const currentCallCount = (localStorage.getItem as jest.Mock).mock.calls.length;
|
|
92
|
+
|
|
93
|
+
const token = await manager.getAccessToken();
|
|
94
|
+
|
|
95
|
+
expect(localStorage.getItem).toHaveBeenCalledTimes(currentCallCount + 1);
|
|
96
|
+
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
75
100
|
it('throws an error when no refresh token is found', async () => {
|
|
76
101
|
localStorage.removeItem('refresh_token');
|
|
77
102
|
|
|
@@ -125,4 +150,4 @@ describe('AuthManager Tests', () => {
|
|
|
125
150
|
|
|
126
151
|
|
|
127
152
|
|
|
128
|
-
});
|
|
153
|
+
});
|