@lumoauth/react 1.4.1 → 1.5.0

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/dist/index.mjs CHANGED
@@ -5,6 +5,32 @@ import { LumoAuth as LumoAuth2 } from "@lumoauth/client";
5
5
 
6
6
  // src/provider.tsx
7
7
  import { createContext, useContext, useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
8
+
9
+ // src/utils/magicLink.ts
10
+ var MAGIC_LINK_CONTINUE_PARAM = "lumoauth_continue";
11
+ var MAGIC_LINK_RETURN_TO_MAX = 1024;
12
+ function defaultMagicLinkReturnTo(href) {
13
+ const page = new URL(href);
14
+ page.hash = "";
15
+ page.searchParams.set(MAGIC_LINK_CONTINUE_PARAM, "1");
16
+ if (page.href.length <= MAGIC_LINK_RETURN_TO_MAX) {
17
+ return page.href;
18
+ }
19
+ const withoutQuery = new URL(page.origin + page.pathname);
20
+ withoutQuery.searchParams.set(MAGIC_LINK_CONTINUE_PARAM, "1");
21
+ if (withoutQuery.href.length <= MAGIC_LINK_RETURN_TO_MAX) {
22
+ return withoutQuery.href;
23
+ }
24
+ return `${page.origin}/?${MAGIC_LINK_CONTINUE_PARAM}=1`;
25
+ }
26
+ function takeMagicLinkContinue(href) {
27
+ const page = new URL(href);
28
+ const found = page.searchParams.has(MAGIC_LINK_CONTINUE_PARAM);
29
+ page.searchParams.delete(MAGIC_LINK_CONTINUE_PARAM);
30
+ return { found, cleanUrl: `${page.pathname}${page.search}${page.hash}` };
31
+ }
32
+
33
+ // src/provider.tsx
8
34
  import { LumoAuthSession } from "@lumoauth/client";
9
35
  import { AuthModule } from "@lumoauth/client";
10
36
 
@@ -24,36 +50,7 @@ function injectStyles() {
24
50
  document.head.appendChild(style);
25
51
  injected = true;
26
52
  }
27
- var CSS_CONTENT = `
28
- /* \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
29
- @lumoauth/react \u2014 Self-contained component styles
30
- \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 */
31
-
32
- :root {
33
- --la-font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
34
- --la-font-size-sm: 0.8125rem;
35
- --la-font-size-base: 0.875rem;
36
- --la-font-size-lg: 1rem;
37
- --la-font-size-xl: 1.25rem;
38
- --la-font-size-2xl: 1.5rem;
39
- --la-font-weight-normal: 400;
40
- --la-font-weight-medium: 500;
41
- --la-font-weight-semibold: 600;
42
-
43
- --la-radius-sm: 6px;
44
- --la-radius-md: 8px;
45
- --la-radius-lg: 12px;
46
- --la-radius-xl: 16px;
47
- --la-radius-full: 9999px;
48
-
49
- --la-shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
50
- --la-shadow-md: 0 4px 6px -1px rgba(0,0,0,0.07), 0 2px 4px -1px rgba(0,0,0,0.04);
51
- --la-shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -2px rgba(0,0,0,0.04);
52
- --la-shadow-xl: 0 20px 25px -5px rgba(0,0,0,0.1), 0 10px 10px -5px rgba(0,0,0,0.04);
53
-
54
- --la-transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);
55
-
56
- /* Light theme (default) */
53
+ var LIGHT_THEME = `
57
54
  --la-bg: #ffffff;
58
55
  --la-bg-subtle: #f8f9fb;
59
56
  --la-bg-muted: #f1f3f5;
@@ -73,11 +70,13 @@ var CSS_CONTENT = `
73
70
  --la-link: #7c5cfc;
74
71
  --la-overlay: rgba(0,0,0,0.5);
75
72
  --la-card-shadow: var(--la-shadow-xl);
76
- }
77
-
78
- /* Dark theme */
79
- [data-lumoauth-theme="dark"],
80
- .la-dark {
73
+ --la-badge-success-bg: #dcfce7;
74
+ --la-badge-success-text: #166534;
75
+ --la-badge-warning-bg: #fef9c3;
76
+ --la-badge-warning-text: #854d0e;
77
+ --la-badge-muted-bg: var(--la-bg-muted);
78
+ `;
79
+ var DARK_THEME = `
81
80
  --la-bg: #1a1a2e;
82
81
  --la-bg-subtle: #16213e;
83
82
  --la-bg-muted: #0f3460;
@@ -97,32 +96,71 @@ var CSS_CONTENT = `
97
96
  --la-link: #9d85f7;
98
97
  --la-overlay: rgba(0,0,0,0.7);
99
98
  --la-card-shadow: 0 20px 25px -5px rgba(0,0,0,0.3), 0 10px 10px -5px rgba(0,0,0,0.2);
99
+ --la-badge-success-bg: rgba(22, 163, 74, 0.15);
100
+ --la-badge-success-text: #4ade80;
101
+ --la-badge-warning-bg: rgba(234, 179, 8, 0.15);
102
+ --la-badge-warning-text: #facc15;
103
+ --la-badge-muted-bg: rgba(255, 255, 255, 0.08);
104
+ `;
105
+ var CSS_CONTENT = `
106
+ /* \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
107
+ @lumoauth/react \u2014 Self-contained component styles
108
+ \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 */
109
+
110
+ :root {
111
+ --la-font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
112
+ --la-font-size-sm: 0.8125rem;
113
+ --la-font-size-base: 0.875rem;
114
+ --la-font-size-lg: 1rem;
115
+ --la-font-size-xl: 1.25rem;
116
+ --la-font-size-2xl: 1.5rem;
117
+ --la-font-weight-normal: 400;
118
+ --la-font-weight-medium: 500;
119
+ --la-font-weight-semibold: 600;
120
+
121
+ --la-radius-sm: 6px;
122
+ --la-radius-md: 8px;
123
+ --la-radius-lg: 12px;
124
+ --la-radius-xl: 16px;
125
+ --la-radius-full: 9999px;
126
+
127
+ --la-shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
128
+ --la-shadow-md: 0 4px 6px -1px rgba(0,0,0,0.07), 0 2px 4px -1px rgba(0,0,0,0.04);
129
+ --la-shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -2px rgba(0,0,0,0.04);
130
+ --la-shadow-xl: 0 20px 25px -5px rgba(0,0,0,0.1), 0 10px 10px -5px rgba(0,0,0,0.04);
131
+
132
+ --la-transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);
133
+ }
134
+
135
+ /*
136
+ * Colours. Three ways choose the theme, from weakest to strongest:
137
+ *
138
+ * 1. the operating system (prefers-color-scheme), when nothing else is set;
139
+ * 2. a class or attribute on an ancestor or on the component itself:
140
+ * .la-light / .la-dark, or data-lumoauth-theme="light" | "dark"
141
+ * (the appearance.theme prop sets the class on the component);
142
+ * 3. appearance.variables, which is inline style on the component.
143
+ *
144
+ * The light values are repeated under .la-light so that a component can be
145
+ * light on a page that the operating system made dark.
146
+ */
147
+ :root,
148
+ .la-light,
149
+ [data-lumoauth-theme="light"] {
150
+ ${LIGHT_THEME}
100
151
  }
