wip
This commit is contained in:
@@ -1,14 +1,33 @@
|
||||
import { useMemo, useCallback, createContext, useState } from 'react'
|
||||
import {
|
||||
useContext,
|
||||
useMemo,
|
||||
useCallback,
|
||||
createContext,
|
||||
useState,
|
||||
} from 'react'
|
||||
import * as Auth from 'aws-amplify/auth'
|
||||
|
||||
const AuthContext = createContext(null)
|
||||
interface AuthContextType {
|
||||
authUser: Auth.FetchUserAttributesOutput | null
|
||||
signIn: ({
|
||||
username,
|
||||
password,
|
||||
}: {
|
||||
username: string
|
||||
password: string
|
||||
}) => Promise<Auth.SignInOutput>
|
||||
currentUser: () => Promise<Auth.FetchUserAttributesOutput | void>
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextType | null>(null)
|
||||
|
||||
export function useAuth() {
|
||||
return useContext(AuthContext)
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }) {
|
||||
const [authUser, setAuthUser] = useState(null)
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [authUser, setAuthUser] =
|
||||
useState<Auth.FetchUserAttributesOutput | null>(null)
|
||||
|
||||
const currentUser = useCallback(async () => {
|
||||
try {
|
||||
@@ -21,30 +40,41 @@ export function AuthProvider({ children }) {
|
||||
}
|
||||
}, [])
|
||||
|
||||
const signIn = useCallback(async ({ username, password }) => {
|
||||
const signInOut = await Auth.signIn({
|
||||
const signIn = useCallback(
|
||||
async ({
|
||||
username,
|
||||
password,
|
||||
options: {
|
||||
clientMetadata: {},
|
||||
},
|
||||
})
|
||||
}: {
|
||||
username: string
|
||||
password: string
|
||||
}): Promise<Auth.SignInOutput> => {
|
||||
const signInOut = await Auth.signIn({
|
||||
username,
|
||||
password,
|
||||
options: {
|
||||
clientMetadata: {},
|
||||
},
|
||||
})
|
||||
|
||||
if (signInOut?.isSignedIn) {
|
||||
setAuthUser(await Auth.fetchUserAttributes())
|
||||
}
|
||||
if (signInOut?.isSignedIn) {
|
||||
setAuthUser(await Auth.fetchUserAttributes())
|
||||
}
|
||||
|
||||
return signInOut
|
||||
}, [])
|
||||
return signInOut
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
const ctxValue = useMemo(
|
||||
const ctxValue = useMemo<AuthContextType>(
|
||||
() => ({
|
||||
authUser,
|
||||
signIn,
|
||||
currentUser,
|
||||
}),
|
||||
[authUser]
|
||||
[authUser, signIn, currentUser],
|
||||
)
|
||||
|
||||
return <AuthContext.Provider value={ctxValue}>{children}</AuthContext.Provider>
|
||||
return (
|
||||
<AuthContext.Provider value={ctxValue}>{children}</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user