-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
184 lines (167 loc) · 6.97 KB
/
Copy pathpage.tsx
File metadata and controls
184 lines (167 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use client'
import { useState, useEffect } from 'react'
import Link from 'next/link'
import { useRouter, useSearchParams } from 'next/navigation'
import { useAuth } from '@/lib/auth-context'
import toast from 'react-hot-toast'
export default function LoginPage() {
const { signIn, signInWithGoogle, user, loading } = useAuth()
const router = useRouter()
const searchParams = useSearchParams()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [isSubmitting, setIsSubmitting] = useState(false)
const [isGoogleLoading, setIsGoogleLoading] = useState(false)
// Show any errors coming back from the OAuth callback
useEffect(() => {
const error = searchParams.get('error')
if (error) toast.error(decodeURIComponent(error))
}, [searchParams])
// Redirect already-logged-in users
useEffect(() => {
if (!loading && user) router.replace('/dashboard')
}, [user, loading, router])
const handleEmailLogin = async (e: React.FormEvent) => {
e.preventDefault()
if (!email || !password) return toast.error('Please fill in all fields')
setIsSubmitting(true)
const { error } = await signIn(email, password)
setIsSubmitting(false)
if (error) {
// Make common Supabase error messages friendlier
if (error.includes('Invalid login credentials')) {
toast.error('Incorrect email or password')
} else if (error.includes('Email not confirmed')) {
toast.error('Please confirm your email before signing in')
} else {
toast.error(error)
}
}
// signIn() handles the redirect on success
}
const handleGoogleLogin = async () => {
setIsGoogleLoading(true)
const { error } = await signInWithGoogle()
if (error) {
toast.error(error)
setIsGoogleLoading(false)
}
// Page will redirect to Google — no need to setIsGoogleLoading(false)
}
if (loading) {
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-green-600" />
</div>
)
}
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50 px-4">
<div className="w-full max-w-md">
{/* Logo / Header */}
<div className="text-center mb-8">
<Link href="/" className="text-2xl font-bold text-green-600">
TaskCloud
</Link>
<h1 className="mt-4 text-2xl font-semibold text-gray-900">Welcome back</h1>
<p className="mt-1 text-sm text-gray-500">Sign in to your account</p>
</div>
<div className="bg-white rounded-2xl shadow-sm border border-gray-200 p-8 space-y-6">
{/* Google OAuth */}
<button
type="button"
onClick={handleGoogleLogin}
disabled={isGoogleLoading || isSubmitting}
className="w-full flex items-center justify-center gap-3 px-4 py-2.5 border border-gray-300 rounded-lg text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 transition disabled:opacity-50 disabled:cursor-not-allowed"
>
{isGoogleLoading ? (
<span className="animate-spin rounded-full h-4 w-4 border-b-2 border-gray-600" />
) : (
<svg className="h-5 w-5" viewBox="0 0 24 24">
<path
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
fill="#4285F4"
/>
<path
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
fill="#34A853"
/>
<path
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l3.66-2.84z"
fill="#FBBC05"
/>
<path
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
fill="#EA4335"
/>
</svg>
)}
Continue with Google
</button>
<div className="relative">
<div className="absolute inset-0 flex items-center">
<div className="w-full border-t border-gray-200" />
</div>
<div className="relative flex justify-center text-xs text-gray-400 uppercase">
<span className="bg-white px-3">or</span>
</div>
</div>
{/* Email / Password */}
<form onSubmit={handleEmailLogin} className="space-y-4">
<div>
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1">
Email
</label>
<input
id="email"
type="email"
autoComplete="email"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-green-500 focus:border-transparent"
placeholder="you@example.com"
/>
</div>
<div>
<div className="flex justify-between items-center mb-1">
<label htmlFor="password" className="block text-sm font-medium text-gray-700">
Password
</label>
<Link
href="/auth/forgot-password"
className="text-xs text-green-600 hover:text-green-700"
>
Forgot password?
</Link>
</div>
<input
id="password"
type="password"
autoComplete="current-password"
required
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-green-500 focus:border-transparent"
placeholder="••••••••"
/>
</div>
<button
type="submit"
disabled={isSubmitting || isGoogleLoading}
className="w-full py-2.5 px-4 bg-green-600 text-white text-sm font-medium rounded-lg hover:bg-green-700 transition disabled:opacity-50 disabled:cursor-not-allowed"
>
{isSubmitting ? 'Signing in…' : 'Sign in'}
</button>
</form>
<p className="text-center text-sm text-gray-500">
Don't have an account?{' '}
<Link href="/auth/signup" className="text-green-600 font-medium hover:text-green-700">
Sign up
</Link>
</p>
</div>
</div>
</div>
)
}