-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathedge.ts
67 lines (54 loc) · 2.46 KB
/
edge.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
import { NextMiddleware, NextRequest, NextResponse } from 'next/server';
import { StatelessSession } from './auth0-session/session/stateless-session';
import { StatefulSession } from './auth0-session/session/stateful-session';
import MiddlewareCookies from './utils/middleware-cookies';
import Session from './session/session';
import SessionCache from './session/cache';
import {
WithMiddlewareAuthRequired,
default as withMiddlewareAuthRequiredFactory
} from './helpers/with-middleware-auth-required';
import { getConfig, ConfigParameters } from './config';
import { setIsUsingNamedExports, setIsUsingOwnInstance } from './utils/instance-check';
export type Auth0Edge = { withMiddlewareAuthRequired: WithMiddlewareAuthRequired; getSession: GetSession };
export type GetSession = (req: NextRequest, res: NextResponse) => Promise<Session | null | undefined>;
export type InitAuth0 = (params?: ConfigParameters) => Auth0Edge;
export { WithMiddlewareAuthRequired };
let instance: Auth0Edge;
const genId = () => {
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
return Array.from(bytes)
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
};
function getInstance(params?: ConfigParameters): Auth0Edge {
setIsUsingNamedExports();
if (instance) {
return instance;
}
instance = _initAuth0(params);
return instance;
}
export const initAuth0: InitAuth0 = (params?) => {
setIsUsingOwnInstance();
return _initAuth0(params);
};
const _initAuth0: InitAuth0 = (params?) => {
const { baseConfig, nextConfig } = getConfig({ ...params, session: { genId, ...params?.session } });
// Init base layer (with base config)
const sessionStore = baseConfig.session.store
? new StatefulSession<NextRequest, NextResponse, Session>(baseConfig, MiddlewareCookies)
: new StatelessSession<NextRequest, NextResponse, Session>(baseConfig, MiddlewareCookies);
const sessionCache = new SessionCache(baseConfig, sessionStore);
// Init Next layer (with next config)
const getSession: GetSession = (req, res) => sessionCache.get(req, res);
const withMiddlewareAuthRequired = withMiddlewareAuthRequiredFactory(nextConfig.routes, () => sessionCache);
return {
getSession,
withMiddlewareAuthRequired
};
};
export const getSession: GetSession = (...args) => getInstance().getSession(...args);
export const withMiddlewareAuthRequired: WithMiddlewareAuthRequired = (middleware?: NextMiddleware) =>
getInstance().withMiddlewareAuthRequired(middleware);