add loading

This commit is contained in:
2025-02-20 21:39:30 -03:00
parent a5dec435c5
commit 30eff74c08
7 changed files with 298 additions and 33 deletions

View File

@@ -0,0 +1,50 @@
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>
}