-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathauth.ts
More file actions
67 lines (61 loc) · 2.18 KB
/
Copy pathauth.ts
File metadata and controls
67 lines (61 loc) · 2.18 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
import { z } from 'zod';
/**
* Password validation requirements.
*
* A valid password must:
* - Be at least 8 characters long
* - Contain at least one uppercase letter
* - Contain at least one lowercase letter
* - Contain at least one numeric digit
* - Contain at least one special character
*/
const passwordSchema = z
.string()
.min(8, 'Password must be at least 8 characters')
.regex(/[A-Z]/, 'Password must contain at least 1 uppercase letter')
.regex(/[a-z]/, 'Password must contain at least 1 lowercase letter')
.regex(/[0-9]/, 'Password must contain at least 1 number')
.regex(
/[!@#$%^&*(),.?":{}|<>]/,
'Password must contain at least 1 special character'
);
/**
* Validation schema for user registration.
*
* Fields:
* - name: User's display name (minimum 2 characters)
* - email: Valid email address
* - password: Must satisfy the password policy
*/
export const signupSchema = z.object({
name: z.string().min(2, 'Name is required'),
email: z.string().email('Invalid email address'),
password: z.string()
.min(8, 'Password must be at least 8 characters')
.regex(/[A-Z]/, 'Password must contain at least 1 uppercase letter')
.regex(/[a-z]/, 'Password must contain at least 1 lowercase letter')
.regex(/[0-9]/, 'Password must contain at least 1 number')
.regex(/[@$!%*?&]/, 'Password must contain at least 1 special character'),
});
export function getPasswordError(password: string): string | null {
if (password.length < 8) return 'Must be at least 8 characters';
if (!/[A-Z]/.test(password)) return 'Must include one uppercase letter';
if (!/[a-z]/.test(password)) return 'Must include one lowercase letter';
if (!/\d/.test(password)) return 'Must include one number';
if (!/[@$!%*?&]/.test(password)) return 'Must include one special character';
return null;
}
name: z
.string()
.min(2, 'Name is required'),
email: z
.email('Invalid email address'),
password: passwordSchema,
});
/**
* Type inferred from the signup validation schema.
*
* Use this type to ensure consistency between validation
* and application logic.
*/
export type SignupInput = z.infer<typeof signupSchema>;