Repository navigation
Expand file tree
/
Copy pathauth.js
More file actions
131 lines (117 loc) · 3.93 KB
/
Copy pathauth.js
File metadata and controls
131 lines (117 loc) · 3.93 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(function () {
const config = {
domain: 'dev-zgzp0yft7ibbh6tq.us.auth0.com',
clientId: 'vbeJVVDDrAf5PvEkSjZl6LKHMbG6fu1I'
};
let clientPromise;
function callbackUrl() {
return location.origin;
}
function getNextPath() {
const next = new URLSearchParams(location.search).get('next');
if (!next) return '/games.html';
try {
const decoded = decodeURIComponent(next);
return decoded.startsWith('/') ? decoded : '/games.html';
} catch {
return '/games.html';
}
}
async function getClient() {
if (!clientPromise) {
clientPromise = auth0.createAuth0Client({
domain: config.domain,
clientId: config.clientId,
authorizationParams: { redirect_uri: callbackUrl() },
cacheLocation: 'localstorage'
});
}
return clientPromise;
}
async function verifyActiveSession() {
const client = await getClient();
try {
await client.getTokenSilently({ cacheMode: 'off' });
return true;
} catch (error) {
return false;
}
}
async function ensureCallbackHandled() {
if (location.pathname !== '/login.html') return;
if (!(location.search.includes('code=') && location.search.includes('state='))) return;
const client = await getClient();
await client.handleRedirectCallback();
history.replaceState({}, '', '/login.html' + location.hash);
}
function redirectToLogin() {
const next = encodeURIComponent(location.pathname + location.search + location.hash);
location.replace('/login.html?next=' + next);
}
async function enforceAuth() {
const client = await getClient();
const authed = await client.isAuthenticated();
const activeSession = authed ? await verifyActiveSession() : false;
if (!authed || !activeSession) {
try {
await client.logout({ logoutParams: { returnTo: location.origin + '/login.html' }, localOnly: true });
} catch {}
redirectToLogin();
return false;
}
return true;
}
window.Auth = {
async login(isSignup) {
const client = await getClient();
const next = new URLSearchParams(location.search).get('next') || '/games.html';
await client.loginWithRedirect({
authorizationParams: {
redirect_uri: callbackUrl(),
screen_hint: isSignup ? 'signup' : undefined
},
appState: { next }
});
},
async logout() {
const client = await getClient();
await client.logout({ logoutParams: { returnTo: location.origin + '/login.html' } });
},
async isAuthenticated() {
await ensureCallbackHandled();
const client = await getClient();
return client.isAuthenticated();
},
async currentUser() {
await ensureCallbackHandled();
const client = await getClient();
if (!(await client.isAuthenticated())) return null;
return client.getUser();
},
async requireAuth() {
const authed = await this.isAuthenticated();
const activeSession = authed ? await verifyActiveSession() : false;
if (!authed || !activeSession) {
const next = encodeURIComponent(location.pathname + location.search + location.hash);
location.replace('/login.html?next=' + next);
}
},
async completeLoginRedirect() {
const params = new URLSearchParams(location.search);
if (params.get('error')) {
return { ok: false, error: params.get('error_description') || params.get('error') };
}
const hasCode = params.has('code') && params.has('state');
if (hasCode) {
const client = await getClient();
const { appState } = await client.handleRedirectCallback();
history.replaceState({}, '', '/login.html');
return { ok: true, next: appState?.next || getNextPath(), redirected: true };
}
if (await this.isAuthenticated()) {
return { ok: true, next: getNextPath() };
}
return { ok: false, unauthenticated: true };
}
};
})();