1
1
import * as React from 'react' ;
2
2
import type { FC , ChangeEvent } from "react" ;
3
3
import { Container , Grid } from '@mui/material' ;
4
- import { useNavigate , Form , useSubmit , useActionData } from '@remix-run/react' ;
4
+ import { useNavigate } from '@remix-run/react' ;
5
5
import { TextField , Typography , Button , CircularProgress } from '@mui/material' ;
6
6
import ReCAPTCHA from 'react-google-recaptcha' ;
7
7
import type { ReCAPTCHA as ReCAPTCHAType } from 'react-google-recaptcha' ;
8
8
import UserContext from '../contexts/UserContext' ;
9
9
10
10
interface LoginProps {
11
- onLogin : ( ) => void ;
11
+ onLogin : ( username : string , password : string , recaptcha : string ) => Promise < { success : boolean } > ;
12
12
recaptchaSiteKey : string ;
13
13
}
14
14
15
- export const Login : FC < LoginProps > = ( { recaptchaSiteKey } ) => {
15
+ export const Login : FC < LoginProps > = ( { recaptchaSiteKey, onLogin } ) => {
16
16
const [ username , setUsername ] = React . useState < string > ( "" ) ;
17
17
const [ password , setPassword ] = React . useState < string > ( "" ) ;
18
18
const [ usernameInvalid , setUsernameInvalid ] = React . useState < boolean > ( false ) ;
19
19
const [ passwordInvalid , setPasswordInvalid ] = React . useState < boolean > ( false ) ;
20
20
const [ isLoading , setIsLoading ] = React . useState < boolean > ( false ) ;
21
21
22
22
const navigate = useNavigate ( ) ;
23
- const submit = useSubmit ( ) ;
24
23
const recaptchaRef = React . useRef < ReCAPTCHAType > ( null ) ;
25
- const formRef = React . useRef < HTMLFormElement > ( null ) ;
26
- const actionData = useActionData < { success ?: boolean ; error ?: string ; user ?: any } > ( ) ;
27
- const { updateUser } = React . useContext ( UserContext ) ;
28
-
29
- React . useEffect ( ( ) => {
30
- if ( actionData ?. success && actionData . user ) {
31
- updateUser ( actionData . user ) ;
32
- navigate ( "/" ) ;
33
- }
34
- if ( actionData ?. error ) {
35
- setIsLoading ( false ) ;
36
- }
37
- } , [ actionData , navigate , updateUser ] ) ;
24
+ const [ error , setError ] = React . useState < string > ( "" ) ;
38
25
39
26
const validateForm = ( ) : boolean => {
40
27
const isUsernameInvalid = ! username ;
@@ -65,23 +52,20 @@ export const Login: FC<LoginProps> = ({ recaptchaSiteKey }) => {
65
52
return ;
66
53
}
67
54
68
- if ( formRef . current ) {
69
- const formData = new FormData ( formRef . current ) ;
70
- formData . set ( 'username' , username ) ;
71
- formData . set ( 'password' , password ) ;
72
- formData . set ( 'recaptcha' , token ) ;
73
-
74
- console . log ( 'Submitting form with data:' , {
75
- username,
76
- recaptcha : 'present'
77
- } ) ;
78
-
79
- submit ( formData , {
80
- method : 'post'
81
- } ) ;
55
+ console . log ( 'Calling onLogin with data:' , {
56
+ username,
57
+ recaptcha : 'present'
58
+ } ) ;
59
+
60
+ // Call the client-side login handler
61
+ const result = await onLogin ( username , password , token ) ;
62
+ if ( result . success ) {
63
+ setIsLoading ( false ) ;
82
64
}
65
+
83
66
} catch ( error ) {
84
- console . error ( 'Error during form submission:' , error ) ;
67
+ console . error ( 'Error during login:' , error ) ;
68
+ setError ( error instanceof Error ? error . message : 'Login failed' ) ;
85
69
setIsLoading ( false ) ;
86
70
}
87
71
} ;
@@ -101,12 +85,12 @@ export const Login: FC<LoginProps> = ({ recaptchaSiteKey }) => {
101
85
< Grid container justifyContent = "center" >
102
86
< Grid item xs = { 12 } md = { 8 } lg = { 3 } >
103
87
< Typography variant = "h4" component = "h2" sx = { { mb : 3 } } > Login</ Typography >
104
- { actionData ?. error && (
88
+ { error && (
105
89
< Typography color = "error" sx = { { mb : 2 } } >
106
- { actionData . error }
90
+ { error }
107
91
</ Typography >
108
92
) }
109
- < Form ref = { formRef } method = "post" >
93
+ < form >
110
94
< input type = "hidden" name = "recaptcha" value = "" />
111
95
< TextField
112
96
id = "username"
@@ -168,7 +152,7 @@ export const Login: FC<LoginProps> = ({ recaptchaSiteKey }) => {
168
152
>
169
153
Forgot Password?
170
154
</ Button >
171
- </ Form >
155
+ </ form >
172
156
</ Grid >
173
157
</ Grid >
174
158
</ Container >
0 commit comments