-
Notifications
You must be signed in to change notification settings - Fork 179
/
auth.ts
156 lines (136 loc) · 3.67 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
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!, $cartEntityId: String) {
login(email: $email, password: $password, guestCartEntityId: $cartEntityId) {
customerAccessToken {
value
}
customer {
entityId
firstName
lastName
email
}
}
}
`);
const LogoutMutation = graphql(`
mutation LogoutMutation {
logout {
result
}
}
`);
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?.customerAccessToken) {
token.customerAccessToken = user.customerAccessToken;
}
return token;
},
session({ session, token }) {
if (token.customerAccessToken) {
session.customerAccessToken = token.customerAccessToken;
}
return session;
},
},
events: {
async signOut(message) {
const customerAccessToken = 'token' in message ? message.token?.customerAccessToken : null;
if (customerAccessToken) {
try {
await client.fetch({
document: LogoutMutation,
variables: {},
customerAccessToken,
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 cookieStore = await cookies();
const cartEntityId = cookieStore.get('cartId')?.value;
const response = await client.fetch({
document: LoginMutation,
variables: { email, password, cartEntityId },
fetchOptions: {
cache: 'no-store',
},
});
if (response.errors && response.errors.length > 0) {
return null;
}
const result = response.data.login;
if (!result.customer || !result.customerAccessToken) {
return null;
}
return {
name: `${result.customer.firstName} ${result.customer.lastName}`,
email: result.customer.email,
customerAccessToken: result.customerAccessToken.value,
};
},
}),
],
} satisfies NextAuthConfig;
const { handlers, auth, signIn, signOut } = NextAuth(config);
const getSessionCustomerAccessToken = async () => {
try {
const session = await auth();
return session?.customerAccessToken;
} catch {
// No empty
}
};
export { handlers, auth, signIn, signOut, getSessionCustomerAccessToken };
declare module 'next-auth' {
interface Session {
user?: DefaultSession['user'];
customerAccessToken?: string;
}
interface User {
name?: string | null;
email?: string | null;
customerAccessToken?: string;
}
}
declare module 'next-auth/jwt' {
interface JWT {
id?: string;
customerAccessToken?: string;
}
}