@spidy092/auth-client 1.0.6 → 1.0.8

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 +53 -45
  2. package/package.json +1 -1
package/core.js CHANGED
@@ -1,8 +1,7 @@
1
-
2
1
  import { setToken, clearToken, getToken } from './token';
3
2
  import { getConfig } from './config';
4
3
 
5
- export function login(clientKeyArg, redirectUriArg, stateArg) {
4
+ export function login(clientKeyArg, redirectUriArg) { // Removed stateArg
6
5
  const {
7
6
  clientKey: defaultClientKey,
8
7
  authBaseUrl,
@@ -12,94 +11,103 @@ export function login(clientKeyArg, redirectUriArg, stateArg) {
12
11
 
13
12
  const clientKey = clientKeyArg || defaultClientKey;
14
13
  const redirectUri = redirectUriArg || defaultRedirectUri;
15
- const state = stateArg || crypto.randomUUID();
14
+ // Removed state generation
16
15
 
17
16
  console.log('Initiating login with parameters:', {
18
17
  clientKey,
19
- redirectUri,
20
- state,});
18
+ redirectUri
19
+ // Removed state from logging
20
+ });
21
21
 
22
-
23
22
  if (!clientKey || !redirectUri) {
24
23
  throw new Error('Missing clientKey or redirectUri');
25
24
  }
26
25
 
27
- // Store state for callback validation
28
- sessionStorage.setItem('authState', state);
26
+ // Store only app info, no state
29
27
  sessionStorage.setItem('originalApp', clientKey);
30
28
  sessionStorage.setItem('returnUrl', redirectUri);
31
29
 
32
30
  // --- ENTERPRISE LOGIC ---
33
31
  // If we are already in Account-UI, go straight to the backend
34
32
  if (window.location.origin === accountUiUrl && clientKey === 'account-ui') {
35
- // Direct SSO kick-off for Account-UI
36
- const backendLoginUrl = `${authBaseUrl}/login/${clientKey}?redirect_uri=${encodeURIComponent(redirectUri)}&state=${encodeURIComponent(state)}`;
33
+ // Direct SSO kick-off for Account-UI (no state parameter)
34
+ const backendLoginUrl = `${authBaseUrl}/login/${clientKey}?redirect_uri=${encodeURIComponent(redirectUri)}`;
37
35
  console.log('Redirecting directly to auth backend:', backendLoginUrl);
38
36
  window.location.href = backendLoginUrl;
39
37
  return;
40
38
  }
41
39
 
42
- // Otherwise, centralized login flow (for other apps)
40
+ // Otherwise, centralized login flow (for other apps, no state)
43
41
  const accountLoginUrl = `${accountUiUrl}/login?` + new URLSearchParams({
44
42
  client: clientKey,
45
- redirect_uri: redirectUri,
46
- state: state
43
+ redirect_uri: redirectUri
44
+ // Removed state
47
45
  });
48
46
  console.log('Redirecting to centralized Account UI:', accountLoginUrl);
49
47
  window.location.href = accountLoginUrl;
50
48
  }
51
49
 
52
-
53
50
  export function logout() {
54
51
  const { clientKey, authBaseUrl, accountUiUrl } = getConfig();
55
52
  const token = getToken();
56
53
 
57
- if (!token) {
58
- window.location.href = `${accountUiUrl}/login`;
59
- return;
60
- }
54
+ console.log('Initiating logout for client:', clientKey);
61
55
 
56
+ // Clear local storage immediately
62
57
  clearToken();
63
-
64
- // Call logout endpoint
65
- fetch(`${authBaseUrl}/logout/${clientKey}`, {
66
- method: 'POST',
67
- credentials: 'include',
68
- headers: {
69
- 'Authorization': `Bearer ${token}`
70
- }
71
- }).catch(console.error);
58
+ sessionStorage.clear();
59
+ // Don't clear localStorage completely - might break other stuff
60
+ // localStorage.clear(); // Remove this line
72
61
 
73
- // Redirect to Account UI logout page
74
- window.location.href = `${accountUiUrl}/logout?client=${clientKey}`;
62
+ // Call backend logout if we have a token
63
+ if (token) {
64
+ fetch(`${authBaseUrl}/logout/${clientKey}`, {
65
+ method: 'POST',
66
+ credentials: 'include', // ✅ CRITICAL: This sends cookies
67
+ headers: {
68
+ 'Authorization': `Bearer ${token}`,
69
+ 'Content-Type': 'application/json'
70
+ }
71
+ })
72
+ .then(response => response.json())
73
+ .then(data => {
74
+ console.log('Backend logout response:', data);
75
+
76
+ // If we get a Keycloak logout URL, redirect there
77
+ if (data.keycloakLogoutUrl) {
78
+ window.location.href = data.keycloakLogoutUrl;
79
+ return;
80
+ }
81
+
82
+ // Otherwise redirect to login
83
+ window.location.href = `${accountUiUrl}/login`;
84
+ })
85
+ .catch(error => {
86
+ console.error('Logout error:', error);
87
+ // Always redirect to login even on error
88
+ window.location.href = `${accountUiUrl}/login`;
89
+ });
90
+ } else {
91
+ // No token, just redirect to login
92
+ window.location.href = `${accountUiUrl}/login`;
93
+ }
75
94
  }
76
95
 
96
+
77
97
  export function handleCallback() {
78
98
  const params = new URLSearchParams(window.location.search);
79
99
  const accessToken = params.get('access_token');
80
100
  const error = params.get('error');
81
- const state = params.get('state');
82
- const storedState = sessionStorage.getItem('authState');
101
+ // Removed state handling completely
83
102
 
84
103
  console.log('Handling authentication callback:', {
85
104
  accessToken,
86
- error,
87
- state,
88
- storedState
105
+ error
106
+ // Removed state from logging
89
107
  });
90
108
 
109
+ // Removed all state validation
91
110
 
92
- // Validate state
93
- // if (state && storedState && state !== storedState) {
94
- // throw new Error('Invalid state. Possible CSRF attack.');
95
- // }
96
-
97
-
98
- if (!state && !storedState ) {
99
- throw new Error('no state. Possible CSRF attack.');
100
- }
101
-
102
- sessionStorage.removeItem('authState');
103
111
  sessionStorage.removeItem('originalApp');
104
112
  sessionStorage.removeItem('returnUrl');
105
113
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spidy092/auth-client",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "Scalable frontend auth SDK for centralized login using Keycloak + Auth Service.",
5
5
  "main": "index.js",
6
6
  "module": "index.js",