Skip to content

Commit b5b3e0f

Browse files
committed
feat: clean up register page
1 parent f7011b2 commit b5b3e0f

1 file changed

Lines changed: 67 additions & 129 deletions

File tree

client/src/routes/register.tsx

Lines changed: 67 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,102 @@
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'
1510

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+
},
1717
component: RegisterPage,
18-
});
18+
})
1919

2020
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()
3527

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)
3942
}
43+
}
4044

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-
};
5745
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">
6747
<Card className="w-full max-w-md">
6848
<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>
7154
</CardHeader>
7255
<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">
8257
<div className="space-y-2">
83-
<Label htmlFor="username">Username</Label>
58+
<label className="text-sm font-medium text-gray-700">Name</label>
8459
<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)}
9263
required
9364
/>
9465
</div>
95-
9666
<div className="space-y-2">
97-
<Label htmlFor="email">Email</Label>
67+
<label className="text-sm font-medium text-gray-700">Email</label>
9868
<Input
99-
id="email"
10069
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)}
12073
required
12174
/>
12275
</div>
123-
12476
<div className="space-y-2">
125-
<Label htmlFor="confirmPassword">Confirm Password</Label>
77+
<label className="text-sm font-medium text-gray-700">Password</label>
12678
<Input
127-
id="confirmPassword"
12879
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)}
13483
required
84+
minLength={6}
13585
/>
13686
</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
14490
</Button>
14591
</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>
16092
</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>
16199
</Card>
162100
</div>
163-
);
101+
)
164102
}

0 commit comments

Comments
 (0)