-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathSignin.tsx
More file actions
329 lines (310 loc) · 12.1 KB
/
Signin.tsx
File metadata and controls
329 lines (310 loc) · 12.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'use client';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Separator } from '@/components/ui/separator';
import { signIn } from 'next-auth/react';
import { useRouter } from 'next/navigation';
import React, { useRef, useState, useEffect } from 'react';
import { toast } from 'sonner';
import { motion } from 'framer-motion';
const emailDomains = [
'gmail.com',
'yahoo.com',
'outlook.com',
'icloud.com',
'hotmail.com',
'rediffmail.com',
];
const Signin = () => {
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
const [checkingPassword, setCheckingPassword] = useState(false);
const [requiredError, setRequiredError] = useState({
emailReq: false,
passReq: false,
});
const [suggestedDomains, setSuggestedDomains] =
useState<string[]>(emailDomains);
const [focusedIndex, setFocusedIndex] = useState(-1);
const passwordRef = useRef<HTMLInputElement>(null);
const suggestionRefs = useRef<HTMLLIElement[]>([]);
const dropdownRef = useRef<HTMLUListElement>(null);
function togglePasswordVisibility() {
setIsPasswordVisible((prevState: any) => !prevState);
}
const router = useRouter();
// const email = useRef('');
// const password = useRef('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
// email.current = value.trim();
setEmail(value);
setFocusedIndex(0);
setRequiredError((prevState) => ({
...prevState,
emailReq: false,
}));
// Check if the input is a phone number
const phoneNumberRegex = /^[0-9]{10}$/;
if (phoneNumberRegex.test(value)) {
setSuggestedDomains([]); // Clear suggestions for phone numbers
return;
}
if (!value.includes('@')) {
setSuggestedDomains(emailDomains);
return;
}
const [, currentDomain] = value.split('@');
// Clear suggestions if domain doesn't match
if (
!currentDomain ||
!emailDomains.some((domain) => domain.startsWith(currentDomain))
) {
setSuggestedDomains([]); // Hide suggestions for mismatched domains
return;
}
// Check for exact matches and filter for partial matches
const exactMatch = emailDomains.find((domain) => domain === currentDomain);
if (exactMatch) {
setSuggestedDomains([]);
return;
}
const matchingDomains = emailDomains.filter((domain) =>
domain.startsWith(currentDomain),
);
setSuggestedDomains(matchingDomains);
};
const handleSuggestionClick = (domain: string) => {
const [username] = email.split('@');
const newEmail = `${username}@${domain}`;
setEmail(newEmail);
passwordRef.current?.focus();
setSuggestedDomains([]);
};
// Handle keyboard events for navigating and selecting suggestions
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter' && focusedIndex >= 0 && suggestedDomains.length > 0) {
handleSuggestionClick(suggestedDomains[focusedIndex]);
} else if (e.key === 'ArrowDown') {
e.preventDefault();
setFocusedIndex((prevIndex) =>
Math.min(prevIndex + 1, suggestedDomains.length - 1),
);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
setFocusedIndex((prevIndex) => Math.max(prevIndex - 1, 0));
}
};
const handleSubmit = async (e?: React.FormEvent<HTMLButtonElement>) => {
const loadId = toast.loading('Signing in...');
if (e) {
e.preventDefault();
}
if (!email || !password) {
setRequiredError({
emailReq: email ? false : true,
passReq: password ? false : true,
});
toast.dismiss(loadId);
return;
}
setCheckingPassword(true);
const res = await signIn('credentials', {
username: email.trim(),
password: password,
redirect: false,
});
console.log('SignIn Response:', res);
toast.dismiss(loadId);
if (!res?.error) {
router.push('/');
toast.success('Signed In');
} else {
if (res.status === 401) {
toast.error('Invalid Credentials, try again!');
} else if (res.status === 400) {
toast.error('Missing Credentials!');
} else if (res.status === 404) {
toast.error('Account not found!');
} else if (res.status === 403) {
toast.error('Forbidden!');
} else {
toast.error('oops something went wrong..!');
}
setCheckingPassword(false);
}
};
// Handle clicks outside the dropdown
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target as Node)
) {
setSuggestedDomains([]);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, []);
return (
<section className="wrapper relative flex min-h-screen items-center justify-center overflow-hidden antialiased">
<motion.div
initial={{ y: -40, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={{
duration: 0.5,
ease: 'easeInOut',
type: 'spring',
damping: 10,
}}
className="flex w-full flex-col justify-between gap-12 rounded-2xl bg-primary/5 p-8 sm:max-w-[26rem]"
>
<div className="flex flex-col text-center">
<h2 className="text-3xl font-semibold tracking-tighter xl:text-4xl">
Welcome to{' '}
<span className="bg-gradient-to-b from-blue-400 to-blue-700 bg-clip-text pr-1 font-black tracking-tighter text-transparent">
100xDevs
</span>
</h2>
<p className="text-lg font-medium tracking-tighter text-primary/75 md:text-xl">
Log in to access paid content!
</p>
</div>
<div className="flex flex-col gap-8">
<div className="grid w-full items-center gap-4">
<div className="relative flex flex-col gap-2">
<Label htmlFor="email">Email</Label>
<Input
className="focus:ring-none border-none bg-primary/5 focus:outline-none"
name="email"
id="email"
placeholder="name@email.com"
value={email}
onChange={handleEmailChange}
onKeyDown={handleKeyDown}
onBlur={() => setSuggestedDomains([])} // Hide suggestions on blur
/>
{email && suggestedDomains.length > 0 && (
<ul
ref={dropdownRef}
className={`absolute top-20 z-50 max-h-96 w-full min-w-[8rem] overflow-auto rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2`}
>
{suggestedDomains.map((domain: string, index: number) => (
<>
<li
key={domain}
value={domain}
//@ts-ignore
ref={(listItem) =>
(suggestionRefs.current[index] = listItem!)
}
onMouseDown={() => handleSuggestionClick(domain)}
onClick={() => handleSuggestionClick(domain)}
className={`relative flex w-full cursor-default select-none items-center rounded-sm p-2 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 ${
focusedIndex === index
? 'bg-primary-foreground font-medium'
: ''
}`}
>
{email.split('@')[0]}@{domain}
</li>
{index < suggestedDomains.length - 1 && <Separator />}
</>
))}
</ul>
)}
{requiredError.emailReq && (
<span className="text-red-500">Email is required</span>
)}
</div>
<div className="relative flex flex-col gap-2">
<Label>Password</Label>
<div className="flex">
<Input
className="focus:ring-none border-none bg-primary/5 focus:outline-none"
name="password"
type={isPasswordVisible ? 'text' : 'password'}
id="password"
placeholder="••••••••"
ref={passwordRef}
onChange={(e) => {
setRequiredError((prevState) => ({
...prevState,
passReq: false,
}));
setPassword(e.target.value);
}}
onKeyDown={async (e) => {
if (e.key === 'Enter') {
setIsPasswordVisible(false);
handleSubmit();
}
}}
/>
<button
className="absolute bottom-0 right-0 flex h-10 items-center px-4 text-neutral-500"
onClick={togglePasswordVisibility}
>
{isPasswordVisible ? (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="h-5 w-5"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M3.98 8.223A10.477 10.477 0 001.934 12C3.226 16.338 7.244 19.5 12 19.5c.993 0 1.953-.138 2.863-.395M6.228 6.228A10.45 10.45 0 0112 4.5c4.756 0 8.773 3.162 10.065 7.498a10.523 10.523 0 01-4.293 5.774M6.228 6.228L3 3m3.228 3.228l3.65 3.65m7.894 7.894L21 21m-3.228-3.228l-3.65-3.65m0 0a3 3 0 10-4.243-4.243m4.242 4.242L9.88 9.88"
/>
</svg>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="h-5 w-5"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M2.036 12.322a1.012 1.012 0 010-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178z"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
/>
</svg>
)}
</button>
</div>
{requiredError.passReq && (
<span className="text-red-500">Password is required</span>
)}
</div>
</div>
<Button
size={'lg'}
variant={'branding'}
disabled={!email || !password || checkingPassword}
onClick={handleSubmit}
>
Login
</Button>
</div>
</motion.div>
<div className="absolute -bottom-[16rem] -z-[20] size-[24rem] overflow-hidden rounded-full bg-gradient-to-t from-blue-400 to-blue-700 blur-[16em]" />
</section>
);
};
export default Signin;