-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent CARE from reloading during sign in/out πββοΈ (#6828)
* Improve sign in flow * Improve sign out flow * Apply suggestions from code review * lints * i should have fixed lints from vscode itself ig
- Loading branch information
1 parent
e292fb7
commit 27c5766
Showing
10 changed files
with
141 additions
and
130 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 |
---|---|---|
@@ -1,14 +1,32 @@ | ||
import { createContext, useContext } from "react"; | ||
import { UserModel } from "../../Components/Users/models"; | ||
import { RequestResult } from "../../Utils/request/types"; | ||
import { JwtTokenObtainPair, LoginCredentials } from "../../Redux/api"; | ||
|
||
export const AuthUserContext = createContext<UserModel | null>(null); | ||
type SignInReturnType = RequestResult<JwtTokenObtainPair>; | ||
|
||
export default function useAuthUser() { | ||
const user = useContext(AuthUserContext); | ||
type AuthContextType = { | ||
user: UserModel | undefined; | ||
signIn: (creds: LoginCredentials) => Promise<SignInReturnType>; | ||
signOut: () => Promise<void>; | ||
}; | ||
|
||
if (!user) { | ||
throw new Error("useAuthUser must be used within an AuthUserProvider"); | ||
export const AuthUserContext = createContext<AuthContextType | null>(null); | ||
|
||
export const useAuthContext = () => { | ||
const ctx = useContext(AuthUserContext); | ||
if (!ctx) { | ||
throw new Error( | ||
"'useAuthContext' must be used within 'AuthUserProvider' only" | ||
); | ||
} | ||
return ctx; | ||
}; | ||
|
||
export default function useAuthUser() { | ||
const user = useAuthContext().user; | ||
if (!user) { | ||
throw new Error("'useAuthUser' must be used within 'AppRouter' only"); | ||
} | ||
return user; | ||
} |
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
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
Oops, something went wrong.