@scalio-cloud/ui 0.105.1 → 0.105.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/react/session.d.ts +12 -0
- package/react/session.js +53 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scalio-cloud/ui",
|
|
3
|
-
"version": "0.105.
|
|
3
|
+
"version": "0.105.2",
|
|
4
4
|
"description": "Scalio Control Room design tokens, Tailwind preset, self-hosted fonts, the shared app header, sign-in layout, sidebar and page blocks",
|
|
5
5
|
"license": "SEE LICENSE IN README.md",
|
|
6
6
|
"private": false,
|
package/react/session.d.ts
CHANGED
|
@@ -14,6 +14,10 @@ export type AuthState = {
|
|
|
14
14
|
setup: (name: string, email: string, password: string, setupCode: string) => Promise<AuthResult>;
|
|
15
15
|
logout: () => Promise<void>;
|
|
16
16
|
updateUser: (user: any) => void;
|
|
17
|
+
/** The page signed in by itself: keeps the user and tells onSignedIn(true). */
|
|
18
|
+
loginWith: (user: any) => void;
|
|
19
|
+
/** Reloads /auth/me and answers the user (throws when the call fails). */
|
|
20
|
+
refresh: () => Promise<any>;
|
|
17
21
|
};
|
|
18
22
|
|
|
19
23
|
export declare const createAuth: (opts: {
|
|
@@ -26,6 +30,14 @@ export declare const createAuth: (opts: {
|
|
|
26
30
|
passkeyCancelled?: (error: unknown) => boolean;
|
|
27
31
|
/** localStorage key of an older token, removed on start. */
|
|
28
32
|
legacyTokenKey?: string;
|
|
33
|
+
/** Awaited before /auth/me (e.g. redeem a one-time code). Errors are ignored. */
|
|
34
|
+
bootstrap?: (api: ApiClient) => void | Promise<void>;
|
|
35
|
+
/** Called when /auth/me refuses. Answer true after navigating away (hub redirect): the provider then stays loading. */
|
|
36
|
+
onNoSession?: (api: ApiClient) => boolean | void | Promise<boolean | void>;
|
|
37
|
+
/** Every change of the signed-in state. */
|
|
38
|
+
onSignedIn?: (signedIn: boolean) => void;
|
|
39
|
+
/** The window event "auth:unauthorized" while a user was signed in (e.g. show a toast). The user is signed out afterwards. */
|
|
40
|
+
onUnauthorized?: (previousUser: any) => void;
|
|
29
41
|
}) => {
|
|
30
42
|
AuthProvider: ScalioComponent<{ children?: React.ReactNode }>;
|
|
31
43
|
/** Outside the AuthProvider the context is null at run time. */
|
package/react/session.js
CHANGED
|
@@ -6,6 +6,14 @@
|
|
|
6
6
|
// { user, ready, login, loginMfa, loginPasskey, loginScalio, register, setup, logout, updateUser }:
|
|
7
7
|
// user is null while checking, false when signed out, else /api/auth/me. Every action answers { ok, error?, status? };
|
|
8
8
|
// login with an authenticator app answers { ok: false, mfaToken } and the page asks for the code.
|
|
9
|
+
// It also has loginWith(user) (the page already signed in) and refresh() (reloads /auth/me, answers the user).
|
|
10
|
+
// Optional hooks (Compass: SSO hub, toast, request guard):
|
|
11
|
+
// bootstrap(api) awaited before /auth/me (e.g. redeem a one-time code); errors are ignored.
|
|
12
|
+
// onNoSession(api) called when /auth/me refuses; answer true when it navigated away (hub redirect),
|
|
13
|
+
// the provider then stays loading (ready false, user null).
|
|
14
|
+
// onSignedIn(bool) every change of the signed-in state (start, sign-in, loginWith, logout, unauthorized).
|
|
15
|
+
// onUnauthorized(prev) the window event "auth:unauthorized" (401 of the api client): signs out locally,
|
|
16
|
+
// calls the hook with the previous user (only when there was one).
|
|
9
17
|
const React = require("react");
|
|
10
18
|
const { setUserTimezone } = require("./profile");
|
|
11
19
|
|
|
@@ -17,6 +25,9 @@ function createAuth(opts) {
|
|
|
17
25
|
function AuthProvider(props) {
|
|
18
26
|
const [user, setUser] = React.useState(null); // null = checking
|
|
19
27
|
const [ready, setReady] = React.useState(false);
|
|
28
|
+
const userRef = React.useRef(null);
|
|
29
|
+
userRef.current = user;
|
|
30
|
+
const notify = (v) => { if (opts.onSignedIn) opts.onSignedIn(v); };
|
|
20
31
|
|
|
21
32
|
React.useEffect(() => {
|
|
22
33
|
if (opts.legacyTokenKey) {
|
|
@@ -26,14 +37,42 @@ function createAuth(opts) {
|
|
|
26
37
|
/* private mode */
|
|
27
38
|
}
|
|
28
39
|
}
|
|
29
|
-
|
|
30
|
-
.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
40
|
+
(async () => {
|
|
41
|
+
if (opts.bootstrap) {
|
|
42
|
+
try { await opts.bootstrap(api); } catch (e) { /* e.g. expired code: continue signed out */ }
|
|
43
|
+
}
|
|
44
|
+
let me = null;
|
|
45
|
+
try { me = (await api.get("/auth/me")).data; } catch (e) { me = null; }
|
|
46
|
+
if (me) {
|
|
47
|
+
notify(true);
|
|
48
|
+
setUser(me);
|
|
49
|
+
} else {
|
|
50
|
+
let away = false;
|
|
51
|
+
if (opts.onNoSession) {
|
|
52
|
+
try { away = !!(await opts.onNoSession(api)); } catch (e) { away = false; }
|
|
53
|
+
}
|
|
54
|
+
if (away) return; // navigating away: keep loading
|
|
55
|
+
notify(false);
|
|
56
|
+
setUser(false);
|
|
57
|
+
}
|
|
58
|
+
setReady(true);
|
|
59
|
+
})();
|
|
60
|
+
}, []);
|
|
61
|
+
|
|
62
|
+
React.useEffect(() => {
|
|
63
|
+
if (!opts.onUnauthorized && !opts.onSignedIn) return undefined;
|
|
64
|
+
const handler = () => {
|
|
65
|
+
const prev = userRef.current;
|
|
66
|
+
if (opts.onUnauthorized && prev) opts.onUnauthorized(prev);
|
|
67
|
+
notify(false);
|
|
68
|
+
setUser(false);
|
|
69
|
+
};
|
|
70
|
+
window.addEventListener("auth:unauthorized", handler);
|
|
71
|
+
return () => window.removeEventListener("auth:unauthorized", handler);
|
|
34
72
|
}, []);
|
|
35
73
|
|
|
36
74
|
const signedIn = (data) => {
|
|
75
|
+
notify(true);
|
|
37
76
|
setUser(data.user);
|
|
38
77
|
// /auth/me adds the permissions of the role (own roles).
|
|
39
78
|
api.get("/auth/me").then((r) => setUser(r.data)).catch(() => null);
|
|
@@ -74,6 +113,13 @@ function createAuth(opts) {
|
|
|
74
113
|
attempt(async () => signedIn((await api.post("/auth/setup", { name, email, password, setup_code })).data));
|
|
75
114
|
// Profile page: new name. After a password change the backend sets the new cookie itself.
|
|
76
115
|
const updateUser = (u) => setUser(u);
|
|
116
|
+
// The page signed in by itself (answer of its own call): only keep the user.
|
|
117
|
+
const loginWith = (u) => { notify(true); setUser(u); };
|
|
118
|
+
const refresh = async () => {
|
|
119
|
+
const r = await api.get("/auth/me");
|
|
120
|
+
setUser(r.data);
|
|
121
|
+
return r.data;
|
|
122
|
+
};
|
|
77
123
|
// Signs out at the server (the cookie is removed there), then locally even if the call fails.
|
|
78
124
|
const logout = async () => {
|
|
79
125
|
try {
|
|
@@ -81,13 +127,14 @@ function createAuth(opts) {
|
|
|
81
127
|
} catch (e) {
|
|
82
128
|
/* already signed out */
|
|
83
129
|
}
|
|
130
|
+
notify(false);
|
|
84
131
|
setUser(false);
|
|
85
132
|
};
|
|
86
133
|
|
|
87
134
|
// Times everywhere follow the profile time zone; set before the children render.
|
|
88
135
|
setUserTimezone(user ? user.timezone : "");
|
|
89
136
|
|
|
90
|
-
const value = { user, ready, login, loginMfa, loginPasskey, loginScalio, register, setup, logout, updateUser };
|
|
137
|
+
const value = { user, ready, login, loginMfa, loginPasskey, loginScalio, register, setup, logout, updateUser, loginWith, refresh };
|
|
91
138
|
return React.createElement(AuthContext.Provider, { value }, props.children);
|
|
92
139
|
}
|
|
93
140
|
|