add amplify

This commit is contained in:
2025-02-21 11:00:37 -03:00
parent 30eff74c08
commit d863791649
26 changed files with 540 additions and 421 deletions

View File

@@ -1,50 +0,0 @@
import { useMemo, useCallback, createContext, useState } from 'react'
import * as Auth from 'aws-amplify/auth'
const AuthContext = createContext(null)
export function useAuth() {
return useContext(AuthContext)
}
export function AuthProvider({ children }) {
const [authUser, setAuthUser] = useState(null)
const currentUser = useCallback(async () => {
try {
const currentUser = await Auth.fetchUserAttributes()
setAuthUser(currentUser)
return currentUser
} catch {
setAuthUser(null)
}
}, [])
const signIn = useCallback(async ({ username, password }) => {
const signInOut = await Auth.signIn({
username,
password,
options: {
clientMetadata: {},
},
})
if (signInOut?.isSignedIn) {
setAuthUser(await Auth.fetchUserAttributes())
}
return signInOut
}, [])
const ctxValue = useMemo(
() => ({
authUser,
signIn,
currentUser,
}),
[authUser]
)
return <AuthContext.Provider value={ctxValue}>{children}</AuthContext.Provider>
}