This commit is contained in:
2025-02-21 14:56:02 -03:00
parent a8d0667b89
commit 634f422528
9 changed files with 335 additions and 39 deletions

View File

@@ -16,7 +16,7 @@ interface AuthContextType {
username: string
password: string
}) => Promise<Auth.SignInOutput>
currentUser: () => Promise<Auth.FetchUserAttributesOutput | void>
signOut: () => Promise<void>
}
const AuthContext = createContext<AuthContextType | null>(null)
@@ -29,17 +29,6 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const [authUser, setAuthUser] =
useState<Auth.FetchUserAttributesOutput | null>(null)
const currentUser = useCallback(async () => {
try {
const currentUser = await Auth.fetchUserAttributes()
setAuthUser(currentUser)
return currentUser
} catch {
setAuthUser(null)
}
}, [])
const signIn = useCallback(
async ({
username,
@@ -65,16 +54,22 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
[],
)
const ctxValue = useMemo<AuthContextType>(
const signOut = useCallback(async (): Promise<void> => {
try {
return await Auth.signOut()
} catch {}
}, [])
const authContext = useMemo<AuthContextType>(
() => ({
authUser,
signIn,
currentUser,
signOut,
}),
[authUser, signIn, currentUser],
[authUser, signIn, signOut],
)
return (
<AuthContext.Provider value={ctxValue}>{children}</AuthContext.Provider>
<AuthContext.Provider value={authContext}>{children}</AuthContext.Provider>
)
}