mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: dashboard left bar improvements
This commit is contained in:
parent
3413e6ca73
commit
6fea7ec1af
16 changed files with 124 additions and 29 deletions
58
apps/web/components/Security/AuthContext.tsx
Normal file
58
apps/web/components/Security/AuthContext.tsx
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import { getNewAccessTokenUsingRefreshToken, getUserInfo } from '@services/auth/auth';
|
||||
import { getAPIUrl } from '@services/config/config';
|
||||
import { swrFetcher } from '@services/utils/ts/requests';
|
||||
import React, { useEffect } from 'react'
|
||||
import useSWR from 'swr';
|
||||
|
||||
const AuthContext = React.createContext({})
|
||||
|
||||
type Auth = {
|
||||
access_token: string;
|
||||
isAuthenticated: boolean;
|
||||
user: any;
|
||||
}
|
||||
|
||||
function AuthProvider({ children, orgslug }: { children: React.ReactNode, orgslug: string }) {
|
||||
const [auth, setAuth] = React.useState<Auth>({ access_token: "", isAuthenticated: false, user: {} });
|
||||
|
||||
async function checkRefreshToken() {
|
||||
//deleteCookie("access_token_cookie");
|
||||
let data = await getNewAccessTokenUsingRefreshToken();
|
||||
if (data) {
|
||||
return data.access_token;
|
||||
}
|
||||
}
|
||||
|
||||
async function checkAuth() {
|
||||
try {
|
||||
let access_token = await checkRefreshToken();
|
||||
let userInfo = {};
|
||||
|
||||
if (access_token) {
|
||||
userInfo = await getUserInfo(access_token);
|
||||
setAuth({ access_token: access_token, isAuthenticated: true, user: userInfo });
|
||||
|
||||
} else {
|
||||
setAuth({ access_token: access_token, isAuthenticated: false, user: userInfo });
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
checkAuth();
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={auth}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
return React.useContext(AuthContext);
|
||||
}
|
||||
|
||||
export default AuthProvider
|
||||
Loading…
Add table
Add a link
Reference in a new issue