|
1 | | -import { UserManager, Log, User } from "oidc-client"; |
| 1 | +import { UserManager, Log, User } from 'oidc-client'; |
2 | 2 |
|
3 | 3 | const config = { |
4 | | - authority: 'https://localhost:9443/realms/calm-hub-realm', |
5 | | - client_id: 'calm-hub-authz-code', |
6 | | - redirect_uri: window.location.origin, |
7 | | - response_type: 'code', |
8 | | - scope: 'openid profile architectures:read adrs:all', |
9 | | - post_logout_redirect_uri: window.location.origin, |
10 | | - automaticSilentRenew: true, |
11 | | - filterProtocolClaims: true, |
12 | | - loadUserInfo: true, |
| 4 | + authority: 'https://localhost:9443/realms/calm-hub-realm', |
| 5 | + client_id: 'calm-hub-authz-code', |
| 6 | + redirect_uri: window.location.origin, |
| 7 | + response_type: 'code', |
| 8 | + scope: 'openid profile architectures:read adrs:all', |
| 9 | + post_logout_redirect_uri: window.location.origin, |
| 10 | + automaticSilentRenew: true, |
| 11 | + filterProtocolClaims: true, |
| 12 | + loadUserInfo: true, |
13 | 13 | }; |
14 | 14 |
|
15 | | -let userManager = null; |
| 15 | +let userManager: UserManager | null = null; |
16 | 16 | const isHttps = window.location.protocol === 'https:'; |
17 | 17 | if (isHttps) { |
18 | | - userManager = new UserManager(config); |
19 | | - Log.logger = console; |
20 | | - Log.level = Log.INFO; |
| 18 | + userManager = new UserManager(config); |
| 19 | + Log.logger = console; |
| 20 | + Log.level = Log.INFO; |
21 | 21 | } |
22 | 22 |
|
23 | | -export const getUser = async (): Promise<User | null> => { |
24 | | - return await userManager.getUser(); |
25 | | -}; |
| 23 | +export async function getUser(): Promise<User | null> { |
| 24 | + return (await userManager?.getUser()) || null; |
| 25 | +} |
26 | 26 |
|
27 | | -export const login = async (): Promise<void> => { |
28 | | - await userManager.signinRedirect(); |
29 | | -}; |
| 27 | +export async function login(): Promise<void> { |
| 28 | + await userManager?.signinRedirect(); |
| 29 | +} |
30 | 30 |
|
31 | | -export const processRedirect = async (): Promise<User | null> => { |
32 | | - try { |
33 | | - await userManager.signinRedirectCallback(); |
34 | | - return await getUser(); |
35 | | - } catch (error) { |
36 | | - console.error("Redirect Processing Error:", error); |
37 | | - return null; |
38 | | - } |
39 | | -}; |
| 31 | +export async function processRedirect(): Promise<User | null> { |
| 32 | + try { |
| 33 | + await userManager?.signinRedirectCallback(); |
| 34 | + return await getUser(); |
| 35 | + } catch (error) { |
| 36 | + console.error('Redirect Processing Error:', error); |
| 37 | + return null; |
| 38 | + } |
| 39 | +} |
40 | 40 |
|
41 | | -export const logout = async (): Promise<void> => { |
42 | | - try { |
43 | | - await userManager.signoutRedirect(); |
44 | | - } catch (error) { |
45 | | - console.error("Logout Error:", error); |
46 | | - } |
47 | | -}; |
| 41 | +export async function logout(): Promise<void> { |
| 42 | + try { |
| 43 | + await userManager?.signoutRedirect(); |
| 44 | + } catch (error) { |
| 45 | + console.error('Logout Error:', error); |
| 46 | + } |
| 47 | +} |
48 | 48 |
|
49 | | -export const clearSession = async (): Promise<void> => { |
50 | | - try { |
51 | | - await userManager.removeUser(); |
52 | | - console.log("Session cleared successfully."); |
53 | | - } catch (error) { |
54 | | - console.error("Error clearing session:", error); |
55 | | - } |
56 | | -}; |
| 49 | +export async function clearSession(): Promise<void> { |
| 50 | + try { |
| 51 | + await userManager?.removeUser(); |
| 52 | + console.log('Session cleared successfully.'); |
| 53 | + } catch (error) { |
| 54 | + console.error('Error clearing session:', error); |
| 55 | + } |
| 56 | +} |
57 | 57 |
|
58 | | -export const getToken = async (): Promise<string> => { |
| 58 | +export async function getToken(): Promise<string> { |
59 | 59 | if (!isHttps) { |
60 | | - return ""; |
| 60 | + return ''; |
61 | 61 | } |
62 | | - const user = await userManager.getUser(); |
| 62 | + const user = await userManager?.getUser(); |
63 | 63 | if (user && !user.expired) { |
64 | 64 | return user.access_token; |
65 | 65 | } |
66 | 66 |
|
67 | 67 | if (user && user.expired) { |
68 | | - try { |
69 | | - const refreshedUser = await userManager.signinSilent(); |
70 | | - return refreshedUser.access_token; |
71 | | - } catch (error) { |
72 | | - console.error('Error refreshing token:', error); |
73 | | - return ""; |
74 | | - } |
| 68 | + try { |
| 69 | + const refreshedUser = await userManager?.signinSilent(); |
| 70 | + return refreshedUser?.access_token || ''; |
| 71 | + } catch (error) { |
| 72 | + console.error('Error refreshing token:', error); |
| 73 | + return ''; |
| 74 | + } |
75 | 75 | } |
76 | | - return ""; |
77 | | -}; |
| 76 | + return ''; |
| 77 | +} |
78 | 78 |
|
79 | | -export const checkAuthorityService = async (): Promise<boolean> => { |
80 | | - try { |
81 | | - const response = await fetch(config.authority, { method: 'HEAD' }); |
82 | | - return response.ok; |
83 | | - } catch (error) { |
84 | | - console.error("Authority Service Check Error:", error); |
85 | | - return false; |
86 | | - } |
87 | | -}; |
| 79 | +export async function checkAuthorityService(): Promise<boolean> { |
| 80 | + try { |
| 81 | + const response = await fetch(config.authority, { method: 'HEAD' }); |
| 82 | + return response.ok; |
| 83 | + } catch (error) { |
| 84 | + console.error('Authority Service Check Error:', error); |
| 85 | + return false; |
| 86 | + } |
| 87 | +} |
88 | 88 |
|
89 | 89 | export const authService = { |
90 | | - getUser, |
91 | | - login, |
92 | | - processRedirect, |
93 | | - logout, |
94 | | - clearSession, |
95 | | - getToken, |
| 90 | + getUser, |
| 91 | + login, |
| 92 | + processRedirect, |
| 93 | + logout, |
| 94 | + clearSession, |
| 95 | + getToken, |
96 | 96 | }; |
0 commit comments