mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-11 00:32:38 +02:00
Add logout with token revocation and loading states
This commit is contained in:
parent
287e5afbac
commit
f13345b226
7 changed files with 98 additions and 20 deletions
|
|
@ -104,6 +104,39 @@ export function clearAllTokens(): void {
|
|||
clearRefreshToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout the current user by revoking the refresh token and clearing localStorage.
|
||||
* Returns true if logout was successful (or tokens were cleared), false otherwise.
|
||||
*/
|
||||
export async function logout(): Promise<boolean> {
|
||||
const refreshToken = getRefreshToken();
|
||||
|
||||
// Call backend to revoke the refresh token
|
||||
if (refreshToken) {
|
||||
try {
|
||||
const backendUrl = process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || "http://localhost:8000";
|
||||
const response = await fetch(`${backendUrl}/auth/jwt/revoke`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ refresh_token: refreshToken }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.warn("Failed to revoke refresh token:", response.status, await response.text());
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Failed to revoke refresh token on server:", error);
|
||||
// Continue to clear local tokens even if server call fails
|
||||
}
|
||||
}
|
||||
|
||||
// Clear all tokens from localStorage
|
||||
clearAllTokens();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user is authenticated (has a token)
|
||||
*/
|
||||
|
|
@ -161,7 +194,7 @@ async function refreshAccessToken(): Promise<string | null> {
|
|||
isRefreshing = true;
|
||||
refreshPromise = (async () => {
|
||||
try {
|
||||
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL || "";
|
||||
const backendUrl = process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || "http://localhost:8000";
|
||||
const response = await fetch(`${backendUrl}/auth/jwt/refresh`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue