mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
fix: access_token freshness issues
This commit is contained in:
parent
23e5fa0525
commit
953207cf8b
13 changed files with 89 additions and 45 deletions
|
|
@ -1,6 +1,6 @@
|
|||
"use client";
|
||||
import React, { useEffect } from "react";
|
||||
import { getRefreshToken, getUserInfo } from "../../services/auth/auth";
|
||||
import { getNewAccessTokenUsingRefreshToken, getUserInfo } from "../../services/auth/auth";
|
||||
import { useRouter, usePathname } from "next/navigation";
|
||||
|
||||
export const AuthContext: any = React.createContext({});
|
||||
|
|
@ -21,8 +21,15 @@ const AuthProvider = ({ children }: any) => {
|
|||
|
||||
const [auth, setAuth] = React.useState<Auth>({ access_token: "", isAuthenticated: false, userInfo: {}, isLoading: true });
|
||||
|
||||
function deleteCookie(cookieName: string) {
|
||||
console.log("Deleting cookie: " + cookieName);
|
||||
document.cookie = cookieName + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
}
|
||||
|
||||
|
||||
async function checkRefreshToken() {
|
||||
let data = await getRefreshToken();
|
||||
deleteCookie("access_token_cookie");
|
||||
let data = await getNewAccessTokenUsingRefreshToken();
|
||||
if (data) {
|
||||
return data.access_token;
|
||||
}
|
||||
|
|
@ -61,13 +68,13 @@ const AuthProvider = ({ children }: any) => {
|
|||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (auth.isLoading) {
|
||||
checkAuth();
|
||||
}
|
||||
checkRefreshToken();
|
||||
checkAuth();
|
||||
console.log("pathname", pathname);
|
||||
return () => {
|
||||
auth.isLoading = false;
|
||||
};
|
||||
}, []);
|
||||
}, [pathname]);
|
||||
|
||||
return <AuthContext.Provider value={auth}>{children}</AuthContext.Provider>;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue