@spidy092/auth-client 1.0.1 → 1.0.3

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.
Files changed (2) hide show
  1. package/core.js +23 -13
  2. package/package.json +1 -1
package/core.js CHANGED
@@ -3,10 +3,10 @@ import { setToken, clearToken, getToken } from './token';
3
3
  import { getConfig } from './config';
4
4
 
5
5
  export function login(clientKeyArg, redirectUriArg, stateArg) {
6
- const {
6
+ const {
7
7
  clientKey: defaultClientKey,
8
8
  authBaseUrl,
9
- redirectUri: defaultRedirectUri,
9
+ redirectUri: defaultRedirectUri,
10
10
  accountUiUrl
11
11
  } = getConfig();
12
12
 
@@ -18,22 +18,32 @@ export function login(clientKeyArg, redirectUriArg, stateArg) {
18
18
  throw new Error('Missing clientKey or redirectUri');
19
19
  }
20
20
 
21
- // Store original app info for return after auth
22
- sessionStorage.setItem('authState', state);
23
- sessionStorage.setItem('originalApp', clientKey);
24
- sessionStorage.setItem('returnUrl', redirectUri);
21
+ // Store state for callback validation
22
+ localStorage.setItem('authState', state);
23
+ localStorage.setItem('originalApp', clientKey);
24
+ localStorage.setItem('returnUrl', redirectUri);
25
+
26
+ // --- ENTERPRISE LOGIC ---
27
+ // If we are already in Account-UI, go straight to the backend
28
+ if (window.location.origin === accountUiUrl && clientKey === 'account-ui') {
29
+ // Direct SSO kick-off for Account-UI
30
+ const backendLoginUrl = `${authBaseUrl}/login/${clientKey}?redirect_uri=${encodeURIComponent(redirectUri)}&state=${encodeURIComponent(state)}`;
31
+ console.log('Redirecting directly to auth backend:', backendLoginUrl);
32
+ window.location.href = backendLoginUrl;
33
+ return;
34
+ }
25
35
 
26
- // Redirect to centralized Account UI instead of direct auth service
36
+ // Otherwise, centralized login flow (for other apps)
27
37
  const accountLoginUrl = `${accountUiUrl}/login?` + new URLSearchParams({
28
38
  client: clientKey,
29
39
  redirect_uri: redirectUri,
30
40
  state: state
31
41
  });
32
-
33
- console.log('Redirecting to Account UI:', accountLoginUrl);
42
+ console.log('Redirecting to centralized Account UI:', accountLoginUrl);
34
43
  window.location.href = accountLoginUrl;
35
44
  }
36
45
 
46
+
37
47
  export function logout() {
38
48
  const { clientKey, authBaseUrl, accountUiUrl } = getConfig();
39
49
  const token = getToken();
@@ -63,16 +73,16 @@ export function handleCallback() {
63
73
  const accessToken = params.get('access_token');
64
74
  const error = params.get('error');
65
75
  const state = params.get('state');
66
- const storedState = sessionStorage.getItem('authState');
76
+ const storedState = localStorage.getItem('authState');
67
77
 
68
78
  // Validate state
69
79
  if (state && storedState && state !== storedState) {
70
80
  throw new Error('Invalid state. Possible CSRF attack.');
71
81
  }
72
82
 
73
- sessionStorage.removeItem('authState');
74
- sessionStorage.removeItem('originalApp');
75
- sessionStorage.removeItem('returnUrl');
83
+ localStorage.removeItem('authState');
84
+ localStorage.removeItem('originalApp');
85
+ localStorage.removeItem('returnUrl');
76
86
 
77
87
  if (error) {
78
88
  throw new Error(`Authentication failed: ${error}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spidy092/auth-client",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Scalable frontend auth SDK for centralized login using Keycloak + Auth Service.",
5
5
  "main": "index.js",
6
6
  "module": "index.js",