101
152
 
102
153
  @media (prefers-color-scheme: dark) {
103
154
  :root:not([data-lumoauth-theme="light"]):not(.la-light) {
104
- --la-bg: #1a1a2e;
105
- --la-bg-subtle: #16213e;
106
- --la-bg-muted: #0f3460;
107
- --la-bg-hover: #1a3a6a;
108
- --la-border: #2a2a4a;
109
- --la-border-focus: #9d85f7;
110
- --la-text: #e8e8f0;
111
- --la-text-secondary: #a0a0b8;
112
- --la-text-muted: #6a6a88;
113
- --la-primary: #9d85f7;
114
- --la-primary-hover: #b39dff;
115
- --la-primary-text: #1a1a2e;
116
- --la-danger: #ff6b6b;
117
- --la-danger-bg: #2d1b1b;
118
- --la-success: #51cf66;
119
- --la-success-bg: #1b2d1b;
120
- --la-link: #9d85f7;
121
- --la-overlay: rgba(0,0,0,0.7);
122
- --la-card-shadow: 0 20px 25px -5px rgba(0,0,0,0.3), 0 10px 10px -5px rgba(0,0,0,0.2);
155
+ ${DARK_THEME}
123
156
  }
124
157
  }
125
158
 
159
+ .la-dark,
160
+ [data-lumoauth-theme="dark"] {
161
+ ${DARK_THEME}
162
+ }
163
+
126
164
  /* \u2500\u2500\u2500 Card Container \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
127
165
 
128
166
  .la-card {
@@ -612,7 +650,7 @@ var CSS_CONTENT = `
612
650
  justify-content: center;
613
651
  font-family: var(--la-font-family);
614
652
  font-weight: var(--la-font-weight-semibold);
615
- color: #fff;
653
+ color: var(--la-primary-text);
616
654
  background: var(--la-primary);
617
655
  overflow: hidden;
618
656
  flex-shrink: 0;
@@ -636,6 +674,9 @@ var CSS_CONTENT = `
636
674
 
637
675
  /* \u2500\u2500\u2500 UserProfile Card \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
638
676
 
677
+ /* --la-text-primary is an optional override that older apps set. It has no
678
+ default: the text colour is --la-text. */
679
+
639
680
  .la-profile-card {
640
681
  max-width: 420px;
641
682
  }
