-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
78 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"use client" | ||
|
||
import { createContext, useContext, useState, useEffect } from 'react'; | ||
import { Dispatch, SetStateAction } from 'react'; | ||
import { User } from '../types'; | ||
import { useCookies } from 'react-cookie'; | ||
import { jwtDecode } from 'jwt-decode'; | ||
import { useRouter } from 'next/navigation'; | ||
|
||
type AuthContextType = { | ||
user: User | null | ||
setUser: Dispatch<SetStateAction<User | null>> | ||
} | ||
|
||
const AuthContext = createContext<AuthContextType>({ user: null, setUser: () => { } }); | ||
|
||
export const AuthProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => { | ||
const [cookies, setCookie, removeCookie] = useCookies(['user_token']); | ||
const [user, setUser] = useState<User | null>(null); | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
const token = cookies.user_token; | ||
if (token) { | ||
let userObject = jwtDecode(cookies.user_token) as User | ||
setUser(userObject); | ||
} else { | ||
router.push('/login'); | ||
} | ||
}, [cookies.user_token, router]); | ||
|
||
return ( | ||
<AuthContext.Provider value={{ user, setUser }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuth = () => useContext(AuthContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import { jwtDecode } from "jwt-decode"; | ||
import { useState } from "react"; | ||
import { useCookies } from "react-cookie" | ||
import { useAuth } from "./auth/authContext"; | ||
|
||
type UserObject = { | ||
exp: number | ||
id: number | ||
permissions: number | ||
username: string | ||
} | ||
|
||
export function Login() { | ||
const [cookies, setCookie, removeCookie] = useCookies(['user_token']); | ||
var userObject: UserObject | null = null | ||
if (cookies.user_token) { | ||
userObject = jwtDecode(cookies.user_token) | ||
} | ||
export function AccountTab() { | ||
const authDetails = useAuth() | ||
return ( | ||
<> | ||
{ | ||
cookies.user_token ? <p>{userObject?.username}</p> : <a className="p-button u-float-right" style={{ marginRight: "5px" }} href="login">Login</a> | ||
authDetails.user ? | ||
<div className="p-side-navigation__item" aria-current="false"> | ||
<i className="p-icon--user is-light p-side-navigation__icon"></i> | ||
<span className="p-side-navigation__label"> | ||
<span className="p-side-navigation__label">{authDetails.user.username}</span> | ||
</span> | ||
<i className="p-icon--menu" style={{ marginLeft: "auto", marginTop: "auto", marginBottom: "auto" }}></i> | ||
</div> | ||
: | ||
<a className="p-side-navigation__link" href="/login" aria-current="false"> | ||
<i className="p-icon--user is-light p-side-navigation__icon"></i> | ||
<span className="p-side-navigation__label"> | ||
<span className="p-side-navigation__label">Login</span> | ||
</span> | ||
</a> | ||
} | ||
</>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters