-
Notifications
You must be signed in to change notification settings - Fork 381
/
index.ts
79 lines (70 loc) · 3.11 KB
/
index.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
import crypto from 'crypto';
import {
Auth0Server as Auth0ServerShared,
ConfigParameters,
GetAccessToken,
GetSession,
HandleAuth,
HandleCallback,
HandleLogin,
HandleLogout,
HandleProfile,
SessionCache,
UpdateSession,
WithApiAuthRequired,
WithPageAuthRequired
} from './shared';
import { _initAuth } from './init';
import { setIsUsingNamedExports, setIsUsingOwnInstance } from './utils/instance-check';
import { getConfig, getLoginUrl } from './config';
import { withPageAuthRequiredFactory } from './helpers';
import { NodeClient } from './auth0-session/client/node-client';
import version from './version';
const genId = () => crypto.randomBytes(16).toString('hex');
const telemetry = { name: 'nextjs-auth0', version };
export type Auth0Server = Omit<Auth0ServerShared, 'withMiddlewareAuthRequired'>;
let instance: Auth0ServerShared & { sessionCache: SessionCache };
/**
* Initialise your own instance of the SDK.
*
* See {@link ConfigParameters}.
*
* @category Server
*/
export type InitAuth0 = (params?: ConfigParameters) => Omit<Auth0Server, 'withMiddlewareAuthRequired'>;
// For using managed instance with named exports.
function getInstance(): Auth0ServerShared & { sessionCache: SessionCache } {
setIsUsingNamedExports();
if (instance) {
return instance;
}
const { baseConfig, nextConfig } = getConfig({ session: { genId } });
const client = new NodeClient(baseConfig, telemetry);
instance = _initAuth({ baseConfig, nextConfig, client });
return instance;
}
// For creating own instance.
export const initAuth0: InitAuth0 = (params) => {
setIsUsingOwnInstance();
const { baseConfig, nextConfig } = getConfig({ ...params, session: { genId, ...params?.session } });
const client = new NodeClient(baseConfig, telemetry);
const { sessionCache, withMiddlewareAuthRequired, ...publicApi } = _initAuth({ baseConfig, nextConfig, client });
return publicApi;
};
const getSessionCache = () => getInstance().sessionCache;
export const getSession: GetSession = (...args) => getInstance().getSession(...args);
export const updateSession: UpdateSession = (...args) => getInstance().updateSession(...args);
export const getAccessToken: GetAccessToken = (...args) => getInstance().getAccessToken(...args);
export const withApiAuthRequired: WithApiAuthRequired = (...args) =>
(getInstance().withApiAuthRequired as any)(...args);
export const withPageAuthRequired: WithPageAuthRequired = withPageAuthRequiredFactory(getLoginUrl(), getSessionCache);
export const handleLogin: HandleLogin = ((...args: Parameters<HandleLogin>) =>
getInstance().handleLogin(...args)) as HandleLogin;
export const handleLogout: HandleLogout = ((...args: Parameters<HandleLogout>) =>
getInstance().handleLogout(...args)) as HandleLogout;
export const handleCallback: HandleCallback = ((...args: Parameters<HandleCallback>) =>
getInstance().handleCallback(...args)) as HandleCallback;
export const handleProfile: HandleProfile = ((...args: Parameters<HandleProfile>) =>
getInstance().handleProfile(...args)) as HandleProfile;
export const handleAuth: HandleAuth = (...args) => getInstance().handleAuth(...args);
export * from './shared';