-
Notifications
You must be signed in to change notification settings - Fork 397
/
edge.ts
76 lines (69 loc) · 2.74 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
68
69
70
71
72
73
74
75
76
import {
Auth0Server,
ConfigParameters,
GetAccessToken,
GetSession,
HandleAuth,
HandleCallback,
HandleLogin,
HandleLogout,
HandleProfile,
TouchSession,
UpdateSession,
WithApiAuthRequired,
WithPageAuthRequired
} from './shared';
import { _initAuth } from './init';
import { setIsUsingNamedExports, setIsUsingOwnInstance } from './utils/instance-check';
import { clientGetter } from './auth0-session/client/edge-client';
import { WithMiddlewareAuthRequired } from './helpers/with-middleware-auth-required';
const genId = () => {
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
return Array.from(bytes)
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
};
let instance: Auth0Server;
/**
* Initialise your own instance of the SDK.
*
* See {@link ConfigParameters}.
*
* @category Server
*/
export type InitAuth0 = (params?: ConfigParameters) => Auth0Server;
// For using managed instance with named exports.
function getInstance(): Auth0Server {
setIsUsingNamedExports();
if (instance) {
return instance;
}
instance = _initAuth({ genId, clientGetter });
return instance;
}
// For creating own instance.
export const initAuth0: InitAuth0 = (params) => {
setIsUsingOwnInstance();
return _initAuth({ genId, clientGetter, params });
};
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 touchSession: TouchSession = (...args) => getInstance().touchSession(...args);
export const withApiAuthRequired: WithApiAuthRequired = (...args) =>
(getInstance().withApiAuthRequired as any)(...args);
export const withPageAuthRequired: WithPageAuthRequired = ((...args: Parameters<WithPageAuthRequired>) =>
getInstance().withPageAuthRequired(...args)) as WithPageAuthRequired;
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 const withMiddlewareAuthRequired: WithMiddlewareAuthRequired = (...args) =>
getInstance().withMiddlewareAuthRequired(...args);
export * from './shared';