import { useContext, useMemo, useCallback, createContext, useState, } from 'react' import * as Auth from 'aws-amplify/auth' export type AuthContextType = { authUser: Auth.FetchUserAttributesOutput | null signIn: ({ username, password, }: { username: string password: string }) => Promise signOut: () => Promise } const AuthContext = createContext(null) export function useAuth(): AuthContextType { const ctx = useContext(AuthContext) if (!ctx) { throw new Error('useAuth must be used within an AuthProvider') } return ctx } export function AuthProvider({ children }: { children: React.ReactNode }) { const [authUser, setAuthUser] = useState(null) const signIn = useCallback( async ({ username, password, }: { username: string password: string }): Promise => { const signInOut = await Auth.signIn({ username, password, options: { clientMetadata: {}, }, }) if (signInOut?.isSignedIn) { setAuthUser(await Auth.fetchUserAttributes()) } return signInOut }, [], ) const signOut = useCallback(async (): Promise => { try { return await Auth.signOut() } catch {} }, []) const authContext = useMemo( () => ({ authUser, signIn, signOut, }), [authUser, signIn, signOut], ) return ( {children} ) }