mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init auth across the app
This commit is contained in:
parent
9479a4b127
commit
3b85a73ec1
12 changed files with 204 additions and 29 deletions
|
|
@ -1,12 +1,15 @@
|
|||
import React from "react";
|
||||
import styled from "styled-components";
|
||||
import Link from "next/link";
|
||||
import { AuthContext } from "../security/AuthProvider";
|
||||
|
||||
export const HeaderProfileBox = () => {
|
||||
const auth: any = React.useContext(AuthContext);
|
||||
|
||||
return (
|
||||
<ProfileArea>
|
||||
{" "}
|
||||
<span>HeaderProfileBox</span>{" "}
|
||||
<span>HeaderProfileBox {String(auth.isAuthenticated)}</span>{" "}
|
||||
<UnidentifiedArea>
|
||||
<ul>
|
||||
<li>
|
||||
|
|
|
|||
47
front/components/security/AuthProvider.tsx
Normal file
47
front/components/security/AuthProvider.tsx
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import React, { useEffect } from "react";
|
||||
import { getRefreshToken, getUserInfo } from "../../services/auth/auth";
|
||||
|
||||
export const AuthContext: any = React.createContext({});
|
||||
|
||||
export interface Auth {
|
||||
access_token: string;
|
||||
isAuthenticated: boolean;
|
||||
userInfo: {};
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
const AuthProvider = (props: any) => {
|
||||
const [auth, setAuth] = React.useState<Auth>({ access_token: "", isAuthenticated: false, userInfo: {}, isLoading: true });
|
||||
|
||||
async function checkRefreshToken() {
|
||||
let data = await getRefreshToken();
|
||||
return data.access_token;
|
||||
}
|
||||
|
||||
async function checkAuth() {
|
||||
let access_token = await checkRefreshToken();
|
||||
let isAuthenticated = false;
|
||||
let userInfo = {};
|
||||
let isLoading = false;
|
||||
|
||||
if (access_token) {
|
||||
userInfo = await getUserInfo(access_token);
|
||||
isAuthenticated = true;
|
||||
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||
} else if (!access_token) {
|
||||
isAuthenticated = false;
|
||||
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(mvp) : fix performance issues > no need to check auth on every render
|
||||
useEffect(() => {
|
||||
if (!auth.isAuthenticated) {
|
||||
checkAuth();
|
||||
}
|
||||
}, []);
|
||||
|
||||
return <AuthContext.Provider value={auth}>{props.children}</AuthContext.Provider>;
|
||||
};
|
||||
|
||||
export default AuthProvider;
|
||||
|
|
@ -6,8 +6,10 @@ import Link from "next/link";
|
|||
export const Menu = () => {
|
||||
return (
|
||||
<GlobalHeader>
|
||||
|
||||
<LogoArea>
|
||||
<Logo>
|
||||
<img style={{ width: "30px", opacity: "0.9", margin: "10px", paddingRight: "4px" }} src="./learnhouse_icon.png" alt="" />
|
||||
<Link href={"/"}>
|
||||
<a>
|
||||
<img src="./learnhouse_logo.png" alt="" />
|
||||
|
|
@ -50,7 +52,9 @@ const Logo = styled.div`
|
|||
display: flex;
|
||||
place-items: center;
|
||||
padding-left: 20px;
|
||||
|
||||
a{
|
||||
margin: 0;
|
||||
}
|
||||
img {
|
||||
width: 100px;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,23 +2,26 @@ import React from "react";
|
|||
import Head from "next/head";
|
||||
import { Header } from "./header";
|
||||
import styled from "styled-components";
|
||||
import AuthProvider from "../security/AuthProvider";
|
||||
|
||||
const Layout = (props: any) => {
|
||||
return (
|
||||
<div>
|
||||
<Head>
|
||||
<title>{props.title}</title>
|
||||
<meta name="description" content={props.description} />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
</Head>
|
||||
<Header></Header>
|
||||
<Main className="min-h-screen">{props.children}</Main>
|
||||
<AuthProvider>
|
||||
<Head>
|
||||
<title>{props.title}</title>
|
||||
<meta name="description" content={props.description} />
|
||||
<link rel="icon" href="/favicon.ico" />
|
||||
</Head>
|
||||
<Header></Header>
|
||||
<Main className="min-h-screen">{props.children}</Main>
|
||||
|
||||
<Footer>
|
||||
<a href="" target="_blank" rel="noopener noreferrer">
|
||||
<img src="/learnhouse_icon.png" alt="Learnhouse Logo" />
|
||||
</a>
|
||||
</Footer>
|
||||
<Footer>
|
||||
<a href="" target="_blank" rel="noopener noreferrer">
|
||||
<img src="/learnhouse_icon.png" alt="Learnhouse Logo" />
|
||||
</a>
|
||||
</Footer>
|
||||
</AuthProvider>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
|
@ -33,9 +36,9 @@ const Footer = styled.footer`
|
|||
margin: 20px;
|
||||
font-size: 16px;
|
||||
|
||||
img{
|
||||
width: 40px;
|
||||
opacity: 0.40;
|
||||
img {
|
||||
width: 20px;
|
||||
opacity: 0.4;
|
||||
display: inline;
|
||||
}
|
||||
`;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue