11import type { ActionFunction , LoaderFunction } from '@remix-run/node'
22import { json } from '@remix-run/node'
33import { Form , useLoaderData , useNavigation } from '@remix-run/react'
4- import { Alert } from '~/components/alerts'
4+ import { Alert , ErrorAlert } from '~/components/alerts'
55import { Button } from '~/components/form-elements'
66import { auth } from '~/services/auth.server'
7- import { getUserSession } from '~/services/session.server'
7+ import { commitSession , getUserSession } from '~/services/session.server'
88
99export const loader : LoaderFunction = async ( { request } ) => {
1010 await auth . isAuthenticated ( request , { successRedirect : '/dashboard' } )
1111 const session = await getUserSession ( request )
1212 // This session key `auth:magiclink` is the default one used by the EmailLinkStrategy
1313 // you can customize it passing a `sessionMagicLinkKey` when creating an
1414 // instance.
15- return json ( {
16- user : session . get ( 'user' ) ,
17- magicLinkSent : session . has ( 'zain:magiclink' ) ,
18- } )
15+ const error = session . get ( auth . sessionErrorKey ) as
16+ | { message : string }
17+ | undefined
18+ return json (
19+ {
20+ user : session . get ( 'user' ) ,
21+ magicLinkSent : session . has ( 'zain:magiclink' ) ,
22+ error : error ?. message ,
23+ } ,
24+ {
25+ headers : {
26+ 'Set-Cookie' : await commitSession ( session ) ,
27+ } ,
28+ }
29+ )
1930}
2031
2132export const action : ActionFunction = async ( { request } ) => {
@@ -31,7 +42,10 @@ export const action: ActionFunction = async ({ request }) => {
3142}
3243
3344export default function Login ( ) {
34- const { magicLinkSent } = useLoaderData < { magicLinkSent : boolean } > ( )
45+ const { magicLinkSent, error } = useLoaderData < {
46+ magicLinkSent : boolean
47+ error ?: string
48+ } > ( )
3549 const { state } = useNavigation ( )
3650
3751 return (
@@ -79,6 +93,7 @@ export default function Login() {
7993 </ Button >
8094 </ div >
8195 </ Form >
96+ { error ? < ErrorAlert > { error } </ ErrorAlert > : null }
8297 { magicLinkSent ? (
8398 < Form action = "/logout" method = "post" >
8499 < input type = "hidden" name = "redirectTo" value = "/login" />
0 commit comments