add auth
This commit is contained in:
@@ -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>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user