-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
125 additions
and
31 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,12 @@ | ||
type User = { | ||
createdAt: string; | ||
email: string; | ||
id: number; | ||
isEmailVerified: boolean; | ||
isPasswordSet: boolean; | ||
name: string; | ||
role: UserRole; | ||
updatedAt: string; | ||
}; | ||
|
||
type UserRole = "ADMIN" | "OWNER" | "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ReactNode, useCallback, useEffect, useMemo, useState } from "react"; | ||
import { useErrorBoundary } from "react-error-boundary"; | ||
import AuthContext, { AuthContextType } from "~/contexts/AuthContext"; | ||
import { setUserAccessToken } from "~/lib/user-access-token"; | ||
|
||
export default function AuthProvider({ children }: { children: ReactNode }) { | ||
const { showBoundary } = useErrorBoundary(); | ||
const [user] = useState<AuthContextType["user"] | null>(null); | ||
const canWrite = useMemo<AuthContextType["canWrite"]>(() => ["ADMIN", "OWNER"].includes(user?.role || ""), [user]); | ||
const isOrganizationOwner = useMemo<AuthContextType["isOrganizationOwner"]>(() => user?.role === "OWNER", [user]); | ||
|
||
const getAuthUser = useCallback(async () => { | ||
try { | ||
/* TODO: Update endpoint */ | ||
// const response = await api.get("/authentication/user"); | ||
// const responseJson = await response.json(); | ||
// | ||
// if (response.ok) { | ||
// setUser(responseJson); | ||
// } else { | ||
// await signOut(); | ||
// } | ||
} catch (error) { | ||
return showBoundary(error); | ||
} | ||
}, []); | ||
|
||
const refreshUser = useCallback(async () => getAuthUser(), [getAuthUser]); | ||
|
||
const signOut = useCallback(async () => { | ||
setUserAccessToken(null); | ||
|
||
await getAuthUser(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
getAuthUser(); | ||
}, []); | ||
|
||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
canWrite, | ||
isOrganizationOwner, | ||
refreshUser, | ||
signOut, | ||
user, | ||
}} | ||
> | ||
{children} | ||
</AuthContext.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
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,19 @@ | ||
import { createContext } from "react"; | ||
|
||
export type AuthContextType = { | ||
canWrite: boolean; | ||
isOrganizationOwner: boolean; | ||
refreshUser: () => void; | ||
signOut: () => void; | ||
user: User | null; | ||
}; | ||
|
||
const AuthContext = createContext<AuthContextType>({ | ||
canWrite: false, | ||
isOrganizationOwner: false, | ||
refreshUser: () => {}, | ||
signOut: () => {}, | ||
user: null, | ||
}); | ||
|
||
export default 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useContext } from "react"; | ||
import AuthContext from "~/contexts/AuthContext"; | ||
|
||
export default function useUser() { | ||
const { user } = useContext(AuthContext); | ||
|
||
if (AuthContext === undefined) { | ||
throw new Error("useUser must be used within a UserProvider"); | ||
} | ||
|
||
return user; | ||
} |