forked from vendure-ecommerce/storefront-remix-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-netlify.js
47 lines (39 loc) · 1.33 KB
/
server-netlify.js
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
import { createRequestHandler } from '@remix-run/netlify';
import * as build from '@remix-run/dev/server-build';
/*
* Returns a context object with at most 3 keys:
* - `netlifyGraphToken`: raw authentication token to use with Netlify Graph
* - `clientNetlifyGraphAccessToken`: For use with JWTs generated by
* `netlify-graph-auth`.
* - `netlifyGraphSignature`: a signature for subscription events. Will be
* present if a secret is set.
*/
function getLoadContext(event, context) {
let rawAuthorizationString;
let netlifyGraphToken;
if (event.authlifyToken != null) {
netlifyGraphToken = event.authlifyToken;
}
let authHeader = event.headers['authorization'];
let graphSignatureHeader = event.headers['x-netlify-graph-signature'];
if (authHeader != null && /Bearer /gi.test(authHeader)) {
rawAuthorizationString = authHeader.split(' ')[1];
}
let loadContext = {
clientNetlifyGraphAccessToken: rawAuthorizationString,
netlifyGraphToken: netlifyGraphToken,
netlifyGraphSignature: graphSignatureHeader,
};
// Remove keys with undefined values
Object.keys(loadContext).forEach((key) => {
if (loadContext[key] == null) {
delete loadContext[key];
}
});
return loadContext;
}
export const handler = createRequestHandler({
build,
getLoadContext,
mode: process.env.NODE_ENV,
});