-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
178 lines (157 loc) · 4.75 KB
/
auth.ts
File metadata and controls
178 lines (157 loc) · 4.75 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
import NextAuth from 'next-auth';
import prisma from '@datalib/_prisma/client';
import Credentials from '@auth/core/providers/credentials';
import bcrypt from 'bcrypt';
import {
emailSchema,
passwordSchema,
phoneNumberSchema,
zipCodeSchema,
} from '@utils/InputValidation';
import InvalidLoginError from '@error/auth/InvalidLoginError';
/**
* Interface definition for required credentials.
*
* Only email and password are required for login.
* Everything is required for sign-up.
*/
export interface Credentials {
name?: string;
email: string; // Required for login only.
password: string; // Required for login only.
phone?: string;
addressLine1?: string;
addressLine2?: string;
city?: string;
state?: string;
country?: string;
zip?: number;
}
function validateCredentials(credentials: Credentials, isLogin: boolean) {
const emailValidation = emailSchema.safeParse(credentials.email);
const passwordValidation = passwordSchema.safeParse(credentials.password);
const phoneValidation = phoneNumberSchema.safeParse(credentials.phone);
const zipValidation = zipCodeSchema.safeParse(credentials.zip);
if (credentials.email && !emailValidation.success)
throw new InvalidLoginError(emailValidation.error.errors[0].message);
if (!isLogin && credentials.password && !passwordValidation.success)
throw new InvalidLoginError(passwordValidation.error.errors[0].message);
if (credentials.phone && !phoneValidation.success)
throw new InvalidLoginError(phoneValidation.error.errors[0].message);
if (credentials.zip && !zipValidation.success)
throw new InvalidLoginError(zipValidation.error.errors[0].message);
}
async function credentialsSignUp(credentials: Credentials) {
const {
name,
email,
password,
phone,
addressLine1,
addressLine2, // Not required for sign up.
city,
state,
country,
zip,
} = credentials;
if (
!name ||
!email ||
!password ||
!phone ||
!addressLine1 ||
!city ||
!state ||
!country ||
!zip
) {
throw new InvalidLoginError('Missing fields for sign up');
}
const hashedPassword = await bcrypt.hash(password, 12);
return prisma.user.create({
data: {
name: name,
email: email,
password: hashedPassword,
phone: phone,
shipping_address_line_1: addressLine1,
shipping_address_line_2: addressLine2,
shipping_city: city,
shipping_state: state,
shipping_country: country,
shipping_zip: parseInt(String(zip)), // For some reason, this is also required.
},
});
}
async function credentialsLogIn(credentials: Credentials) {
const {
name,
email,
password,
phone,
addressLine1,
addressLine2,
city,
state,
country,
zip,
} = credentials;
// This may be redundant.
if (
name ||
phone ||
addressLine1 ||
addressLine2 ||
city ||
state ||
country ||
zip
) {
throw new InvalidLoginError('Too many fields for login.');
}
if (!email || !password)
throw new InvalidLoginError('Email and password are required');
const user = await prisma.user.findUnique({
where: { email: email },
});
if (!user) throw new InvalidLoginError('Invalid credentials');
const passwordsMatch = await bcrypt.compare(password, user.password);
if (!passwordsMatch) throw new InvalidLoginError('Invalid credentials');
return user;
}
export const { handlers, auth, signIn, signOut } = NextAuth({
session: { strategy: 'jwt' },
providers: [
Credentials({
// http://localhost:3000/api/auth/signin
credentials: {
firstName: { label: 'First Name', type: 'text' },
lastName: { label: 'Last Name', type: 'text' },
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' },
phone: { label: 'Phone', type: 'text' },
addressLine1: { label: 'Address Line 1', type: 'text' },
addressLine2: {
label: 'Address Line 2',
type: 'text',
required: false,
},
city: { label: 'City', type: 'text' },
state: { label: 'State', type: 'text' },
country: { label: 'Country', type: 'text' },
zip: { label: 'ZIP Code', type: 'number' },
},
async authorize(credentials) {
const typedCredentials = credentials as Credentials; // Required for some reason.
const totalUsers = await prisma.user.count();
// Sign up logic. Return signed-up user.
if (totalUsers === 0) {
validateCredentials(typedCredentials, false);
return credentialsSignUp(typedCredentials);
}
validateCredentials(typedCredentials, true);
return credentialsLogIn(typedCredentials); // Login logic. Return logged-in user.
},
}),
],
});