-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
43 lines (39 loc) · 1.64 KB
/
Copy pathauth.js
File metadata and controls
43 lines (39 loc) · 1.64 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
/* ============================================================
auth.js — 360 auth state module
Load this after the Supabase CDN, before any page script
that needs the session. Gives every page a single ready
promise instead of racing getSession() calls.
Usage:
await window.authReady; — resolves with session|null
window.authSession — the current session (after ready)
window.authSignOut() — sign out from anywhere
============================================================ */
(function () {
'use strict';
// authReady resolves once onAuthStateChange fires INITIAL_SESSION.
// At that point supabaseClient has the JWT attached and all
// subsequent .from() calls will pass RLS correctly.
window.authReady = new Promise(function (resolve) {
const unsub = supabaseClient.auth.onAuthStateChange(function (event, session) {
if (event === 'INITIAL_SESSION' || event === 'SIGNED_IN') {
window.authSession = session;
resolve(session);
unsub.data?.subscription?.unsubscribe();
}
});
});
// Keep authSession current on token refresh and sign-out
supabaseClient.auth.onAuthStateChange(function (event, session) {
if (event === 'TOKEN_REFRESHED') window.authSession = session;
if (event === 'SIGNED_OUT') {
window.authSession = null;
sessionStorage.removeItem('360_auth_redirect');
}
});
// Global helper — signs out and goes home
window.authSignOut = async function () {
await supabaseClient.auth.signOut();
sessionStorage.removeItem('360_auth_redirect');
window.location.replace('/');
};
})();