forked from bigcommerce/catalyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
194 lines (172 loc) · 4.38 KB
/
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
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
import { cookies } from 'next/headers';
import NextAuth, { type DefaultSession, type NextAuthConfig } from 'next-auth';
import 'next-auth/jwt';
import CredentialsProvider from 'next-auth/providers/credentials';
import { z } from 'zod';
import { client } from './client';
import { graphql } from './client/graphql';
const LoginMutation = graphql(`
mutation Login($email: String!, $password: String!) {
login(email: $email, password: $password) {
customer {
entityId
firstName
lastName
email
}
}
}
`);
const AssignCartToCustomerMutation = graphql(`
mutation AssignCartToCustomer($assignCartToCustomerInput: AssignCartToCustomerInput!) {
cart {
assignCartToCustomer(input: $assignCartToCustomerInput) {
cart {
entityId
}
}
}
}
`);
const UnassignCartFromCustomerMutation = graphql(`
mutation UnassignCartFromCustomer(
$unassignCartFromCustomerInput: UnassignCartFromCustomerInput!
) {
cart {
unassignCartFromCustomer(input: $unassignCartFromCustomerInput) {
cart {
entityId
}
}
}
}
`);
export const Credentials = z.object({
email: z.string().email(),
password: z.string().min(1),
});
const config = {
session: {
strategy: 'jwt',
},
pages: {
signIn: '/login',
},
callbacks: {
jwt: ({ token, user }) => {
// user can actually be undefined
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (user?.id) {
token.id = user.id;
}
return token;
},
session({ session, token }) {
if (token.id) {
session.user.id = token.id;
}
return session;
},
},
events: {
async signIn({ user }) {
const cookieCartId = cookies().get('cartId')?.value;
if (cookieCartId && user.id) {
try {
await client.fetch({
document: AssignCartToCustomerMutation,
variables: {
assignCartToCustomerInput: {
cartEntityId: cookieCartId,
},
},
customerId: user.id,
fetchOptions: {
cache: 'no-store',
},
});
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
}
},
async signOut(message) {
const cookieCartId = cookies().get('cartId')?.value;
const customerId = 'token' in message ? message.token?.id : null;
if (customerId && cookieCartId) {
try {
await client.fetch({
document: UnassignCartFromCustomerMutation,
variables: {
unassignCartFromCustomerInput: {
cartEntityId: cookieCartId,
},
},
customerId,
fetchOptions: {
cache: 'no-store',
},
});
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
}
},
},
providers: [
CredentialsProvider({
credentials: {
email: { label: 'Email', type: 'email' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
const { email, password } = Credentials.parse(credentials);
const response = await client.fetch({
document: LoginMutation,
variables: { email, password },
fetchOptions: {
cache: 'no-store',
},
});
const result = response.data.login;
if (!result.customer) {
return null;
}
return {
id: result.customer.entityId.toString(),
name: `${result.customer.firstName} ${result.customer.lastName}`,
email: result.customer.email,
};
},
}),
],
} satisfies NextAuthConfig;
const { handlers, auth, signIn, signOut } = NextAuth(config);
const getSessionCustomerId = async () => {
try {
const session = await auth();
return session?.user.id;
} catch {
// No empty
}
};
export { handlers, auth, signIn, signOut, getSessionCustomerId };
declare module 'next-auth' {
interface Session {
user: {
id: string;
} & DefaultSession['user'];
}
interface User {
id?: string;
name?: string | null;
email?: string | null;
}
}
declare module 'next-auth/jwt' {
interface JWT {
id?: string;
}
}