mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
fix: token refreshing using wrong object path
This commit is contained in:
@@ -16,12 +16,14 @@ export function getRefreshToken() {
|
|||||||
|
|
||||||
export function setTokens(token: string, refreshToken: string) {
|
export function setTokens(token: string, refreshToken: string) {
|
||||||
if (!browser) return;
|
if (!browser) return;
|
||||||
|
console.log('Setting tokens');
|
||||||
document.cookie = `${TOKEN_KEY}=${token}; path=/; secure; samesite=strict`;
|
document.cookie = `${TOKEN_KEY}=${token}; path=/; secure; samesite=strict`;
|
||||||
document.cookie = `${REFRESH_TOKEN_KEY}=${refreshToken}; path=/; secure; samesite=strict`;
|
document.cookie = `${REFRESH_TOKEN_KEY}=${refreshToken}; path=/; secure; samesite=strict`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clearTokens() {
|
export function clearTokens() {
|
||||||
if (!browser) return;
|
if (!browser) return;
|
||||||
|
console.log('Clearing tokens');
|
||||||
document.cookie = `${TOKEN_KEY}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
document.cookie = `${TOKEN_KEY}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
||||||
document.cookie = `${REFRESH_TOKEN_KEY}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
document.cookie = `${REFRESH_TOKEN_KEY}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
||||||
}
|
}
|
||||||
@@ -36,6 +38,7 @@ export async function refreshTokenIfNeeded() {
|
|||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
if (exp - now < 2 * 60 * 1000) {
|
if (exp - now < 2 * 60 * 1000) {
|
||||||
|
console.log('Refreshing token...');
|
||||||
const res = await fetch('/api/auth/refresh', {
|
const res = await fetch('/api/auth/refresh', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
@@ -43,15 +46,19 @@ export async function refreshTokenIfNeeded() {
|
|||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data.token && data.refreshToken) {
|
if (data.token.token && data.token.refreshToken && data.token.success) {
|
||||||
setTokens(data.token, data.refreshToken);
|
console.log('Token refreshed successfully');
|
||||||
|
setTokens(data.token.token, data.token.refreshToken);
|
||||||
} else {
|
} else {
|
||||||
|
console.log('Invalid tokens received during refresh');
|
||||||
clearTokens();
|
clearTokens();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
console.log('Failed to refresh token');
|
||||||
clearTokens();
|
clearTokens();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log('Token is still valid, no refresh needed');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function startTokenRefreshInterval() {
|
export function startTokenRefreshInterval() {
|
||||||
|
|||||||
Reference in New Issue
Block a user