Session token refresh
This commit is contained in:
+13
-2
@@ -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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user