mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: auth exceptions + roles with user object
This commit is contained in:
parent
23c4224b2b
commit
143f21b70b
11 changed files with 74 additions and 68 deletions
|
|
@ -1,8 +1,10 @@
|
|||
import React, { useEffect } from "react";
|
||||
import { getRefreshToken, getUserInfo } from "../../services/auth/auth";
|
||||
import { useRouter } from "next/router";
|
||||
|
||||
export const AuthContext: any = React.createContext({});
|
||||
|
||||
const NON_AUTHENTICATED_ROUTES = ["/login", "/signup"];
|
||||
export interface Auth {
|
||||
access_token: string;
|
||||
isAuthenticated: boolean;
|
||||
|
|
@ -10,7 +12,9 @@ export interface Auth {
|
|||
isLoading: boolean;
|
||||
}
|
||||
|
||||
|
||||
const AuthProvider = (props: any) => {
|
||||
const router = useRouter();
|
||||
const [auth, setAuth] = React.useState<Auth>({ access_token: "", isAuthenticated: false, userInfo: {}, isLoading: true });
|
||||
|
||||
async function checkRefreshToken() {
|
||||
|
|
@ -19,30 +23,35 @@ const AuthProvider = (props: any) => {
|
|||
}
|
||||
|
||||
async function checkAuth() {
|
||||
let access_token = await checkRefreshToken();
|
||||
let isAuthenticated = false;
|
||||
let userInfo = {};
|
||||
let isLoading = false;
|
||||
try {
|
||||
let access_token = await checkRefreshToken();
|
||||
let userInfo = {};
|
||||
let isLoading = false;
|
||||
|
||||
if (access_token) {
|
||||
userInfo = await getUserInfo(access_token);
|
||||
setAuth({ access_token, isAuthenticated: true, userInfo, isLoading });
|
||||
|
||||
if (access_token) {
|
||||
userInfo = await getUserInfo(access_token);
|
||||
isAuthenticated = true;
|
||||
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||
} else{
|
||||
isAuthenticated = false;
|
||||
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||
// if user is authenticated and tries to access login or signup page, redirect to home
|
||||
if(NON_AUTHENTICATED_ROUTES.includes(router.pathname)) {
|
||||
router.push("/");
|
||||
}
|
||||
} else {
|
||||
setAuth({ access_token, isAuthenticated: false, userInfo, isLoading });
|
||||
router.push("/login");
|
||||
}
|
||||
} catch (error) {
|
||||
router.push("/");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (auth.isLoading) {
|
||||
checkAuth();
|
||||
}
|
||||
return () => {
|
||||
auth.isLoading = false;
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return <AuthContext.Provider value={auth}>{props.children}</AuthContext.Provider>;
|
||||
|
|
|
|||
|
|
@ -1,42 +0,0 @@
|
|||
import React from "react";
|
||||
import { getRefreshToken, getUserInfo } from "../../services/auth/auth";
|
||||
import { Auth, AuthContext } from "./AuthProvider";
|
||||
|
||||
const AuthenticatedOnly = (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 {
|
||||
isAuthenticated = false;
|
||||
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||
}
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
{auth.isLoading && <div>Loading...</div>}
|
||||
{!auth.isLoading && auth.isAuthenticated && <div>{props.children}</div>}
|
||||
{!auth.isLoading && !auth.isAuthenticated && <div>Not Authenticated</div>}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default AuthenticatedOnly;
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
import React from "react";
|
||||
import styled from "styled-components";
|
||||
import Link from "next/link";
|
||||
import { AuthContext } from "../security/AuthProvider";
|
||||
import { getBackendUrl } from "../../services/config";
|
||||
import { AuthContext } from "./AuthProvider";
|
||||
import Avvvatars from "avvvatars-react";
|
||||
|
||||
export const HeaderProfileBox = () => {
|
||||
|
|
@ -28,9 +27,9 @@ export const HeaderProfileBox = () => {
|
|||
)}
|
||||
{auth.isAuthenticated && (
|
||||
<AccountArea>
|
||||
<div>{auth.userInfo.username}</div>
|
||||
<div>{auth.userInfo.user_object.username}</div>
|
||||
<div>
|
||||
<Avvvatars value={auth.userInfo.user_id} style="shape" />
|
||||
<Avvvatars value={auth.userInfo.user_object.user_id} style="shape" />
|
||||
</div>
|
||||
</AccountArea>
|
||||
)}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import React from "react";
|
||||
import styled from "styled-components";
|
||||
import { HeaderProfileBox } from "../../auth/HeaderProfileBox";
|
||||
import { HeaderProfileBox } from "../../security/HeaderProfileBox";
|
||||
import learnhouseIcon from "public/learnhouse_icon.png";
|
||||
import learnhouseLogo from "public/learnhouse_logo.png";
|
||||
import Link from "next/link";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue