-
Notifications
You must be signed in to change notification settings - Fork 52
/
01-auth.ts
117 lines (99 loc) · 2.89 KB
/
01-auth.ts
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
'use server';
import { db } from '@/drizzle/db';
import { users } from '@/drizzle/schema';
import {
FormState,
LoginFormSchema,
SignupFormSchema,
} from '@/app/auth/definitions';
import { createSession, deleteSession } from '@/app/auth/02-stateless-session';
import bcrypt from 'bcrypt';
import { eq } from 'drizzle-orm';
export async function signup(
state: FormState,
formData: FormData,
): Promise<FormState> {
// 1. Validate form fields
const validatedFields = SignupFormSchema.safeParse({
name: formData.get('name'),
email: formData.get('email'),
password: formData.get('password'),
});
// If any form fields are invalid, return early
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
};
}
// 2. Prepare data for insertion into database
const { name, email, password } = validatedFields.data;
// 3. Check if the user's email already exists
const existingUser = await db.query.users.findFirst({
where: eq(users.email, email),
});
if (existingUser) {
return {
message: 'Email already exists, please use a different email or login.',
};
}
// Hash the user's password
const hashedPassword = await bcrypt.hash(password, 10);
// 3. Insert the user into the database or call an Auth Provider's API
const data = await db
.insert(users)
.values({
name,
email,
password: hashedPassword,
})
.returning({ id: users.id });
const user = data[0];
if (!user) {
return {
message: 'An error occurred while creating your account.',
};
}
// 4. Create a session for the user
const userId = user.id.toString();
await createSession(userId);
}
export async function login(
state: FormState,
formData: FormData,
): Promise<FormState> {
// 1. Validate form fields
const validatedFields = LoginFormSchema.safeParse({
email: formData.get('email'),
password: formData.get('password'),
});
const errorMessage = { message: 'Invalid login credentials.' };
// If any form fields are invalid, return early
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
};
}
// 2. Query the database for the user with the given email
const user = await db.query.users.findFirst({
where: eq(users.email, validatedFields.data.email),
});
// If user is not found, return early
if (!user) {
return errorMessage;
}
// 3. Compare the user's password with the hashed password in the database
const passwordMatch = await bcrypt.compare(
validatedFields.data.password,
user.password,
);
// If the password does not match, return early
if (!passwordMatch) {
return errorMessage;
}
// 4. If login successful, create a session for the user and redirect
const userId = user.id.toString();
await createSession(userId);
}
export async function logout() {
deleteSession();
}