Best practices to use actions/mutations for auth? #10198
Unanswered
bodokaiser
asked this question in
Q&A
Replies: 1 comment
-
My proposal would be to have a export async function userLoader() {
const user = await Auth.currentAuthenticatedUser()
return { id: user.getUsername(), ...user.attributes }
} A export function useUser() {
return useContext(UserContext)
}
export function UserProvider({ children }: { children: ReactNode }) {
const user = useLoaderData() as User | null
return <UserContext.Provider value={user}>{children}</UserContext.Provider>
} The sign-in route could be something like {
path: 'sign-in',
action: async ({ request }: ActionFunctionArgs) => {
const formData = await request.formData()
const email = formData.get('email') as string
const password = formData.get('password') as string
return await Auth.signIn(email, password)
},
element: (
<RequireNoUser>
<Form action="post">
<input type="email" name="email" />
<input type="password" name="password" />
<button type="submit">Sign-in</button>
</Form>
</RequireNoUser>
),
} wherein What do you guys think? What could one improve (I am new to the data loader and action API)? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey together :),
has anyone used the new action/mutation feature for authentication?
If yes, could you share some details on how you structured the routes and actions?
Thank you and all the best,
Bodo
Beta Was this translation helpful? Give feedback.
All reactions