@myrjfa/state 1.1.2 → 1.1.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../../../src/lib/actions/fetcher.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../../../src/lib/actions/fetcher.ts"],"names":[],"mappings":"AAwJA,wBAAsB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,GAAE,OAAc,cAEpE;AAED,wBAAsB,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,cAEnD;AAED,wBAAsB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,cAElD;AAED,wBAAsB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,cAEpD;AAED,wBAAsB,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,cAEnD"}
|
|
@@ -85,9 +85,18 @@ async function refreshToken(input, method, body) {
|
|
|
85
85
|
const role = await getRole();
|
|
86
86
|
try {
|
|
87
87
|
const roleForEndpoint = role == "admin" ? "user" : role;
|
|
88
|
+
// #2: Include device ID header in refresh token request (must match backend validation)
|
|
89
|
+
const deviceId = getOrCreateDeviceId();
|
|
90
|
+
const headers = {
|
|
91
|
+
'Content-Type': 'application/json',
|
|
92
|
+
};
|
|
93
|
+
if (deviceId) {
|
|
94
|
+
headers['X-Device-Id'] = deviceId;
|
|
95
|
+
}
|
|
88
96
|
const response = await fetch(`${baseURL}/${roleForEndpoint}s/refresh-token`, {
|
|
89
97
|
method: 'POST',
|
|
90
98
|
credentials: 'include',
|
|
99
|
+
headers,
|
|
91
100
|
});
|
|
92
101
|
const data = await response.json();
|
|
93
102
|
if (response.ok) {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { useAtom } from 'jotai'; // Import useSetAtom
|
|
3
3
|
import { useEffect } from 'react';
|
|
4
4
|
import { useQuery } from '@tanstack/react-query';
|
|
5
|
-
import { initializeAuthSessionAtom } from './userAtom'; // Import the atom itself
|
|
5
|
+
import { initializeAuthSessionAtom, resetAuthState } from './userAtom'; // Import the atom itself
|
|
6
6
|
import { validateSession } from './actions/auth';
|
|
7
7
|
export default function AuthSessionManager() {
|
|
8
8
|
const [, initializeAuthSession] = useAtom(initializeAuthSessionAtom);
|
|
@@ -31,11 +31,11 @@ export default function AuthSessionManager() {
|
|
|
31
31
|
// - Wrong role endpoint (hosts vs users)
|
|
32
32
|
// The fetcher's refreshToken logic already handles 401s transparently.
|
|
33
33
|
// Aggressively resetting here was causing false logouts after browser restart.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (isError) {
|
|
36
|
+
console.log("Session validation failed. Logging out.");
|
|
37
|
+
resetAuthState();
|
|
38
|
+
}
|
|
39
|
+
}, [isError, resetAuthState]);
|
|
40
40
|
return null;
|
|
41
41
|
}
|
package/dist/lib/userAtom.js
CHANGED
|
@@ -3,6 +3,7 @@ import CryptoJS from 'crypto-js';
|
|
|
3
3
|
import { atom, getDefaultStore } from 'jotai';
|
|
4
4
|
import { atomWithStorage } from 'jotai/utils';
|
|
5
5
|
import { getCurrentUser } from './actions/actions';
|
|
6
|
+
import { validateSession } from './actions/auth';
|
|
6
7
|
const STORAGE_KEY = process.env.NEXT_PUBLIC_USER_KEY;
|
|
7
8
|
const LOGIN_KEY = process.env.NEXT_PUBLIC_LOGIN_KEY;
|
|
8
9
|
const SECRET_KEY = process.env.NEXT_PUBLIC_RACE;
|
|
@@ -77,14 +78,15 @@ export const initializeAuthSessionAtom = atom(null, async (get, set) => {
|
|
|
77
78
|
if (typeof window === 'undefined')
|
|
78
79
|
return;
|
|
79
80
|
// 1. Validate session (role-aware — host route differs from user route)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
const session = await validateSession();
|
|
82
|
+
if (session.success) {
|
|
83
|
+
console.log(session.message);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
resetAuthState();
|
|
87
|
+
set(storageInitializedAtom, true);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
88
90
|
// NOTE: if validateSession fails we do NOT reset — we just fall through
|
|
89
91
|
// to cache/API. A failed ping could mean the backend is temporarily down,
|
|
90
92
|
// the user is a host (different endpoint), etc. We only clear state if we
|