1
1
"use client" ;
2
- import { useState } from "react" ;
3
- import { useRouter } from "next/navigation" ;
2
+
3
+ import { useState , useEffect } from "react" ;
4
+ import { useRouter , useSearchParams } from "next/navigation" ;
5
+ import { signIn } from "next-auth/react" ;
4
6
import ActionButton from "@/components/ui/ActionButton" ;
5
7
import ErrorAlert from "@/components/ui/ErrorAlert" ;
8
+ import { colors } from "@/styles/colors" ;
6
9
7
10
export default function CreateAccountPage ( ) {
8
- const [ email , setEmail ] = useState ( "" ) ;
11
+ const router = useRouter ( ) ;
12
+ const searchParams = useSearchParams ( ) ;
13
+
14
+ const initialEmail = searchParams . get ( "email" ) || "" ;
15
+ const [ email , setEmail ] = useState ( initialEmail ) ;
9
16
const [ password , setPassword ] = useState ( "" ) ;
10
17
const [ orgName , setOrgName ] = useState ( "" ) ;
11
18
const [ error , setError ] = useState < string | null > ( null ) ;
12
- const router = useRouter ( ) ;
19
+ const [ loading , setLoading ] = useState ( false ) ;
20
+
21
+ useEffect ( ( ) => {
22
+ if ( initialEmail && ! orgName ) {
23
+ const namePart = initialEmail . split ( "@" ) [ 0 ] ;
24
+ const formatted = namePart
25
+ . replace ( / [ . \- _ ] / g, " " )
26
+ . replace ( / \b \w / g, ( c ) => c . toUpperCase ( ) ) ;
27
+ setOrgName ( formatted ) ;
28
+ }
29
+ } , [ initialEmail , orgName ] ) ;
13
30
14
31
const handleSubmit = async ( e : React . FormEvent ) => {
15
32
e . preventDefault ( ) ;
16
33
setError ( null ) ;
17
34
18
- // front-end validation
19
35
if ( ! email || ! password || ! orgName ) {
20
36
setError ( "Please fill out all fields." ) ;
21
37
return ;
22
38
}
23
39
24
40
try {
41
+ setLoading ( true ) ;
42
+
25
43
const res = await fetch ( `/flask-api/register` , {
26
44
method : "POST" ,
27
45
headers : { "Content-Type" : "application/json" } ,
28
46
body : JSON . stringify ( { orgName, email, password } ) ,
29
47
} ) ;
48
+
30
49
const data = await res . json ( ) ;
31
50
32
51
if ( res . ok ) {
33
- router . push ( "/login" ) ;
52
+ // After successful registration, automatically sign them in
53
+ const signInResult = await signIn ( "credentials" , {
54
+ redirect : false ,
55
+ email,
56
+ password,
57
+ } ) ;
58
+
59
+ if ( signInResult ?. ok ) {
60
+ router . push ( "/" ) ;
61
+ } else {
62
+ setError (
63
+ "Account created, but failed to log in. Please log in manually." ,
64
+ ) ;
65
+ router . push ( "/login" ) ; // Fallback
66
+ }
34
67
} else {
35
68
setError ( data . error || "Registration failed. Please try again." ) ;
36
69
}
37
70
} catch ( err ) {
38
71
console . error ( "Registration error:" , err ) ;
39
72
setError ( "An unexpected error occurred. Please try again." ) ;
73
+ } finally {
74
+ setLoading ( false ) ;
40
75
}
41
76
} ;
42
77
43
78
return (
44
- < div className = "flex justify-center items-center h-screen bg-gray-100" >
79
+ < div
80
+ className = "flex justify-center items-center h-screen"
81
+ style = { { backgroundColor : colors . secondary . base } }
82
+ >
45
83
< form
46
84
onSubmit = { handleSubmit }
47
85
className = "bg-white p-8 rounded-lg shadow-md w-96 space-y-6"
48
86
>
49
- < h2 className = "text-2xl font-bold text-center" > Create Account</ h2 >
87
+ < h2 className = "text-2xl font-bold text-center text-blue-900" >
88
+ Create Account
89
+ </ h2 >
50
90
51
91
< div >
52
92
< label className = "block text-gray-700 mb-1" > Organization Name</ label >
@@ -55,6 +95,7 @@ export default function CreateAccountPage() {
55
95
value = { orgName }
56
96
onChange = { ( e ) => setOrgName ( e . target . value ) }
57
97
className = "w-full px-3 py-2 border rounded-lg"
98
+ placeholder = "Your Organization"
58
99
required
59
100
/>
60
101
</ div >
@@ -66,6 +107,7 @@ export default function CreateAccountPage() {
66
107
value = { email }
67
108
onChange = { ( e ) => setEmail ( e . target . value ) }
68
109
className = "w-full px-3 py-2 border rounded-lg"
110
+
69
111
required
70
112
/>
71
113
</ div >
@@ -77,14 +119,19 @@ export default function CreateAccountPage() {
77
119
value = { password }
78
120
onChange = { ( e ) => setPassword ( e . target . value ) }
79
121
className = "w-full px-3 py-2 border rounded-lg"
122
+ placeholder = "Create a password"
80
123
required
81
124
/>
82
125
</ div >
83
126
84
- { /* error message */ }
85
127
{ error && < ErrorAlert message = { error } /> }
86
128
87
- < ActionButton type = "submit" label = "Create Account" variant = "primary" />
129
+ < ActionButton
130
+ type = "submit"
131
+ label = { loading ? "Creating..." : "Create Account" }
132
+ variant = "primary"
133
+ disabled = { loading }
134
+ />
88
135
</ form >
89
136
</ div >
90
137
) ;
0 commit comments