-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.multipass.tsx
292 lines (257 loc) · 7.99 KB
/
login.multipass.tsx
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import {
json,
redirect,
type ActionArgs,
type LoaderArgs,
} from '@shopify/remix-oxygen';
import jwtDecode from 'jwt-decode';
import {Multipassify} from '~/lib/multipass/multipassify.server';
import type {
CustomerInfoType,
GoogleJwtCredentialsType,
MultipassRequestBody,
NotLoggedInResponseType,
} from '~/lib/multipass/types';
/*
Redirect document GET requests to the login page (housekeeping)
*/
export async function loader({params, context}: LoaderArgs) {
const customerAccessToken = await context.session.get('customerAccessToken');
if (customerAccessToken) {
return redirect(params.lang ? `${params.lang}/account` : '/account');
}
return redirect(params.lang ? `${params.lang}/account` : '/account/login');
}
/*
Generates a multipass token for a given customer and return_to url.
Handles POST `/account/login/multipass` requests.
expects body: { return_to?: string, customer }
*/
export async function action({request, context}: ActionArgs) {
const {session, storefront, env} = context;
const origin = request.headers.get('Origin') || '';
const isOptionsReq = request.method === 'OPTIONS';
const isPostReq = request.method === 'POST';
let customerAccessToken;
let customer: CustomerInfoType | undefined;
try {
// only POST and OPTIONS allowed
if (!isOptionsReq && !isPostReq) {
return handleMethodNotAllowed();
}
// handle OPTIONS preflight requests
if (isOptionsReq) {
return handleOptionsPreflight(origin);
}
// POST requests handler
// Get the request body
const body: MultipassRequestBody = await request.json();
const token = body?.token;
const provider = body?.provider;
if (!session) {
return NotLoggedInResponse({
url: body?.return_to ?? null,
error: 'MISSING_SESSION',
});
}
// try to grab the customerAccessToken from the session if available
customerAccessToken = await session.get('customerAccessToken');
// attempmt to get the customer info from a thrid party token e.g google signin
if (token) {
if (provider === 'google') {
const account: GoogleJwtCredentialsType = jwtDecode(token);
// google derived jwt customer info
customer = {
first_name: account.given_name,
last_name: account.family_name,
email: account.email,
multipass_identifier: account.sub,
return_to: `/account`,
};
} else {
// provider not supported
return NotLoggedInResponse({
url: body?.return_to ?? null,
error: 'PROVIDER_NOT_SUPPORTED',
});
}
} else if (customerAccessToken) {
// Have a customerAccessToken, get the customer so we can find their email.
const response: {customer: CustomerInfoType} = await storefront.query(
CUSTOMER_INFO_QUERY,
{
variables: {
customerAccessToken,
},
},
);
customer = response?.customer ?? null;
} else {
return handleLoggedOutResponse({
return_to: body?.return_to ?? null,
checkoutDomain: env.PRIVATE_SHOPIFY_CHECKOUT_DOMAIN,
});
}
// Check if customer has the required fields to create a multipass token
if (!customer?.email) {
return NotLoggedInResponse({
url: body?.return_to ?? null,
error: 'MISSING_EMAIL',
});
}
if (!customer?.return_to && !body?.return_to) {
return NotLoggedInResponse({
url: body?.return_to ?? null,
error: 'MISSING_RETURN_TO_URL',
});
}
try {
// generate a multipass url and token
const multipassify = new Multipassify(
env.PRIVATE_SHOPIFY_STORE_MULTIPASS_SECRET,
);
const customerInfo: CustomerInfoType = {
...customer,
created_at: new Date().toISOString(),
return_to: customer?.return_to || body?.return_to || '',
};
// Generating a token for customer
const data = multipassify.generate(
customerInfo,
env.PUBLIC_STORE_DOMAIN,
request,
);
if (!data?.url) {
return NotLoggedInResponse({
url: body?.return_to ?? null,
error: 'FAILED_GENERATING_MULTIPASS',
});
}
// success, return token, url
return json(
{data: {...data, error: null}},
{
status: 200,
headers: getCorsHeaders(origin),
},
);
} catch (error) {
let message = 'unknown error';
if (error instanceof Error) {
message = error.message;
} else {
message = JSON.stringify(error);
}
return NotLoggedInResponse({
url: body?.return_to ?? null,
error: message,
});
}
} catch (error) {
let message = 'unknown error';
if (error instanceof Error) {
message = error.message;
// eslint-disable-next-line no-console
console.log('Multipass error:', error.message);
} else {
message = JSON.stringify(error);
}
return NotLoggedInResponse({
url: null,
error: message,
});
}
}
function handleMethodNotAllowed() {
return json(
{
data: null,
error: 'Method not allowed.',
},
{
status: 405,
headers: {Allow: 'POST, OPTIONS'},
},
);
}
function handleOptionsPreflight(origin: string) {
return json(null, {
status: 204,
headers: getCorsHeaders(origin),
});
}
// Force log out a user in the checkout, if they logged out in the site.
// This fixes the edge-case where a user logs in (app),
// goes to checkout (logged in), then goes back to the app,
// logs out via the app and finally goes back to the checkout
// and the user is still logged in the checkout.
async function handleLoggedOutResponse(options: {
return_to: string | null;
checkoutDomain: string | undefined;
}) {
const {return_to, checkoutDomain} = options;
const isCheckoutReq = /\/[\w-]{32}$/g.test(return_to || '');
if (!return_to || !isCheckoutReq) {
return NotLoggedInResponse({
url: null,
error: 'NOT_AUTHORIZED',
});
}
// Force logging off the user in the checkout
const encodedCheckoutUrl = encodeURIComponent(return_to);
// For example, checkoutDomain `checkout.hydrogen.shop` or `shop.example.com` or `{shop}.myshopify.com`.
const logOutUrl = `https://${checkoutDomain}/account/logout?return_url=${encodedCheckoutUrl}&step=contact_information`;
return json({data: {url: logOutUrl}, error: null});
}
/*
Helper response when errors occur.
*/
function NotLoggedInResponse(options: NotLoggedInResponseType) {
interface ErrorsType {
[key: string]: string;
}
const ERRORS: ErrorsType = {
MISSING_SESSION: 'No session found.',
MISSING_EMAIL: 'Required customer `email` was not provided.',
MISSING_RETURN_TO_URL:
'Required customer `return_to` URL was not provided.',
FAILED_GENERATING_MULTIPASS: 'Could not generate a multipass url.',
'Invalid Secret': 'Invalid Secret',
PROVIDER_NOT_SUPPORTED: 'Provider not supported.',
NOT_AUTHORIZED: 'Not authorized.',
};
const {url, error: errorKey} = options;
let error;
if (!errorKey) {
error = 'UNKNOWN_ERROR';
} else {
error = ERRORS[errorKey] ?? 'UNKNOWN_ERROR';
}
console.error('Multipass not logged in error:', error);
// Always return the original URL.
return json({data: {url}, error});
}
function getCorsHeaders(origin: string): {[key: string]: string} {
// Only requests from these origins will pass pre-flight checks
const allowedOrigin = [
origin,
// Add other domains that you'd like to allow to multipass from
// 'https://example.com',
].find((allowedHost) => origin.includes(allowedHost));
return {
'Access-Control-Allow-Origin': `${allowedOrigin}`,
'Access-Control-Allow-Headers':
'Origin, X-Requested-With, Content-Type, Accept',
};
}
const CUSTOMER_INFO_QUERY = `#graphql
query CustomerInfo($customerAccessToken: String!) {
customer(customerAccessToken: $customerAccessToken) {
firstName
lastName
phone
email
acceptsMarketing
}
}
`;