@lumoauth/react 1.4.2 → 1.6.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
 
@@ -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),
@@ -1146,6 +1177,11 @@ function LumoAuthProvider({
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) {
@@ -2183,14 +2225,19 @@ function useMagicLink() {
2183
2225
  const [isLoading, setIsLoading] = useState7(false);
2184
2226
  const [isSent, setIsSent] = useState7(false);
2185
2227
  const [error, setError] = useState7(null);
2186
- const sendMagicLink = useCallback6(async (email, redirectUri) => {
2228
+ const [errorCode, setErrorCode] = useState7(null);
2229
+ const sendMagicLink = useCallback6(async (email, returnTo, options) => {
2187
2230
  setIsLoading(true);
2231
+ setIsSent(false);
2188
2232
  setError(null);
2233
+ setErrorCode(null);
2189
2234
  try {
2190
- await sendMagicLinkCtx(email, redirectUri);
2235
+ await sendMagicLinkCtx(email, returnTo, options);
2191
2236
  setIsSent(true);
2192
2237
  } catch (err) {
2193
2238
  setError(err instanceof Error ? err.message : "Failed to send magic link");
2239
+ const code = err?.code;
2240
+ setErrorCode(typeof code === "string" ? code : null);
2194
2241
  } finally {
2195
2242
  setIsLoading(false);
2196
2243
  }
@@ -2198,9 +2245,10 @@ function useMagicLink() {
2198
2245
  const reset = useCallback6(() => {
2199
2246
  setIsSent(false);
2200
2247
  setError(null);
2248
+ setErrorCode(null);
2201
2249
  setIsLoading(false);
2202
2250
  }, []);
2203
- return { sendMagicLink, isLoading, isSent, error, reset };
2251
+ return { sendMagicLink, isLoading, isSent, error, errorCode, reset };
2204
2252
  }
2205
2253
  function useEmailFirst() {
2206
2254
  const { checkEmail: checkEmailCtx } = useLumoAuthContext();