Skip to content

Commit 002cad6

Browse files
committed
Rerouting to login after unauth
1 parent 7a486d9 commit 002cad6

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/pages/_app.jsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import { Context } from '../context';
77
import { useEffect, useState } from 'react';
88
import request from '@/utils/request';
99
import { Analytics } from "@vercel/analytics/react"
10+
import { usePathname, useRouter } from 'next/navigation';
11+
1012

1113
export default function App({ Component, pageProps }) {
1214
const [username, setUsername] = useState('');
1315
const [role, setRole] = useState('');
1416
const [accountType, setAccountType] = useState('');
1517
const [profilePic, setProfilePic] = useState('');
1618
const [points, setPoints] = useState(0);
19+
const path = usePathname();
20+
const router = useRouter();
1721

1822
const appState = {
1923
role, setRole,
@@ -26,7 +30,14 @@ export default function App({ Component, pageProps }) {
2630
const fetchUser = async () => {
2731
const url = process.env.NEXT_PUBLIC_API_URL + "/account";
2832
const user = await request(url, "GET", null);
29-
console.log(user);
33+
34+
if(user && user.error && user.error === "Not authorized") {
35+
const pathNames = ["/", "/login", "/careers", "/register",
36+
"/onboarding", "/forgot-password", "/education", "/userrs",
37+
"/privacy-policy","/404", "/terms-of-service", "/learn"];
38+
if(!pathNames.includes(path)) router.push("/login");
39+
}
40+
3041
if (user && user.username) {
3142
setRole(user.role);
3243
setUsername(user.username);

0 commit comments

Comments
 (0)