@spidy092/auth-client 1.0.9 → 1.0.11
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/core.js +59 -58
- package/index.js +3 -2
- package/package.json +1 -1
package/core.js
CHANGED
|
@@ -2,7 +2,60 @@
|
|
|
2
2
|
import { setToken, clearToken, getToken } from './token';
|
|
3
3
|
import { getConfig, isRouterMode } from './config';
|
|
4
4
|
|
|
5
|
+
// ✅ Track if callback was already processed
|
|
6
|
+
let callbackProcessed = false;
|
|
7
|
+
|
|
8
|
+
export function handleCallback() {
|
|
9
|
+
const params = new URLSearchParams(window.location.search);
|
|
10
|
+
const accessToken = params.get('access_token');
|
|
11
|
+
const error = params.get('error');
|
|
12
|
+
|
|
13
|
+
console.log('🔄 Handling authentication callback:', {
|
|
14
|
+
mode: isRouterMode() ? 'ROUTER' : 'CLIENT',
|
|
15
|
+
hasAccessToken: !!accessToken,
|
|
16
|
+
error,
|
|
17
|
+
alreadyProcessed: callbackProcessed // ✅ Log if already processed
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// ✅ If already processed and we have a token, return it
|
|
21
|
+
if (callbackProcessed) {
|
|
22
|
+
const existingToken = getToken();
|
|
23
|
+
if (existingToken) {
|
|
24
|
+
console.log('🔄 Callback already processed, returning existing token');
|
|
25
|
+
return existingToken;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ✅ Mark as processed first
|
|
30
|
+
callbackProcessed = true;
|
|
31
|
+
|
|
32
|
+
// Clean up session storage (only once)
|
|
33
|
+
sessionStorage.removeItem('originalApp');
|
|
34
|
+
sessionStorage.removeItem('returnUrl');
|
|
35
|
+
|
|
36
|
+
if (error) {
|
|
37
|
+
throw new Error(`Authentication failed: ${error}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (accessToken) {
|
|
41
|
+
setToken(accessToken);
|
|
42
|
+
console.log('✅ Token set successfully');
|
|
43
|
+
return accessToken;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
throw new Error('No access token found in callback URL');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ✅ Reset callback state when needed
|
|
50
|
+
export function resetCallbackState() {
|
|
51
|
+
callbackProcessed = false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Your other functions remain the same...
|
|
5
55
|
export function login(clientKeyArg, redirectUriArg) {
|
|
56
|
+
// ✅ Reset callback state when starting new login
|
|
57
|
+
resetCallbackState();
|
|
58
|
+
|
|
6
59
|
const {
|
|
7
60
|
clientKey: defaultClientKey,
|
|
8
61
|
authBaseUrl,
|
|
@@ -27,12 +80,10 @@ export function login(clientKeyArg, redirectUriArg) {
|
|
|
27
80
|
sessionStorage.setItem('originalApp', clientKey);
|
|
28
81
|
sessionStorage.setItem('returnUrl', redirectUri);
|
|
29
82
|
|
|
30
|
-
//
|
|
83
|
+
// Smart Router Logic (from my previous response)
|
|
31
84
|
if (isRouterMode()) {
|
|
32
|
-
// Router mode: Direct backend authentication
|
|
33
85
|
return routerLogin(clientKey, redirectUri);
|
|
34
86
|
} else {
|
|
35
|
-
// Client mode: Redirect to centralized login
|
|
36
87
|
return clientLogin(clientKey, redirectUri);
|
|
37
88
|
}
|
|
38
89
|
}
|
|
@@ -66,6 +117,9 @@ function clientLogin(clientKey, redirectUri) {
|
|
|
66
117
|
}
|
|
67
118
|
|
|
68
119
|
export function logout() {
|
|
120
|
+
// ✅ Reset callback state on logout
|
|
121
|
+
resetCallbackState();
|
|
122
|
+
|
|
69
123
|
const { clientKey, authBaseUrl, accountUiUrl } = getConfig();
|
|
70
124
|
const token = getToken();
|
|
71
125
|
|
|
@@ -80,15 +134,13 @@ export function logout() {
|
|
|
80
134
|
sessionStorage.clear();
|
|
81
135
|
|
|
82
136
|
if (isRouterMode()) {
|
|
83
|
-
// Router logout: Backend logout for all sessions
|
|
84
137
|
return routerLogout(clientKey, authBaseUrl, accountUiUrl, token);
|
|
85
138
|
} else {
|
|
86
|
-
// Client logout: Simple redirect to centralized login
|
|
87
139
|
return clientLogout(clientKey, accountUiUrl);
|
|
88
140
|
}
|
|
89
141
|
}
|
|
90
142
|
|
|
91
|
-
//
|
|
143
|
+
// Router logout (same as before)
|
|
92
144
|
async function routerLogout(clientKey, authBaseUrl, accountUiUrl, token) {
|
|
93
145
|
console.log('🏭 Router Logout: Backend logout for all sessions');
|
|
94
146
|
|
|
@@ -115,63 +167,12 @@ async function routerLogout(clientKey, authBaseUrl, accountUiUrl, token) {
|
|
|
115
167
|
}
|
|
116
168
|
}
|
|
117
169
|
|
|
118
|
-
// Fallback: redirect to login
|
|
119
170
|
window.location.href = '/login';
|
|
120
171
|
}
|
|
121
172
|
|
|
122
|
-
//
|
|
173
|
+
// Client logout (same as before)
|
|
123
174
|
function clientLogout(clientKey, accountUiUrl) {
|
|
124
175
|
console.log('🔄 Client Logout: Redirecting to centralized login');
|
|
125
176
|
const logoutUrl = `${accountUiUrl}/login?client=${clientKey}&logout=true`;
|
|
126
177
|
window.location.href = logoutUrl;
|
|
127
178
|
}
|
|
128
|
-
|
|
129
|
-
export function handleCallback() {
|
|
130
|
-
const params = new URLSearchParams(window.location.search);
|
|
131
|
-
const accessToken = params.get('access_token');
|
|
132
|
-
const error = params.get('error');
|
|
133
|
-
|
|
134
|
-
console.log('🔄 Handling authentication callback:', {
|
|
135
|
-
mode: isRouterMode() ? 'ROUTER' : 'CLIENT',
|
|
136
|
-
hasAccessToken: !!accessToken,
|
|
137
|
-
error
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
sessionStorage.removeItem('originalApp');
|
|
141
|
-
sessionStorage.removeItem('returnUrl');
|
|
142
|
-
|
|
143
|
-
if (error) {
|
|
144
|
-
throw new Error(`Authentication failed: ${error}`);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (accessToken) {
|
|
148
|
-
setToken(accessToken);
|
|
149
|
-
return accessToken;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
throw new Error('No access token found in callback URL');
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export async function refreshToken() {
|
|
156
|
-
const { clientKey, authBaseUrl } = getConfig();
|
|
157
|
-
|
|
158
|
-
console.log('🔄 Refreshing token:', { clientKey, mode: isRouterMode() ? 'ROUTER' : 'CLIENT' });
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
const response = await fetch(`${authBaseUrl}/refresh/${clientKey}`, {
|
|
162
|
-
method: 'POST',
|
|
163
|
-
credentials: 'include',
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
if (!response.ok) {
|
|
167
|
-
throw new Error('Refresh failed');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const { access_token } = await response.json();
|
|
171
|
-
setToken(access_token);
|
|
172
|
-
return access_token;
|
|
173
|
-
} catch (err) {
|
|
174
|
-
clearToken();
|
|
175
|
-
throw err;
|
|
176
|
-
}
|
|
177
|
-
}
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// auth-client/index.js
|
|
2
2
|
import { setConfig, getConfig, isRouterMode } from './config';
|
|
3
|
-
import { login, logout, handleCallback, refreshToken } from './core';
|
|
3
|
+
import { login, logout, handleCallback, refreshToken, resetCallbackState } from './core';
|
|
4
4
|
import { getToken, setToken, clearToken } from './token';
|
|
5
5
|
import api from './api';
|
|
6
6
|
import { decodeToken, isTokenExpired } from './utils/jwt';
|
|
@@ -9,13 +9,14 @@ export const auth = {
|
|
|
9
9
|
// 🔧 Config
|
|
10
10
|
setConfig,
|
|
11
11
|
getConfig,
|
|
12
|
-
isRouterMode,
|
|
12
|
+
isRouterMode,
|
|
13
13
|
|
|
14
14
|
// 🔐 Core flows
|
|
15
15
|
login,
|
|
16
16
|
logout,
|
|
17
17
|
handleCallback,
|
|
18
18
|
refreshToken,
|
|
19
|
+
resetCallbackState, // ✅ Export reset function
|
|
19
20
|
|
|
20
21
|
// 🔑 Token management
|
|
21
22
|
getToken,
|