generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth-api.js
More file actions
41 lines (35 loc) · 996 Bytes
/
auth-api.js
File metadata and controls
41 lines (35 loc) · 996 Bytes
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
const AUTH_ORIGIN = 'https://demo-bbird-auth.aem-poc-lab.workers.dev';
const AUTH_PATHS = {
login: '/auth/login',
logout: '/auth/logout',
session: '/auth/session',
};
const AUTH_LABELS = {
login: 'Login',
logout: 'Logout',
};
function authUrl(path) {
return new URL(path, AUTH_ORIGIN).toString();
}
export function getDefaultAuthLabel(type) {
return AUTH_LABELS[type] || '';
}
export function getLoginUrl(returnTo = window.location.href) {
const target = new URL(AUTH_PATHS.login, AUTH_ORIGIN);
target.searchParams.set('returnTo', returnTo);
return target.toString();
}
export function getLogoutUrl() {
return authUrl(AUTH_PATHS.logout);
}
export async function getSessionState() {
const response = await fetch(authUrl(AUTH_PATHS.session), {
method: 'GET',
credentials: 'include',
headers: { Accept: 'application/json' },
});
if (!response.ok) {
throw new Error(`Auth session request failed: ${response.status}`);
}
return response.json();
}