Session token refresh

This commit is contained in:
2025-10-22 17:06:31 +02:00
parent 06631f8069
commit 3ca3f377b0
3 changed files with 44 additions and 2 deletions
+13 -2
View File
@@ -6,6 +6,7 @@ import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { get } from "@/lib/request";
import { toast } from "sonner";
import axios from "axios";
export interface UserProfile extends ProfileSettings {
id: string;
@@ -20,8 +21,18 @@ export default function useUser() {
try {
setUser((await get("/api/profile", (r) => setUser(r.data))).data);
} catch (e: any) {
router.push("/auth?t=login");
toast.error(`Error: ${e}`);
try {
console.log("Failed to obtain current user, refreshing token..");
const refresh = await axios.put("/api/auth/");
const { access_token, refresh_token } = refresh.data as any;
Cookies.set("token", access_token);
Cookies.set("refresh_token", refresh_token);
setUser((await get("/api/profile", (r) => setUser(r.data))).data);
} catch (e: any) {
router.push("/auth?t=login");
toast.error(`Error: ${e}`);
}
}
}