|
1 | | -import { createFileRoute, Link, useNavigate } from "@tanstack/react-router"; |
2 | | -import React, { useState } from "react"; |
3 | | -import { Button } from "@/components/ui/button"; |
4 | | -import { |
5 | | - Card, |
6 | | - CardContent, |
7 | | - CardDescription, |
8 | | - CardHeader, |
9 | | - CardTitle, |
10 | | -} from "@/components/ui/card"; |
11 | | -import { Input } from "@/components/ui/input"; |
12 | | -import { Label } from "@/components/ui/label"; |
13 | | -import { ArrowLeft } from "lucide-react"; |
14 | | -import { useRegister } from "@/hooks/useAuth"; |
| 1 | +import { createFileRoute, Link, redirect, useNavigate } from '@tanstack/react-router' |
| 2 | +import { useState } from 'react' |
| 3 | +import { Button } from '@/components/ui/button' |
| 4 | +import { Input } from '@/components/ui/input' |
| 5 | +import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card' |
| 6 | +import { useAuthStore } from '@/stores/authStore' |
| 7 | +import { authService } from '@/services/auth.service' |
| 8 | +import { CheckCircle, Loader2 } from 'lucide-react' |
| 9 | +import { toast } from 'sonner' |
15 | 10 |
|
16 | | -export const Route = createFileRoute("/register")({ |
| 11 | +export const Route = createFileRoute('/register')({ |
| 12 | + beforeLoad: () => { |
| 13 | + if (useAuthStore.getState().isAuthenticated) { |
| 14 | + throw redirect({ to: '/todo' }) |
| 15 | + } |
| 16 | + }, |
17 | 17 | component: RegisterPage, |
18 | | -}); |
| 18 | +}) |
19 | 19 |
|
20 | 20 | function RegisterPage() { |
21 | | - const navigate = useNavigate(); |
22 | | - const registerMutation = useRegister(); |
23 | | - |
24 | | - const [formData, setFormData] = useState({ |
25 | | - username: "", |
26 | | - email: "", |
27 | | - password: "", |
28 | | - confirmPassword: "", |
29 | | - }); |
30 | | - const [error, setError] = useState(""); |
31 | | - |
32 | | - const handleSubmit = async (e: React.FormEvent) => { |
33 | | - e.preventDefault(); |
34 | | - setError(""); |
| 21 | + const [name, setName] = useState('') |
| 22 | + const [email, setEmail] = useState('') |
| 23 | + const [password, setPassword] = useState('') |
| 24 | + const [isLoading, setIsLoading] = useState(false) |
| 25 | + const { setUser, setAuthenticated, setTokens } = useAuthStore() |
| 26 | + const navigate = useNavigate() |
35 | 27 |
|
36 | | - if (formData.password !== formData.confirmPassword) { |
37 | | - setError("Passwords do not match"); |
38 | | - return; |
| 28 | + const handleRegister = async (e: React.FormEvent) => { |
| 29 | + e.preventDefault() |
| 30 | + setIsLoading(true) |
| 31 | + try { |
| 32 | + const response = await authService.register({ name, email, password }) |
| 33 | + setTokens(response.tokens) |
| 34 | + setUser(response.user) |
| 35 | + setAuthenticated(true) |
| 36 | + toast.success('Account created! Welcome 🎉') |
| 37 | + navigate({ to: '/todo' }) |
| 38 | + } catch (error: any) { |
| 39 | + toast.error(error.message || 'Registration failed') |
| 40 | + } finally { |
| 41 | + setIsLoading(false) |
39 | 42 | } |
| 43 | + } |
40 | 44 |
|
41 | | - registerMutation.mutate( |
42 | | - { |
43 | | - username: formData.username, |
44 | | - email: formData.email, |
45 | | - password: formData.password, |
46 | | - }, |
47 | | - { |
48 | | - onSuccess: () => { |
49 | | - navigate({ to: "/" }); |
50 | | - }, |
51 | | - onError: (error: any) => { |
52 | | - setError(error.message || "Registration failed. Please try again."); |
53 | | - }, |
54 | | - } |
55 | | - ); |
56 | | - }; |
57 | 45 | return ( |
58 | | - <div className="min-h-screen flex items-center justify-center bg-gray-50 relative"> |
59 | | - {/* Back to Home Button */} |
60 | | - <Link to="/" className="absolute top-4 left-4"> |
61 | | - <Button variant="ghost" size="sm"> |
62 | | - <ArrowLeft className="w-4 h-4 mr-2" /> |
63 | | - Back to Home |
64 | | - </Button> |
65 | | - </Link> |
66 | | - |
| 46 | + <div className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex items-center justify-center p-4"> |
67 | 47 | <Card className="w-full max-w-md"> |
68 | 48 | <CardHeader className="text-center"> |
69 | | - <CardTitle className="text-2xl">Sign Up</CardTitle> |
70 | | - <CardDescription>Create a new account to get started</CardDescription> |
| 49 | + <div className="flex justify-center mb-2"> |
| 50 | + <CheckCircle className="w-10 h-10 text-indigo-600" /> |
| 51 | + </div> |
| 52 | + <CardTitle className="text-2xl">Create account</CardTitle> |
| 53 | + <CardDescription>Start managing your todos with TanStack</CardDescription> |
71 | 54 | </CardHeader> |
72 | 55 | <CardContent> |
73 | | - <form onSubmit={handleSubmit} className="space-y-4"> |
74 | | - {(error || registerMutation.error) && ( |
75 | | - <div className="p-3 text-sm text-red-600 bg-red-50 border border-red-200 rounded-md"> |
76 | | - {error || |
77 | | - registerMutation.error?.message || |
78 | | - "Registration failed. Please try again."} |
79 | | - </div> |
80 | | - )} |
81 | | - |
| 56 | + <form onSubmit={handleRegister} className="space-y-4"> |
82 | 57 | <div className="space-y-2"> |
83 | | - <Label htmlFor="username">Username</Label> |
| 58 | + <label className="text-sm font-medium text-gray-700">Name</label> |
84 | 59 | <Input |
85 | | - id="username" |
86 | | - type="text" |
87 | | - placeholder="Enter your username" |
88 | | - value={formData.username} |
89 | | - onChange={(e) => |
90 | | - setFormData({ ...formData, username: e.target.value }) |
91 | | - } |
| 60 | + placeholder="Your name" |
| 61 | + value={name} |
| 62 | + onChange={(e) => setName(e.target.value)} |
92 | 63 | required |
93 | 64 | /> |
94 | 65 | </div> |
95 | | - |
96 | 66 | <div className="space-y-2"> |
97 | | - <Label htmlFor="email">Email</Label> |
| 67 | + <label className="text-sm font-medium text-gray-700">Email</label> |
98 | 68 | <Input |
99 | | - id="email" |
100 | 69 | type="email" |
101 | | - placeholder="Enter your email" |
102 | | - value={formData.email} |
103 | | - onChange={(e) => |
104 | | - setFormData({ ...formData, email: e.target.value }) |
105 | | - } |
106 | | - required |
107 | | - /> |
108 | | - </div> |
109 | | - |
110 | | - <div className="space-y-2"> |
111 | | - <Label htmlFor="password">Password</Label> |
112 | | - <Input |
113 | | - id="password" |
114 | | - type="password" |
115 | | - placeholder="Enter your password" |
116 | | - value={formData.password} |
117 | | - onChange={(e) => |
118 | | - setFormData({ ...formData, password: e.target.value }) |
119 | | - } |
| 70 | + placeholder="you@example.com" |
| 71 | + value={email} |
| 72 | + onChange={(e) => setEmail(e.target.value)} |
120 | 73 | required |
121 | 74 | /> |
122 | 75 | </div> |
123 | | - |
124 | 76 | <div className="space-y-2"> |
125 | | - <Label htmlFor="confirmPassword">Confirm Password</Label> |
| 77 | + <label className="text-sm font-medium text-gray-700">Password</label> |
126 | 78 | <Input |
127 | | - id="confirmPassword" |
128 | 79 | type="password" |
129 | | - placeholder="Confirm your password" |
130 | | - value={formData.confirmPassword} |
131 | | - onChange={(e) => |
132 | | - setFormData({ ...formData, confirmPassword: e.target.value }) |
133 | | - } |
| 80 | + placeholder="Min. 6 characters" |
| 81 | + value={password} |
| 82 | + onChange={(e) => setPassword(e.target.value)} |
134 | 83 | required |
| 84 | + minLength={6} |
135 | 85 | /> |
136 | 86 | </div> |
137 | | - |
138 | | - <Button |
139 | | - type="submit" |
140 | | - className="w-full" |
141 | | - disabled={registerMutation.isPending} |
142 | | - > |
143 | | - {registerMutation.isPending ? "Creating Account..." : "Sign Up"} |
| 87 | + <Button type="submit" className="w-full" disabled={isLoading}> |
| 88 | + {isLoading ? <Loader2 className="w-4 h-4 animate-spin mr-2" /> : null} |
| 89 | + Create Account |
144 | 90 | </Button> |
145 | 91 | </form> |
146 | | - <div className="text-center"> |
147 | | - <div className="text-sm text-gray-600"> |
148 | | - Already have an account?{" "} |
149 | | - <Link |
150 | | - to="/login" |
151 | | - className="text-blue-600 hover:underline" |
152 | | - search={{ |
153 | | - redirect: "/", |
154 | | - }} |
155 | | - > |
156 | | - Sign in |
157 | | - </Link> |
158 | | - </div> |
159 | | - </div> |
160 | 92 | </CardContent> |
| 93 | + <CardFooter className="text-center text-sm text-gray-600"> |
| 94 | + Already have an account?{" "} |
| 95 | + <Link to="/login" className="text-indigo-600 hover:underline ml-1"> |
| 96 | + Sign in |
| 97 | + </Link> |
| 98 | + </CardFooter> |
161 | 99 | </Card> |
162 | 100 | </div> |
163 | | - ); |
| 101 | + ) |
164 | 102 | } |
0 commit comments