-
Notifications
You must be signed in to change notification settings - Fork 3
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
8 changed files
with
464 additions
and
10 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
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,63 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { type UserPrivate } from "../utils/types"; | ||
import { | ||
AuthStateEnum, | ||
type AuthState, | ||
CurrentUserContext, | ||
} from "../contexts/CurrentUserContext"; | ||
import * as Api from "../utils/api"; | ||
import { removeApiTokens, doApiTokensExist } from "../utils/cookies"; | ||
|
||
export const CurrentUserProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [userData, setUserData] = useState<{ | ||
user?: UserPrivate; | ||
authState: AuthState; | ||
}>({ | ||
authState: AuthStateEnum.LOADING, | ||
}); | ||
|
||
const login = (user: UserPrivate): void => { | ||
setUserData({ | ||
user, | ||
authState: AuthStateEnum.AUTHENTICATED, | ||
}); | ||
}; | ||
const logout = (): void => { | ||
setUserData({ | ||
authState: AuthStateEnum.NOT_AUTHENTICATED, | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
const checkLoggedIn = async (): Promise<void> => { | ||
// check if cookies exist before attempting to load user | ||
if (!doApiTokensExist()) { | ||
logout(); | ||
return; | ||
} | ||
try { | ||
const user = await Api.getUserUserProfile(); | ||
login(user); | ||
} catch (error) { | ||
logout(); | ||
removeApiTokens(); | ||
} | ||
}; | ||
void checkLoggedIn(); | ||
}, []); | ||
|
||
return ( | ||
<CurrentUserContext.Provider | ||
value={{ | ||
authState: userData.authState, | ||
user: userData.user, | ||
login, | ||
logout, | ||
}} | ||
> | ||
{children} | ||
</CurrentUserContext.Provider> | ||
); | ||
}; |
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,23 @@ | ||
import React from "react"; | ||
import { AuthStateEnum, useCurrentUser } from "../contexts/CurrentUserContext"; | ||
import { Outlet, useNavigate } from "react-router-dom"; | ||
import Spinner from "./Spinner"; | ||
|
||
const PrivateRoute: React.FC = () => { | ||
const { authState } = useCurrentUser(); | ||
const navigate = useNavigate(); | ||
if (authState === AuthStateEnum.AUTHENTICATED) { | ||
return <Outlet />; | ||
} else if (authState === AuthStateEnum.NOT_AUTHENTICATED) { | ||
navigate("/login"); | ||
return null; | ||
} else { | ||
return ( | ||
<div className="h-screen flex items-center justify-center"> | ||
<Spinner /> | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
export default PrivateRoute; |
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,33 @@ | ||
import { createContext, useContext } from "react"; | ||
import { type UserPrivate } from "../utils/types/model/models"; | ||
|
||
export enum AuthStateEnum { | ||
LOADING = "loading", | ||
AUTHENTICATED = "authenticated", | ||
NOT_AUTHENTICATED = "not_authenticated", | ||
} | ||
|
||
export type AuthState = `${AuthStateEnum}`; | ||
|
||
interface CurrentUserContextType { | ||
authState: AuthState; | ||
user?: UserPrivate; | ||
login: (user: UserPrivate) => void; | ||
logout: () => void; | ||
} | ||
|
||
export const CurrentUserContext = createContext<CurrentUserContextType | null>( | ||
null | ||
); | ||
|
||
export const useCurrentUser = (): CurrentUserContextType => { | ||
const currentUserContext = useContext(CurrentUserContext); | ||
|
||
if (currentUserContext === null) { | ||
throw new Error( | ||
"useCurrentUser has to be used within <CurrentUserContext.Provider>" | ||
); | ||
} | ||
|
||
return currentUserContext; | ||
}; |
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
Oops, something went wrong.