richie-education 3.3.1-dev3 → 3.3.1-dev5
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/js/api/auth/keycloak.spec.ts +16 -0
- package/js/api/auth/keycloak.ts +10 -1
- package/package.json +1 -1
|
@@ -84,6 +84,22 @@ describe('Keycloak API', () => {
|
|
|
84
84
|
});
|
|
85
85
|
|
|
86
86
|
describe('user.me', () => {
|
|
87
|
+
it('returns null when init returns false (not authenticated)', async () => {
|
|
88
|
+
mockKeycloakInit.mockResolvedValueOnce(false);
|
|
89
|
+
const api = API(authConfig);
|
|
90
|
+
const response = await api.user.me();
|
|
91
|
+
expect(response).toBeNull();
|
|
92
|
+
expect(mockKeycloakUpdateToken).not.toHaveBeenCalled();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('returns null when init rejects', async () => {
|
|
96
|
+
mockKeycloakInit.mockRejectedValueOnce(new Error('Init failed'));
|
|
97
|
+
const api = API(authConfig);
|
|
98
|
+
const response = await api.user.me();
|
|
99
|
+
expect(response).toBeNull();
|
|
100
|
+
expect(mockKeycloakUpdateToken).not.toHaveBeenCalled();
|
|
101
|
+
});
|
|
102
|
+
|
|
87
103
|
it('returns null when updateToken fails', async () => {
|
|
88
104
|
mockKeycloakUpdateToken.mockRejectedValueOnce(new Error('Token refresh failed'));
|
|
89
105
|
const response = await keycloakApi.user.me();
|
package/js/api/auth/keycloak.ts
CHANGED
|
@@ -12,7 +12,7 @@ const API = (APIConf: AuthenticationBackend): { user: APIAuthentication } => {
|
|
|
12
12
|
realm: APIConf.realm!,
|
|
13
13
|
clientId: APIConf.client_id!,
|
|
14
14
|
});
|
|
15
|
-
keycloak.init({
|
|
15
|
+
const initPromise = keycloak.init({
|
|
16
16
|
checkLoginIframe: false,
|
|
17
17
|
flow: 'standard',
|
|
18
18
|
onLoad: 'check-sso',
|
|
@@ -39,6 +39,15 @@ const API = (APIConf: AuthenticationBackend): { user: APIAuthentication } => {
|
|
|
39
39
|
user: {
|
|
40
40
|
accessToken: () => sessionStorage.getItem(RICHIE_USER_TOKEN),
|
|
41
41
|
me: async () => {
|
|
42
|
+
let isAuthenticated: boolean;
|
|
43
|
+
try {
|
|
44
|
+
isAuthenticated = await initPromise;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
handle(error);
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (!isAuthenticated) return null;
|
|
50
|
+
|
|
42
51
|
try {
|
|
43
52
|
await keycloak.updateToken(30);
|
|
44
53
|
} catch (error) {
|