@@ -669,7 +710,7 @@ var CSS_CONTENT = `
669
710
  .la-profile-name {
670
711
  font-size: var(--la-font-size-lg);
671
712
  font-weight: var(--la-font-weight-semibold);
672
- color: var(--la-text-primary);
713
+ color: var(--la-text-primary, var(--la-text));
673
714
  line-height: 1.3;
674
715
  overflow: hidden;
675
716
  text-overflow: ellipsis;
@@ -703,7 +744,7 @@ var CSS_CONTENT = `
703
744
  gap: 0.625rem;
704
745
  padding: 0.5rem 0;
705
746
  font-size: var(--la-font-size-base);
706
- color: var(--la-text-primary);
747
+ color: var(--la-text-primary, var(--la-text));
707
748
  }
708
749
 
709
750
  .la-profile-row svg {
@@ -737,32 +778,17 @@ var CSS_CONTENT = `
737
778
  }
738
779
 
739
780
  .la-badge-success {
740
- background: #dcfce7;
741
- color: #166534;
781
+ background: var(--la-badge-success-bg);
782
+ color: var(--la-badge-success-text);
742
783
  }
743
784
 
744
785
  .la-badge-warning {
745
- background: #fef9c3;
746
- color: #854d0e;
786
+ background: var(--la-badge-warning-bg);
787
+ color: var(--la-badge-warning-text);
747
788
  }
748
789
 
749
790
  .la-badge-muted {
750
- background: var(--la-bg-muted);
751
- color: var(--la-text-secondary);
752
- }
753
-
754
- .la-dark .la-badge-success {
755
- background: rgba(22, 163, 74, 0.15);
756
- color: #4ade80;
757
- }
758
-
759
- .la-dark .la-badge-warning {
760
- background: rgba(234, 179, 8, 0.15);
761
- color: #facc15;
762
- }
763
-
764
- .la-dark .la-badge-muted {
765
- background: rgba(255, 255, 255, 0.08);
791
+ background: var(--la-badge-muted-bg);
766
792
  color: var(--la-text-secondary);
767
793
  }
768
794
 
@@ -801,7 +827,7 @@ var CSS_CONTENT = `
801
827
  .la-btn-outline {
802
828
  background: transparent;
803
829
  border: 1px solid var(--la-border);
804
- color: var(--la-text-primary);
830
+ color: var(--la-text-primary, var(--la-text));
805
831
  padding: 0.625rem 1rem;
806
832
  border-radius: var(--la-radius-md);
807
833
  font-size: var(--la-font-size-base);
@@ -904,6 +930,7 @@ function LumoAuthProvider({
904
930
  const [user, setUser] = useState(null);
905
931
  const tokensRef = useRef({ accessToken: null, refreshToken: null, expiresAt: null, idToken: null });
906
932
  const callbackHandledRef = useRef(false);
933
+ const magicLinkContinueRef = useRef(false);
907
934
  const callbackInflightRef = useRef(null);
908
935
  useEffect(() => {
909
936
  injectStyles();
@@ -1103,8 +1130,12 @@ function LumoAuthProvider({
1103
1130
  `Inline registration is not supported by this LumoAuth server: there is no JSON registration endpoint. Use redirectToSignUp() to send the user to the hosted registration page (${domain.replace(/\/+$/, "")}/orgs/${encodeURIComponent(orgId)}/register), or set authStrategy: "pkce".`
1104
1131
  );
1105
1132
  }, [authStrategy, domain, orgId, clientId, resolvedRedirectUri, scope, signIn]);
1106
- const sendMagicLink = useCallback(async (email, redirectUri2) => {
1107
- await authModule.requestMagicLink({ email, redirectUri: redirectUri2 });
1133
+ const sendMagicLink = useCallback(async (email, returnTo, options) => {
1134
+ const target = returnTo ?? (typeof window !== "undefined" ? defaultMagicLinkReturnTo(window.location.href) : void 0);
1135
+ const result = await authModule.requestMagicLink({ email, returnTo: target, captchaToken: options?.captchaToken });
1136
+ if (!result.sent) {
1137
+ throw new Error("The sign-in link request was not accepted.");
1138
+ }
1108
1139
  }, [authModule]);
1109
1140
  const checkEmail = useCallback(
1110
1141
  (email) => authModule.checkEmailExists(email),
@@ -1132,7 +1163,7 @@ function LumoAuthProvider({
1132
1163
  params.set("id_token_hint", idToken);
1133
1164
  }
1134
1165
  const logoutUrl = `${domain.replace(/\/+$/, "")}/orgs/${safeOrgId}/api/v1/oauth/logout?${params.toString()}`;
1135
- if (accessToken) {
1166
+ if (accessToken && !storage?.serverManaged) {
1136
1167
  authModule.revokeToken(accessToken, accessToken).catch(() => {
1137
1168
  });
1138
1169
  }
@@ -1141,11 +1172,16 @@ function LumoAuthProvider({
1141
1172
  window.location.replace(logoutUrl);
1142
1173
  await new Promise(() => {
1143
1174
  });
1144
- }, [authModule, domain, orgId, afterSignOutUrl, session, dispatch]);
1175
+ }, [authModule, domain, orgId, afterSignOutUrl, session, storage, dispatch]);
1145
1176
  useEffect(() => {
1146
1177
  let cancelled = false;
1147
1178
  async function init() {
1148
1179
  if (typeof window === "undefined") return;
1180
+ const back = takeMagicLinkContinue(window.location.href);
1181
+ if (back.found) {
1182
+ magicLinkContinueRef.current = true;
1183
+ window.history.replaceState(window.history.state, "", back.cleanUrl);
1184
+ }
1149
1185
  const params = new URLSearchParams(window.location.search);
1150
1186
  const code = params.get("code");
1151
1187
  const returnedState = params.get("state");
@@ -1168,13 +1204,19 @@ function LumoAuthProvider({
1168
1204
  }
1169
1205
  }
1170
1206
  }
1171
- init().then(() => {
1172
- if (!cancelled) void session.hydrate();
1207
+ init().then(async () => {
1208
+ if (cancelled) return;
1209
+ await session.hydrate();
1210
+ if (cancelled || !magicLinkContinueRef.current) return;
1211
+ magicLinkContinueRef.current = false;
1212
+ if (!session.getSnapshot().isSignedIn) {
1213
+ signInWithRedirect();
1214
+ }
1173
1215
  });
1174
1216
  return () => {
1175
1217
  cancelled = true;
1176
1218
  };
1177
- }, [fetchUser, handleCallback, refreshAccessToken, session]);
1219
+ }, [fetchUser, handleCallback, refreshAccessToken, session, signInWithRedirect]);
1178
1220
  useEffect(() => {
1179
1221
  let cancelled = false;
1180
1222
  if (!sessionState.isSignedIn) {
@@ -1881,11 +1923,12 @@ function UserAvatar({
1881
1923
  appearance
1882
1924
  }) {
1883
1925
  const { user, isSignedIn } = useLumoAuthContext();
1926
+ const themeClass = appearance?.theme === "dark" ? "la-dark" : appearance?.theme === "light" ? "la-light" : "";
1884
1927
  if (!isSignedIn || !user) {
1885
1928
  return /* @__PURE__ */ jsx6(
1886
1929
  "span",
1887
1930
  {
1888
- className: `la-avatar la-avatar-${shape} ${appearance?.className || ""}`,
1931
+ className: `la-avatar la-avatar-${shape} ${themeClass} ${appearance?.className || ""}`,
1889
1932
  style: {
1890
1933
  width: size,
1891
1934
  height: size,
@@ -1900,7 +1943,6 @@ function UserAvatar({
1900
1943
  );
1901
1944
  }
1902
1945
  const initials = [user.firstName, user.lastName].filter(Boolean).map((n) => n.charAt(0).toUpperCase()).join("") || user.email.charAt(0).toUpperCase();
1903
- const themeClass = appearance?.theme === "dark" ? "la-dark" : appearance?.theme === "light" ? "la-light" : "";
1904
1946
  return /* @__PURE__ */ jsx6(
1905
1947
  "span",
1906
1948
  {
@@ -1949,6 +1991,9 @@ var ICONS = {
1949
1991
  /* @__PURE__ */ jsx7("line", { x1: "21", y1: "12", x2: "9", y2: "12" })
1950
1992
  ] })
1951
1993
  };
1994
+ function hasMfaStatus(user) {
1995
+ return typeof user.claims?.mfa_enabled === "boolean";
1996
+ }
1952
1997
  function UserProfile({
1953
1998
  afterSignOutUrl,
1954
1999
  mode = "full",
@@ -2000,7 +2045,7 @@ function UserProfile({
2000
2045
  ] })
2001
2046
  ] }),
2002
2047
  mode === "full" && /* @__PURE__ */ jsxs6(Fragment2, { children: [
2003
- /* @__PURE__ */ jsxs6("div", { className: "la-profile-section", children: [
2048
+ hasMfaStatus(user) && /* @__PURE__ */ jsxs6("div", { className: "la-profile-section", children: [
2004
2049
  /* @__PURE__ */ jsx7("div", { className: "la-profile-section-title", children: "Security" }),
2005
2050
  /* @__PURE__ */ jsxs6("div", { className: "la-profile-row", children: [
2006
2051
  ICONS.shield,
@@ -2180,14 +2225,19 @@ function useMagicLink() {
2180
2225
  const [isLoading, setIsLoading] = useState7(false);
2181
2226
  const [isSent, setIsSent] = useState7(false);
2182
2227
  const [error, setError] = useState7(null);
2183
- const sendMagicLink = useCallback6(async (email, redirectUri) => {
2228
+ const [errorCode, setErrorCode] = useState7(null);
2229
+ const sendMagicLink = useCallback6(async (email, returnTo, options) => {
2184
2230
  setIsLoading(true);
2231
+ setIsSent(false);
2185
2232
  setError(null);
2233
+ setErrorCode(null);
2186
2234
  try {
2187
- await sendMagicLinkCtx(email, redirectUri);
2235
+ await sendMagicLinkCtx(email, returnTo, options);
2188
2236
  setIsSent(true);
2189
2237
  } catch (err) {
2190
2238
  setError(err instanceof Error ? err.message : "Failed to send magic link");
2239
+ const code = err?.code;
2240
+ setErrorCode(typeof code === "string" ? code : null);
2191
2241
  } finally {
2192
2242
  setIsLoading(false);
2193
2243
  }
@@ -2195,9 +2245,10 @@ function useMagicLink() {
2195
2245
  const reset = useCallback6(() => {
2196
2246
  setIsSent(false);
2197
2247
  setError(null);
2248
+ setErrorCode(null);
2198
2249
  setIsLoading(false);
2199
2250
  }, []);
2200
- return { sendMagicLink, isLoading, isSent, error, reset };
2251
+ return { sendMagicLink, isLoading, isSent, error, errorCode, reset };
2201
2252
  }
2202
2253
  function useEmailFirst() {
2203
2254
  const { checkEmail: checkEmailCtx } = useLumoAuthContext();