@exwave-nexus/auth-react 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +96 -2
- package/dist/gates.d.ts +9 -0
- package/dist/hooks.d.ts +93 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +333 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +76 -0
- package/package.json +27 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Exwave
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,97 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @exwave-nexus/auth-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Exwave Auth for React: a provider that holds the session, and headless hooks
|
|
4
|
+
for sign-in, sign-up, email links, password reset and sign-out, for your own
|
|
5
|
+
markup. Built on [`@exwave-nexus/auth`](https://www.npmjs.com/package/@exwave-nexus/auth).
|
|
6
|
+
|
|
7
|
+
On Next.js, use [`@exwave-nexus/auth-nextjs`](https://www.npmjs.com/package/@exwave-nexus/auth-nextjs)
|
|
8
|
+
too: it wires this provider to the App Router and the session cookie.
|
|
9
|
+
|
|
10
|
+
Full guide: <https://www.exwave.net/docs/auth/react/>
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @exwave-nexus/auth @exwave-nexus/auth-react
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Starting from scratch? `npm create exwave@latest` makes a Vite or Next.js app
|
|
17
|
+
with every screen already built.
|
|
18
|
+
|
|
19
|
+
## The provider
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { ExwaveProvider } from "@exwave-nexus/auth-react";
|
|
23
|
+
|
|
24
|
+
<ExwaveProvider
|
|
25
|
+
publishableKey={import.meta.env.VITE_EXWAVE_PUBLISHABLE_KEY}
|
|
26
|
+
navigate={(to) => router.navigate(to)} // your router
|
|
27
|
+
>
|
|
28
|
+
<App />
|
|
29
|
+
</ExwaveProvider>;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The browser then talks to Exwave Auth directly. To go through your server's
|
|
33
|
+
routes instead (the session in an httpOnly cookie), pass `api="/api/exwave"`
|
|
34
|
+
and the server's `initialState`, or your own client with `client`.
|
|
35
|
+
|
|
36
|
+
| Prop | Default | |
|
|
37
|
+
| --- | --- | --- |
|
|
38
|
+
| `navigate` | A full page load | How hooks and components move between pages |
|
|
39
|
+
| `paths` | `/sign-in`, `/sign-up`, `/verify`, `/forgot-password`, `/reset-password`, and `/` after signing in and out | Where your auth pages are |
|
|
40
|
+
| `messages` | English | Overrides of `defaultMessages` from `@exwave-nexus/auth/messages` |
|
|
41
|
+
| `onAuthChange` | | Called on sign-in and sign-out from this page or another tab (never for what the server reported) |
|
|
42
|
+
|
|
43
|
+
## Hooks
|
|
44
|
+
|
|
45
|
+
Each action has `run()`, which never throws, plus `pending`, `error` and
|
|
46
|
+
`message` (the error in words).
|
|
47
|
+
|
|
48
|
+
| Hook | For |
|
|
49
|
+
| --- | --- |
|
|
50
|
+
| `useExwave()` | `user`, `isSignedIn`, `isLoaded`, `signOutReason`, `navigate`, `paths`, `auth` |
|
|
51
|
+
| `useSignIn()` | Sign-in, and `unverifiedEmail` to offer a new link |
|
|
52
|
+
| `useResendVerification(email)` | Sends the verification link again |
|
|
53
|
+
| `useSignUp()` | Sign-up, then `step: "check_email"` with `resend` |
|
|
54
|
+
| `useEmailLink()` | The page an email link opens: verifies, or hands back the reset code |
|
|
55
|
+
| `usePasswordReset()` | `request` a link, then `confirm` the new password |
|
|
56
|
+
| `useSignOut()` | Sign out, here or `{ everywhere: true }` |
|
|
57
|
+
| `usePasswordCheck(password, policy)` | The platform's password rules, ticked off as the user types |
|
|
58
|
+
| `usePlatformConfig()` | The platform's settings, `null` while they load |
|
|
59
|
+
| `<SignedIn>`, `<SignedOut>` | Render once the session is known |
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
import { useSignIn } from "@exwave-nexus/auth-react";
|
|
63
|
+
|
|
64
|
+
function SignIn() {
|
|
65
|
+
const signIn = useSignIn();
|
|
66
|
+
return (
|
|
67
|
+
<form
|
|
68
|
+
onSubmit={(event) => {
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
const form = new FormData(event.currentTarget);
|
|
71
|
+
signIn.run({
|
|
72
|
+
login: String(form.get("login")),
|
|
73
|
+
password: String(form.get("password")),
|
|
74
|
+
});
|
|
75
|
+
}}
|
|
76
|
+
>
|
|
77
|
+
<input name="login" autoComplete="username" />
|
|
78
|
+
<input name="password" type="password" autoComplete="current-password" />
|
|
79
|
+
{signIn.message && <p role="alert">{signIn.message}</p>}
|
|
80
|
+
<button disabled={signIn.pending}>Sign in</button>
|
|
81
|
+
</form>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Ready-made screens
|
|
87
|
+
|
|
88
|
+
Sign-in, sign-up, verification and password reset screens built on these
|
|
89
|
+
hooks come two ways: plain React and CSS in the apps `npm create exwave`
|
|
90
|
+
makes, or shadcn/ui components:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
npx shadcn@latest add https://www.exwave.net/r/auth-react.json
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
React is a peer dependency (19 or later). The package adds about 8 KB
|
|
97
|
+
min+gz, the core included.
|
package/dist/gates.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
/** Renders its children once the session is known and someone is signed in. */
|
|
3
|
+
export declare function SignedIn({ children }: {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
}): ReactNode;
|
|
6
|
+
/** Renders its children once the session is known and nobody is signed in. */
|
|
7
|
+
export declare function SignedOut({ children }: {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}): ReactNode;
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { CodeInput, PasswordPolicy, PlatformConfig, Session, SignInInput, SignUpInput, SignUpResult } from "@exwave-nexus/auth";
|
|
2
|
+
import { type PasswordRule } from "@exwave-nexus/auth";
|
|
3
|
+
/** One call's state. `run` never throws: read `error` or `message` instead. */
|
|
4
|
+
export interface Action<A extends unknown[], R> {
|
|
5
|
+
run(...args: A): Promise<R | undefined>;
|
|
6
|
+
pending: boolean;
|
|
7
|
+
error: unknown;
|
|
8
|
+
/** What to show for `error`, from `@exwave-nexus/auth/messages` and your overrides. */
|
|
9
|
+
message: string | null;
|
|
10
|
+
reset(): void;
|
|
11
|
+
}
|
|
12
|
+
/** Wraps any call in an `Action`. The hooks below are built on it. */
|
|
13
|
+
export declare function useAction<A extends unknown[], R>(call: (...args: A) => Promise<R>, options?: {
|
|
14
|
+
policy?: PasswordPolicy | null;
|
|
15
|
+
}): Action<A, R>;
|
|
16
|
+
/** The platform's config (sign-up, password policy…), or `null` while it loads. */
|
|
17
|
+
export declare function usePlatformConfig(): PlatformConfig | null;
|
|
18
|
+
export declare function usePasswordPolicy(): PasswordPolicy | null;
|
|
19
|
+
export interface PasswordCheck {
|
|
20
|
+
/** Every rule of the policy, ticked off as the password meets it. */
|
|
21
|
+
rules: {
|
|
22
|
+
rule: PasswordRule;
|
|
23
|
+
ok: boolean;
|
|
24
|
+
label: string;
|
|
25
|
+
}[];
|
|
26
|
+
failed: PasswordRule[];
|
|
27
|
+
valid: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** Checks a password against the policy as it is typed, for a rules list. */
|
|
30
|
+
export declare function usePasswordCheck(password: string, policy: PasswordPolicy | null): PasswordCheck;
|
|
31
|
+
export declare function useSignIn(): Action<[SignInInput], Session> & {
|
|
32
|
+
/** Set when the email isn't verified yet, to offer a new link. */
|
|
33
|
+
unverifiedEmail: string | null;
|
|
34
|
+
};
|
|
35
|
+
export declare function useResendVerification(email: string | null): Action<[], void> & {
|
|
36
|
+
sent: boolean;
|
|
37
|
+
};
|
|
38
|
+
export declare function useSignUp(): {
|
|
39
|
+
/** `"check_email"` once the verification link is on its way. */
|
|
40
|
+
step: "check_email" | "form";
|
|
41
|
+
email: string | null;
|
|
42
|
+
policy: PasswordPolicy | null;
|
|
43
|
+
signUp: Action<[input: Omit<SignUpInput, "redirectTo">], SignUpResult>;
|
|
44
|
+
resend: Action<[], void> & {
|
|
45
|
+
sent: boolean;
|
|
46
|
+
};
|
|
47
|
+
/** Back to the form, to fix the email address. */
|
|
48
|
+
back: () => void;
|
|
49
|
+
};
|
|
50
|
+
export declare function usePasswordReset(): {
|
|
51
|
+
request: {
|
|
52
|
+
sent: boolean;
|
|
53
|
+
run(email: string): Promise<void | undefined>;
|
|
54
|
+
pending: boolean;
|
|
55
|
+
error: unknown;
|
|
56
|
+
/** What to show for `error`, from `@exwave-nexus/auth/messages` and your overrides. */
|
|
57
|
+
message: string | null;
|
|
58
|
+
reset(): void;
|
|
59
|
+
};
|
|
60
|
+
confirm: Action<[link: CodeInput, password: string], Session>;
|
|
61
|
+
policy: PasswordPolicy | null;
|
|
62
|
+
};
|
|
63
|
+
export type EmailLinkStatus = {
|
|
64
|
+
status: "loading";
|
|
65
|
+
} | {
|
|
66
|
+
status: "none";
|
|
67
|
+
} | {
|
|
68
|
+
status: "verified";
|
|
69
|
+
session: Session;
|
|
70
|
+
} | {
|
|
71
|
+
status: "reset";
|
|
72
|
+
userId: string;
|
|
73
|
+
code: string;
|
|
74
|
+
} | {
|
|
75
|
+
status: "error";
|
|
76
|
+
error: unknown;
|
|
77
|
+
message: string;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Handles the email link this page was opened with: verifies the email (and
|
|
81
|
+
* signs in) or hands back the password reset code. Runs in an effect, so link
|
|
82
|
+
* scanners that don't run JavaScript can't use the code up, and only once
|
|
83
|
+
* under React Strict Mode. Removes the code from the address bar.
|
|
84
|
+
*/
|
|
85
|
+
export declare function useEmailLink(options?: {
|
|
86
|
+
url?: string;
|
|
87
|
+
clean?: boolean;
|
|
88
|
+
}): EmailLinkStatus;
|
|
89
|
+
export declare function useSignOut(): Action<[
|
|
90
|
+
options?: {
|
|
91
|
+
everywhere?: boolean;
|
|
92
|
+
}
|
|
93
|
+
], void>;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
// src/provider.tsx
|
|
3
|
+
import {
|
|
4
|
+
createExwaveAuth,
|
|
5
|
+
createExwaveRouteClient
|
|
6
|
+
} from "@exwave-nexus/auth";
|
|
7
|
+
import {
|
|
8
|
+
createContext,
|
|
9
|
+
useContext,
|
|
10
|
+
useEffect,
|
|
11
|
+
useMemo,
|
|
12
|
+
useRef,
|
|
13
|
+
useState
|
|
14
|
+
} from "react";
|
|
15
|
+
import { jsx } from "react/jsx-runtime";
|
|
16
|
+
var DEFAULT_PATHS = {
|
|
17
|
+
signIn: "/sign-in",
|
|
18
|
+
signUp: "/sign-up",
|
|
19
|
+
forgotPassword: "/forgot-password",
|
|
20
|
+
resetPassword: "/reset-password",
|
|
21
|
+
verifyEmail: "/verify",
|
|
22
|
+
afterSignIn: "/",
|
|
23
|
+
afterSignOut: "/"
|
|
24
|
+
};
|
|
25
|
+
var ExwaveContext = createContext(null);
|
|
26
|
+
var defaultNavigate = (to, options) => {
|
|
27
|
+
if (options?.replace)
|
|
28
|
+
location.replace(to);
|
|
29
|
+
else
|
|
30
|
+
location.assign(to);
|
|
31
|
+
};
|
|
32
|
+
function createClient(props) {
|
|
33
|
+
if (props.client)
|
|
34
|
+
return props.client;
|
|
35
|
+
if (props.api !== undefined)
|
|
36
|
+
return createExwaveRouteClient({
|
|
37
|
+
api: props.api,
|
|
38
|
+
initialState: props.initialState
|
|
39
|
+
});
|
|
40
|
+
return createExwaveAuth(props);
|
|
41
|
+
}
|
|
42
|
+
function ExwaveProvider(props) {
|
|
43
|
+
const { children, messages, paths, navigate, onAuthChange } = props;
|
|
44
|
+
const initialState = "initialState" in props ? props.initialState : undefined;
|
|
45
|
+
const [auth] = useState(() => createClient(props));
|
|
46
|
+
const [state, setState] = useState(() => ({
|
|
47
|
+
isLoaded: initialState !== undefined,
|
|
48
|
+
session: initialState?.user ? {
|
|
49
|
+
user: initialState.user,
|
|
50
|
+
accessToken: null,
|
|
51
|
+
expiresAt: initialState.expiresAt
|
|
52
|
+
} : null,
|
|
53
|
+
event: null,
|
|
54
|
+
signOutReason: initialState?.signOutReason ?? null
|
|
55
|
+
}));
|
|
56
|
+
const latest = useRef({ navigate, onAuthChange });
|
|
57
|
+
latest.current = { navigate, onAuthChange };
|
|
58
|
+
useEffect(() => auth.onAuthStateChange((event, session, info) => {
|
|
59
|
+
setState({
|
|
60
|
+
isLoaded: true,
|
|
61
|
+
session,
|
|
62
|
+
event,
|
|
63
|
+
signOutReason: info.reason ?? null
|
|
64
|
+
});
|
|
65
|
+
if ((event === "SIGNED_IN" || event === "SIGNED_OUT") && info.source !== "server")
|
|
66
|
+
latest.current.onAuthChange?.(event, session);
|
|
67
|
+
}), [auth]);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (initialState && auth.mode === "route")
|
|
70
|
+
auth.hydrate(initialState);
|
|
71
|
+
}, [auth, initialState]);
|
|
72
|
+
const stableNavigate = useMemo(() => (to, options) => (latest.current.navigate ?? defaultNavigate)(to, options), []);
|
|
73
|
+
const mergedPaths = useMemo(() => ({ ...DEFAULT_PATHS, ...paths }), [paths]);
|
|
74
|
+
const value = useMemo(() => ({
|
|
75
|
+
auth,
|
|
76
|
+
...state,
|
|
77
|
+
isSignedIn: state.session !== null,
|
|
78
|
+
user: state.session?.user ?? null,
|
|
79
|
+
messages,
|
|
80
|
+
paths: mergedPaths,
|
|
81
|
+
navigate: stableNavigate
|
|
82
|
+
}), [auth, state, messages, mergedPaths, stableNavigate]);
|
|
83
|
+
return /* @__PURE__ */ jsx(ExwaveContext.Provider, {
|
|
84
|
+
value,
|
|
85
|
+
children
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function useExwave() {
|
|
89
|
+
const value = useContext(ExwaveContext);
|
|
90
|
+
if (!value)
|
|
91
|
+
throw new Error("useExwave() must be used inside <ExwaveProvider>: wrap your app in it (in Next.js, app/layout.tsx with @exwave-nexus/auth-nextjs/client). See https://www.exwave.net/docs/auth/get-started/");
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/gates.tsx
|
|
96
|
+
function SignedIn({ children }) {
|
|
97
|
+
const { isLoaded, isSignedIn } = useExwave();
|
|
98
|
+
return isLoaded && isSignedIn ? children : null;
|
|
99
|
+
}
|
|
100
|
+
function SignedOut({ children }) {
|
|
101
|
+
const { isLoaded, isSignedIn } = useExwave();
|
|
102
|
+
return isLoaded && !isSignedIn ? children : null;
|
|
103
|
+
}
|
|
104
|
+
// src/hooks.ts
|
|
105
|
+
import {
|
|
106
|
+
checkPassword,
|
|
107
|
+
parseEmailLink,
|
|
108
|
+
passwordRules,
|
|
109
|
+
withoutEmailLink
|
|
110
|
+
} from "@exwave-nexus/auth";
|
|
111
|
+
import { describeError, describeRule } from "@exwave-nexus/auth/messages";
|
|
112
|
+
import { useCallback, useEffect as useEffect2, useRef as useRef2, useState as useState2 } from "react";
|
|
113
|
+
function useAction(call, options = {}) {
|
|
114
|
+
const { messages } = useExwave();
|
|
115
|
+
const latest = useRef2(call);
|
|
116
|
+
latest.current = call;
|
|
117
|
+
const mounted = useRef2(true);
|
|
118
|
+
useEffect2(() => {
|
|
119
|
+
mounted.current = true;
|
|
120
|
+
return () => {
|
|
121
|
+
mounted.current = false;
|
|
122
|
+
};
|
|
123
|
+
}, []);
|
|
124
|
+
const [state, setState] = useState2({
|
|
125
|
+
pending: false,
|
|
126
|
+
error: null
|
|
127
|
+
});
|
|
128
|
+
const run = useCallback(async (...args) => {
|
|
129
|
+
setState({ pending: true, error: null });
|
|
130
|
+
try {
|
|
131
|
+
const result = await latest.current(...args);
|
|
132
|
+
if (mounted.current)
|
|
133
|
+
setState({ pending: false, error: null });
|
|
134
|
+
return result;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
if (mounted.current)
|
|
137
|
+
setState({ pending: false, error });
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}, []);
|
|
141
|
+
const reset = useCallback(() => setState({ pending: false, error: null }), []);
|
|
142
|
+
return {
|
|
143
|
+
run,
|
|
144
|
+
reset,
|
|
145
|
+
pending: state.pending,
|
|
146
|
+
error: state.error,
|
|
147
|
+
message: state.error ? describeError(state.error, { policy: options.policy, messages }) : null
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
var LOOPBACK = ["localhost", "127.0.0.1", "[::1]"];
|
|
151
|
+
var warnedOrigin;
|
|
152
|
+
function warnAboutOrigin(config) {
|
|
153
|
+
if (typeof location === "undefined")
|
|
154
|
+
return;
|
|
155
|
+
const { origin, hostname } = location;
|
|
156
|
+
if (!LOOPBACK.includes(hostname) || warnedOrigin === origin)
|
|
157
|
+
return;
|
|
158
|
+
if (config.allowedRedirectOrigins.includes(origin))
|
|
159
|
+
return;
|
|
160
|
+
warnedOrigin = origin;
|
|
161
|
+
console.warn(`@exwave-nexus/auth-react: ${origin} isn't a Site URL or Redirect URL of this platform, so email links can't land here. Add it in Nexus → Auth → Settings → Redirect URLs.`);
|
|
162
|
+
}
|
|
163
|
+
function usePlatformConfig() {
|
|
164
|
+
const { auth } = useExwave();
|
|
165
|
+
const [config, setConfig] = useState2(null);
|
|
166
|
+
useEffect2(() => {
|
|
167
|
+
let active = true;
|
|
168
|
+
auth.getConfig().then((value) => {
|
|
169
|
+
warnAboutOrigin(value);
|
|
170
|
+
if (active)
|
|
171
|
+
setConfig(value);
|
|
172
|
+
}, () => {
|
|
173
|
+
return;
|
|
174
|
+
});
|
|
175
|
+
return () => {
|
|
176
|
+
active = false;
|
|
177
|
+
};
|
|
178
|
+
}, [auth]);
|
|
179
|
+
return config;
|
|
180
|
+
}
|
|
181
|
+
function usePasswordPolicy() {
|
|
182
|
+
return usePlatformConfig()?.passwordPolicy ?? null;
|
|
183
|
+
}
|
|
184
|
+
function usePasswordCheck(password, policy) {
|
|
185
|
+
const { messages } = useExwave();
|
|
186
|
+
const failed = policy ? checkPassword(password, policy) : [];
|
|
187
|
+
const rules = policy ? passwordRules(policy).map((rule) => ({
|
|
188
|
+
rule,
|
|
189
|
+
ok: !failed.includes(rule),
|
|
190
|
+
label: describeRule(rule, { policy, messages })
|
|
191
|
+
})) : [];
|
|
192
|
+
return { rules, failed, valid: failed.length === 0 };
|
|
193
|
+
}
|
|
194
|
+
function useLinkTo() {
|
|
195
|
+
const { auth } = useExwave();
|
|
196
|
+
return useCallback((path) => auth.mode === "browser" && typeof location !== "undefined" ? new URL(path, location.origin).href : undefined, [auth]);
|
|
197
|
+
}
|
|
198
|
+
function useSignIn() {
|
|
199
|
+
const { auth } = useExwave();
|
|
200
|
+
const [unverifiedEmail, setUnverifiedEmail] = useState2(null);
|
|
201
|
+
const action = useAction(async (input) => {
|
|
202
|
+
setUnverifiedEmail(null);
|
|
203
|
+
try {
|
|
204
|
+
return await auth.signIn(input);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
const code = error.code;
|
|
207
|
+
if (code === "email_not_verified" && input.login.includes("@"))
|
|
208
|
+
setUnverifiedEmail(input.login);
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
return { ...action, unverifiedEmail };
|
|
213
|
+
}
|
|
214
|
+
function useResendVerification(email) {
|
|
215
|
+
const { auth, paths } = useExwave();
|
|
216
|
+
const linkTo = useLinkTo();
|
|
217
|
+
const [sent, setSent] = useState2(false);
|
|
218
|
+
const action = useAction(async () => {
|
|
219
|
+
if (!email)
|
|
220
|
+
return;
|
|
221
|
+
setSent(false);
|
|
222
|
+
await auth.resendVerification({
|
|
223
|
+
email,
|
|
224
|
+
redirectTo: linkTo(paths.verifyEmail)
|
|
225
|
+
});
|
|
226
|
+
setSent(true);
|
|
227
|
+
});
|
|
228
|
+
return { ...action, sent };
|
|
229
|
+
}
|
|
230
|
+
function useSignUp() {
|
|
231
|
+
const { auth, paths } = useExwave();
|
|
232
|
+
const linkTo = useLinkTo();
|
|
233
|
+
const policy = usePasswordPolicy();
|
|
234
|
+
const [email, setEmail] = useState2(null);
|
|
235
|
+
const signUp = useAction(async (input) => {
|
|
236
|
+
const result = await auth.signUp({
|
|
237
|
+
...input,
|
|
238
|
+
redirectTo: linkTo(paths.verifyEmail)
|
|
239
|
+
});
|
|
240
|
+
setEmail(input.email);
|
|
241
|
+
return result;
|
|
242
|
+
}, { policy });
|
|
243
|
+
const resend = useResendVerification(email);
|
|
244
|
+
return {
|
|
245
|
+
step: email ? "check_email" : "form",
|
|
246
|
+
email,
|
|
247
|
+
policy,
|
|
248
|
+
signUp,
|
|
249
|
+
resend,
|
|
250
|
+
back: () => {
|
|
251
|
+
setEmail(null);
|
|
252
|
+
signUp.reset();
|
|
253
|
+
resend.reset();
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function usePasswordReset() {
|
|
258
|
+
const { auth, paths } = useExwave();
|
|
259
|
+
const linkTo = useLinkTo();
|
|
260
|
+
const policy = usePasswordPolicy();
|
|
261
|
+
const [sent, setSent] = useState2(false);
|
|
262
|
+
const request = useAction(async (email) => {
|
|
263
|
+
setSent(false);
|
|
264
|
+
await auth.resetPassword({
|
|
265
|
+
email,
|
|
266
|
+
redirectTo: linkTo(paths.resetPassword)
|
|
267
|
+
});
|
|
268
|
+
setSent(true);
|
|
269
|
+
});
|
|
270
|
+
const confirm = useAction((link, password) => auth.confirmPasswordReset({ ...link, password }), { policy });
|
|
271
|
+
return { request: { ...request, sent }, confirm, policy };
|
|
272
|
+
}
|
|
273
|
+
function useEmailLink(options = {}) {
|
|
274
|
+
const { auth, messages } = useExwave();
|
|
275
|
+
const [status, setStatus] = useState2({ status: "loading" });
|
|
276
|
+
const found = useRef2(null);
|
|
277
|
+
useEffect2(() => {
|
|
278
|
+
let active = true;
|
|
279
|
+
if (!found.current) {
|
|
280
|
+
const href2 = options.url ?? location.href;
|
|
281
|
+
found.current = { href: href2, link: parseEmailLink(href2) };
|
|
282
|
+
if (found.current.link && options.clean !== false && !options.url)
|
|
283
|
+
history.replaceState(history.state, "", withoutEmailLink(href2));
|
|
284
|
+
}
|
|
285
|
+
const { href, link } = found.current;
|
|
286
|
+
if (!link)
|
|
287
|
+
setStatus({ status: "none" });
|
|
288
|
+
else if (link.type === "reset")
|
|
289
|
+
setStatus({ status: "reset", userId: link.userId, code: link.code });
|
|
290
|
+
else
|
|
291
|
+
auth.handleEmailLink(href).then((result) => {
|
|
292
|
+
if (active && result?.type === "email_verified")
|
|
293
|
+
setStatus({ status: "verified", session: result.session });
|
|
294
|
+
}, (error) => {
|
|
295
|
+
if (active)
|
|
296
|
+
setStatus({
|
|
297
|
+
status: "error",
|
|
298
|
+
error,
|
|
299
|
+
message: describeError(error, { messages })
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
return () => {
|
|
303
|
+
active = false;
|
|
304
|
+
};
|
|
305
|
+
}, [auth, messages, options.url, options.clean]);
|
|
306
|
+
return status;
|
|
307
|
+
}
|
|
308
|
+
function useSignOut() {
|
|
309
|
+
const { auth } = useExwave();
|
|
310
|
+
return useAction((options) => auth.signOut(options));
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// src/index.tsx
|
|
314
|
+
export {
|
|
315
|
+
useSignUp,
|
|
316
|
+
useSignOut,
|
|
317
|
+
useSignIn,
|
|
318
|
+
useResendVerification,
|
|
319
|
+
usePlatformConfig,
|
|
320
|
+
usePasswordReset,
|
|
321
|
+
usePasswordPolicy,
|
|
322
|
+
usePasswordCheck,
|
|
323
|
+
useExwave,
|
|
324
|
+
useEmailLink,
|
|
325
|
+
useAction,
|
|
326
|
+
SignedOut,
|
|
327
|
+
SignedIn,
|
|
328
|
+
ExwaveProvider,
|
|
329
|
+
DEFAULT_PATHS
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
//# debugId=955AF62B3ABB16BE64756E2164756E21
|
|
333
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx","../src/gates.tsx","../src/hooks.ts","../src/index.tsx"],"sourcesContent":["import type { AuthState, Session, User } from \"@exwave-nexus/auth\";\nimport {\n type AuthChangeEvent,\n createExwaveAuth,\n createExwaveRouteClient,\n type ExwaveAuth,\n type ExwaveAuthOptions,\n type ExwaveRouteClient,\n type SignOutReason,\n} from \"@exwave-nexus/auth\";\nimport type { Messages } from \"@exwave-nexus/auth/messages\";\nimport {\n createContext,\n type ReactNode,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\n\n/** Where the auth pages live, and where to go after signing in and out. */\nexport interface ExwavePaths {\n signIn: string;\n signUp: string;\n forgotPassword: string;\n /** Password reset emails land here. */\n resetPassword: string;\n /** Verification emails land here. */\n verifyEmail: string;\n afterSignIn: string;\n afterSignOut: string;\n}\n\nexport const DEFAULT_PATHS: ExwavePaths = {\n signIn: \"/sign-in\",\n signUp: \"/sign-up\",\n forgotPassword: \"/forgot-password\",\n resetPassword: \"/reset-password\",\n verifyEmail: \"/verify\",\n afterSignIn: \"/\",\n afterSignOut: \"/\",\n};\n\nexport type Navigate = (to: string, options?: { replace?: boolean }) => void;\n\n/** Either client: check `auth.mode` before using what only one of them has. */\nexport type ExwaveClient = ExwaveAuth | ExwaveRouteClient;\n\nexport interface ExwaveContextValue {\n auth: ExwaveClient;\n /** `false` until the session is known: read from storage, the server or `initialState`. */\n isLoaded: boolean;\n isSignedIn: boolean;\n session: Session | null;\n user: User | null;\n /** The last auth event, such as `TOKEN_REFRESHED`. */\n event: AuthChangeEvent | null;\n /**\n * Why the user is signed out, such as `account_locked`: set by `SIGNED_OUT`,\n * or on load when the session ended in the last 5 minutes. `null` while\n * signed in.\n */\n signOutReason: SignOutReason | null;\n /** Your overrides of `defaultMessages` (`@exwave-nexus/auth/messages`). */\n messages: Messages | undefined;\n paths: ExwavePaths;\n navigate: Navigate;\n}\n\nconst ExwaveContext = createContext<ExwaveContextValue | null>(null);\n\nexport interface ExwaveProviderSettings {\n children: ReactNode;\n messages?: Messages;\n paths?: Partial<ExwavePaths>;\n /** How hooks and components move between pages. Default: a full page load. */\n navigate?: Navigate;\n /**\n * Called on `SIGNED_IN` and `SIGNED_OUT` from this page or another tab,\n * never for changes the server reported (`initialState`), so it can\n * re-render server content without looping.\n */\n onAuthChange?: (event: AuthChangeEvent, session: Session | null) => void;\n}\n\nexport type ExwaveProviderProps = ExwaveProviderSettings &\n (\n | (ExwaveAuthOptions & { api?: undefined; client?: undefined })\n | {\n /** Where your server's auth routes are mounted, such as `/api/exwave`. */\n api: string;\n /** The server's view of the session; pass a fresh one on every server render. */\n initialState?: AuthState;\n client?: undefined;\n }\n | { client: ExwaveClient; initialState?: AuthState; api?: undefined }\n );\n\nconst defaultNavigate: Navigate = (to, options) => {\n if (options?.replace) location.replace(to);\n else location.assign(to);\n};\n\nfunction createClient(props: ExwaveProviderProps): ExwaveClient {\n if (props.client) return props.client;\n if (props.api !== undefined)\n return createExwaveRouteClient({\n api: props.api,\n initialState: props.initialState,\n });\n return createExwaveAuth(props as ExwaveAuthOptions);\n}\n\n/**\n * Keeps the session in React state for hooks and components. Give it your\n * publishable key (the browser talks to Exwave Auth), `api` (your server's\n * `/api/exwave` routes keep the session in a cookie), or a client.\n */\nexport function ExwaveProvider(props: ExwaveProviderProps) {\n const { children, messages, paths, navigate, onAuthChange } = props;\n const initialState = \"initialState\" in props ? props.initialState : undefined;\n const [auth] = useState(() => createClient(props));\n const [state, setState] = useState<{\n isLoaded: boolean;\n session: Session | null;\n event: AuthChangeEvent | null;\n signOutReason: SignOutReason | null;\n }>(() => ({\n isLoaded: initialState !== undefined,\n session: initialState?.user\n ? {\n user: initialState.user,\n accessToken: null,\n expiresAt: initialState.expiresAt,\n }\n : null,\n event: null,\n signOutReason: initialState?.signOutReason ?? null,\n }));\n\n // Callbacks change on every render; read the latest from refs.\n const latest = useRef({ navigate, onAuthChange });\n latest.current = { navigate, onAuthChange };\n\n useEffect(\n () =>\n auth.onAuthStateChange((event, session, info) => {\n setState({\n isLoaded: true,\n session,\n event,\n // INITIAL_SESSION carries it too, when a reload found the session ended.\n signOutReason: info.reason ?? null,\n });\n if (\n (event === \"SIGNED_IN\" || event === \"SIGNED_OUT\") &&\n info.source !== \"server\"\n )\n latest.current.onAuthChange?.(event, session);\n }),\n [auth],\n );\n\n // A server render brings a fresh view of the session: take it quietly.\n useEffect(() => {\n if (initialState && auth.mode === \"route\") auth.hydrate(initialState);\n }, [auth, initialState]);\n\n const stableNavigate = useMemo<Navigate>(\n () => (to, options) =>\n (latest.current.navigate ?? defaultNavigate)(to, options),\n [],\n );\n const mergedPaths = useMemo(() => ({ ...DEFAULT_PATHS, ...paths }), [paths]);\n\n const value = useMemo<ExwaveContextValue>(\n () => ({\n auth,\n ...state,\n isSignedIn: state.session !== null,\n user: state.session?.user ?? null,\n messages,\n paths: mergedPaths,\n navigate: stableNavigate,\n }),\n [auth, state, messages, mergedPaths, stableNavigate],\n );\n return (\n <ExwaveContext.Provider value={value}>{children}</ExwaveContext.Provider>\n );\n}\n\nexport function useExwave(): ExwaveContextValue {\n const value = useContext(ExwaveContext);\n if (!value)\n throw new Error(\n \"useExwave() must be used inside <ExwaveProvider>: wrap your app in it (in Next.js, app/layout.tsx with @exwave-nexus/auth-nextjs/client). See https://www.exwave.net/docs/auth/get-started/\",\n );\n return value;\n}\n","import type { ReactNode } from \"react\";\nimport { useExwave } from \"./provider\";\n\n/** Renders its children once the session is known and someone is signed in. */\nexport function SignedIn({ children }: { children: ReactNode }) {\n const { isLoaded, isSignedIn } = useExwave();\n return isLoaded && isSignedIn ? children : null;\n}\n\n/** Renders its children once the session is known and nobody is signed in. */\nexport function SignedOut({ children }: { children: ReactNode }) {\n const { isLoaded, isSignedIn } = useExwave();\n return isLoaded && !isSignedIn ? children : null;\n}\n","// Headless hooks: the state and calls behind sign-in, sign-up, email links,\n// password reset and sign-out, for your own markup. They work the same with\n// the browser client and the route client.\n\nimport type {\n CodeInput,\n PasswordPolicy,\n PlatformConfig,\n Session,\n SignInInput,\n SignUpInput,\n SignUpResult,\n} from \"@exwave-nexus/auth\";\nimport {\n checkPassword,\n type EmailLinkParams,\n type PasswordRule,\n parseEmailLink,\n passwordRules,\n withoutEmailLink,\n} from \"@exwave-nexus/auth\";\nimport { describeError, describeRule } from \"@exwave-nexus/auth/messages\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useExwave } from \"./provider\";\n\n/** One call's state. `run` never throws: read `error` or `message` instead. */\nexport interface Action<A extends unknown[], R> {\n run(...args: A): Promise<R | undefined>;\n pending: boolean;\n error: unknown;\n /** What to show for `error`, from `@exwave-nexus/auth/messages` and your overrides. */\n message: string | null;\n reset(): void;\n}\n\n/** Wraps any call in an `Action`. The hooks below are built on it. */\nexport function useAction<A extends unknown[], R>(\n call: (...args: A) => Promise<R>,\n options: { policy?: PasswordPolicy | null } = {},\n): Action<A, R> {\n const { messages } = useExwave();\n const latest = useRef(call);\n latest.current = call;\n const mounted = useRef(true);\n useEffect(() => {\n mounted.current = true;\n return () => {\n mounted.current = false;\n };\n }, []);\n const [state, setState] = useState<{ pending: boolean; error: unknown }>({\n pending: false,\n error: null,\n });\n\n const run = useCallback(async (...args: A) => {\n setState({ pending: true, error: null });\n try {\n const result = await latest.current(...args);\n if (mounted.current) setState({ pending: false, error: null });\n return result;\n } catch (error) {\n if (mounted.current) setState({ pending: false, error });\n return undefined;\n }\n }, []);\n const reset = useCallback(\n () => setState({ pending: false, error: null }),\n [],\n );\n\n return {\n run,\n reset,\n pending: state.pending,\n error: state.error,\n message: state.error\n ? describeError(state.error, { policy: options.policy, messages })\n : null,\n };\n}\n\nconst LOOPBACK = [\"localhost\", \"127.0.0.1\", \"[::1]\"];\nlet warnedOrigin: string | undefined;\n\n/**\n * Email links can only land on the Site URL or a Redirect URL. Catch the\n * usual first-run problem: developing on localhost before adding it.\n */\nfunction warnAboutOrigin(config: PlatformConfig) {\n if (typeof location === \"undefined\") return;\n const { origin, hostname } = location;\n if (!LOOPBACK.includes(hostname) || warnedOrigin === origin) return;\n if (config.allowedRedirectOrigins.includes(origin)) return;\n warnedOrigin = origin;\n console.warn(\n `@exwave-nexus/auth-react: ${origin} isn't a Site URL or Redirect URL of this platform, so email links can't land here. Add it in Nexus → Auth → Settings → Redirect URLs.`,\n );\n}\n\n/** The platform's config (sign-up, password policy…), or `null` while it loads. */\nexport function usePlatformConfig(): PlatformConfig | null {\n const { auth } = useExwave();\n const [config, setConfig] = useState<PlatformConfig | null>(null);\n useEffect(() => {\n let active = true;\n auth.getConfig().then(\n (value) => {\n warnAboutOrigin(value);\n if (active) setConfig(value);\n },\n () => undefined,\n );\n return () => {\n active = false;\n };\n }, [auth]);\n return config;\n}\n\nexport function usePasswordPolicy(): PasswordPolicy | null {\n return usePlatformConfig()?.passwordPolicy ?? null;\n}\n\nexport interface PasswordCheck {\n /** Every rule of the policy, ticked off as the password meets it. */\n rules: { rule: PasswordRule; ok: boolean; label: string }[];\n failed: PasswordRule[];\n valid: boolean;\n}\n\n/** Checks a password against the policy as it is typed, for a rules list. */\nexport function usePasswordCheck(\n password: string,\n policy: PasswordPolicy | null,\n): PasswordCheck {\n const { messages } = useExwave();\n const failed = policy ? checkPassword(password, policy) : [];\n const rules = policy\n ? passwordRules(policy).map((rule) => ({\n rule,\n ok: !failed.includes(rule),\n label: describeRule(rule, { policy, messages }),\n }))\n : [];\n return { rules, failed, valid: failed.length === 0 };\n}\n\n/** An email link's URL for this page's origin; the route client's server picks its own. */\nfunction useLinkTo() {\n const { auth } = useExwave();\n return useCallback(\n (path: string) =>\n auth.mode === \"browser\" && typeof location !== \"undefined\"\n ? new URL(path, location.origin).href\n : undefined,\n [auth],\n );\n}\n\nexport function useSignIn(): Action<[SignInInput], Session> & {\n /** Set when the email isn't verified yet, to offer a new link. */\n unverifiedEmail: string | null;\n} {\n const { auth } = useExwave();\n const [unverifiedEmail, setUnverifiedEmail] = useState<string | null>(null);\n const action = useAction(async (input: SignInInput) => {\n setUnverifiedEmail(null);\n try {\n return await auth.signIn(input);\n } catch (error) {\n const code = (error as { code?: string }).code;\n if (code === \"email_not_verified\" && input.login.includes(\"@\"))\n setUnverifiedEmail(input.login);\n throw error;\n }\n });\n return { ...action, unverifiedEmail };\n}\n\nexport function useResendVerification(\n email: string | null,\n): Action<[], void> & { sent: boolean } {\n const { auth, paths } = useExwave();\n const linkTo = useLinkTo();\n const [sent, setSent] = useState(false);\n const action = useAction(async () => {\n if (!email) return;\n setSent(false);\n await auth.resendVerification({\n email,\n redirectTo: linkTo(paths.verifyEmail),\n });\n setSent(true);\n });\n return { ...action, sent };\n}\n\nexport function useSignUp() {\n const { auth, paths } = useExwave();\n const linkTo = useLinkTo();\n const policy = usePasswordPolicy();\n const [email, setEmail] = useState<string | null>(null);\n const signUp = useAction(\n async (input: Omit<SignUpInput, \"redirectTo\">): Promise<SignUpResult> => {\n const result = await auth.signUp({\n ...input,\n redirectTo: linkTo(paths.verifyEmail),\n });\n setEmail(input.email);\n return result;\n },\n { policy },\n );\n const resend = useResendVerification(email);\n return {\n /** `\"check_email\"` once the verification link is on its way. */\n step: email ? (\"check_email\" as const) : (\"form\" as const),\n email,\n policy,\n signUp,\n resend,\n /** Back to the form, to fix the email address. */\n back: () => {\n setEmail(null);\n signUp.reset();\n resend.reset();\n },\n };\n}\n\nexport function usePasswordReset() {\n const { auth, paths } = useExwave();\n const linkTo = useLinkTo();\n const policy = usePasswordPolicy();\n const [sent, setSent] = useState(false);\n const request = useAction(async (email: string) => {\n setSent(false);\n await auth.resetPassword({\n email,\n redirectTo: linkTo(paths.resetPassword),\n });\n setSent(true);\n });\n const confirm = useAction(\n (link: CodeInput, password: string) =>\n auth.confirmPasswordReset({ ...link, password }),\n { policy },\n );\n return { request: { ...request, sent }, confirm, policy };\n}\n\nexport type EmailLinkStatus =\n | { status: \"loading\" }\n | { status: \"none\" }\n | { status: \"verified\"; session: Session }\n | { status: \"reset\"; userId: string; code: string }\n | { status: \"error\"; error: unknown; message: string };\n\n/**\n * Handles the email link this page was opened with: verifies the email (and\n * signs in) or hands back the password reset code. Runs in an effect, so link\n * scanners that don't run JavaScript can't use the code up, and only once\n * under React Strict Mode. Removes the code from the address bar.\n */\nexport function useEmailLink(\n options: { url?: string; clean?: boolean } = {},\n): EmailLinkStatus {\n const { auth, messages } = useExwave();\n const [status, setStatus] = useState<EmailLinkStatus>({ status: \"loading\" });\n // Strict Mode runs the effect twice; the second run sees a cleaned URL.\n const found = useRef<{ href: string; link: EmailLinkParams | null } | null>(\n null,\n );\n\n useEffect(() => {\n let active = true;\n if (!found.current) {\n const href = options.url ?? location.href;\n found.current = { href, link: parseEmailLink(href) };\n if (found.current.link && options.clean !== false && !options.url)\n history.replaceState(history.state, \"\", withoutEmailLink(href));\n }\n const { href, link } = found.current;\n if (!link) setStatus({ status: \"none\" });\n else if (link.type === \"reset\")\n setStatus({ status: \"reset\", userId: link.userId, code: link.code });\n else\n auth.handleEmailLink(href).then(\n (result) => {\n if (active && result?.type === \"email_verified\")\n setStatus({ status: \"verified\", session: result.session });\n },\n (error: unknown) => {\n if (active)\n setStatus({\n status: \"error\",\n error,\n message: describeError(error, { messages }),\n });\n },\n );\n return () => {\n active = false;\n };\n }, [auth, messages, options.url, options.clean]);\n\n return status;\n}\n\nexport function useSignOut(): Action<\n [options?: { everywhere?: boolean }],\n void\n> {\n const { auth } = useExwave();\n return useAction((options?: { everywhere?: boolean }) =>\n auth.signOut(options),\n );\n}\n","\"use client\";\n\nexport * from \"./gates\";\nexport * from \"./hooks\";\nexport * from \"./provider\";\n"],"mappings":";;AACA;AAAA;AAAA;AAAA;AAUA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuBO,IAAM,gBAA6B;AAAA,EACxC,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,gBAAgB;AAAA,EAChB,eAAe;AAAA,EACf,aAAa;AAAA,EACb,aAAa;AAAA,EACb,cAAc;AAChB;AA4BA,IAAM,gBAAgB,cAAyC,IAAI;AA6BnE,IAAM,kBAA4B,CAAC,IAAI,YAAY;AAAA,EACjD,IAAI,SAAS;AAAA,IAAS,SAAS,QAAQ,EAAE;AAAA,EACpC;AAAA,aAAS,OAAO,EAAE;AAAA;AAGzB,SAAS,YAAY,CAAC,OAA0C;AAAA,EAC9D,IAAI,MAAM;AAAA,IAAQ,OAAO,MAAM;AAAA,EAC/B,IAAI,MAAM,QAAQ;AAAA,IAChB,OAAO,wBAAwB;AAAA,M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,gBAAgB,IAAI,EAAE,KACzB,CAAC,WAAW;AAAA,QACV,IAAI,UAAU,QAAQ,SAAS;AAAA,UAC7B,UAAU,EAAE,QAAQ,YAAY,SAAS,OAAO,QAAQ,CAAC;AAAA,SAE7D,CAAC,UAAmB;AAAA,QAClB,IAAI;AAAA,UACF,UAAU;AAAA,YACR,QAAQ;AAAA,YACR;AAAA,YACA,SAAS,cAAc,OAAO,EAAE,SAAS,CAAC;AAAA,UAC5C,CAAC;AAAA,OAEP;AAAA,IACF,OAAO,MAAM;AAAA,MACX,SAAS;AAAA;AAAA,KAEV,CAAC,MAAM,UAAU,QAAQ,KAAK,QAAQ,KAAK,CAAC;AAAA,EAE/C,OAAO;AAAA;AAGF,SAAS,UAAU,GAGxB;AAAA,EACA,QAAQ,SAAS,UAAU;AAAA,EAC3B,OAAO,UAAU,CAAC,YAChB,KAAK,QAAQ,OAAO,CACtB;AAAA;;;AC7TF;","debugId":"955AF62B3ABB16BE64756E2164756E21","names":[]}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { AuthState, Session, User } from "@exwave-nexus/auth";
|
|
2
|
+
import { type AuthChangeEvent, type ExwaveAuth, type ExwaveAuthOptions, type ExwaveRouteClient, type SignOutReason } from "@exwave-nexus/auth";
|
|
3
|
+
import type { Messages } from "@exwave-nexus/auth/messages";
|
|
4
|
+
import { type ReactNode } from "react";
|
|
5
|
+
/** Where the auth pages live, and where to go after signing in and out. */
|
|
6
|
+
export interface ExwavePaths {
|
|
7
|
+
signIn: string;
|
|
8
|
+
signUp: string;
|
|
9
|
+
forgotPassword: string;
|
|
10
|
+
/** Password reset emails land here. */
|
|
11
|
+
resetPassword: string;
|
|
12
|
+
/** Verification emails land here. */
|
|
13
|
+
verifyEmail: string;
|
|
14
|
+
afterSignIn: string;
|
|
15
|
+
afterSignOut: string;
|
|
16
|
+
}
|
|
17
|
+
export declare const DEFAULT_PATHS: ExwavePaths;
|
|
18
|
+
export type Navigate = (to: string, options?: {
|
|
19
|
+
replace?: boolean;
|
|
20
|
+
}) => void;
|
|
21
|
+
/** Either client: check `auth.mode` before using what only one of them has. */
|
|
22
|
+
export type ExwaveClient = ExwaveAuth | ExwaveRouteClient;
|
|
23
|
+
export interface ExwaveContextValue {
|
|
24
|
+
auth: ExwaveClient;
|
|
25
|
+
/** `false` until the session is known: read from storage, the server or `initialState`. */
|
|
26
|
+
isLoaded: boolean;
|
|
27
|
+
isSignedIn: boolean;
|
|
28
|
+
session: Session | null;
|
|
29
|
+
user: User | null;
|
|
30
|
+
/** The last auth event, such as `TOKEN_REFRESHED`. */
|
|
31
|
+
event: AuthChangeEvent | null;
|
|
32
|
+
/**
|
|
33
|
+
* Why the user is signed out, such as `account_locked`: set by `SIGNED_OUT`,
|
|
34
|
+
* or on load when the session ended in the last 5 minutes. `null` while
|
|
35
|
+
* signed in.
|
|
36
|
+
*/
|
|
37
|
+
signOutReason: SignOutReason | null;
|
|
38
|
+
/** Your overrides of `defaultMessages` (`@exwave-nexus/auth/messages`). */
|
|
39
|
+
messages: Messages | undefined;
|
|
40
|
+
paths: ExwavePaths;
|
|
41
|
+
navigate: Navigate;
|
|
42
|
+
}
|
|
43
|
+
export interface ExwaveProviderSettings {
|
|
44
|
+
children: ReactNode;
|
|
45
|
+
messages?: Messages;
|
|
46
|
+
paths?: Partial<ExwavePaths>;
|
|
47
|
+
/** How hooks and components move between pages. Default: a full page load. */
|
|
48
|
+
navigate?: Navigate;
|
|
49
|
+
/**
|
|
50
|
+
* Called on `SIGNED_IN` and `SIGNED_OUT` from this page or another tab,
|
|
51
|
+
* never for changes the server reported (`initialState`), so it can
|
|
52
|
+
* re-render server content without looping.
|
|
53
|
+
*/
|
|
54
|
+
onAuthChange?: (event: AuthChangeEvent, session: Session | null) => void;
|
|
55
|
+
}
|
|
56
|
+
export type ExwaveProviderProps = ExwaveProviderSettings & ((ExwaveAuthOptions & {
|
|
57
|
+
api?: undefined;
|
|
58
|
+
client?: undefined;
|
|
59
|
+
}) | {
|
|
60
|
+
/** Where your server's auth routes are mounted, such as `/api/exwave`. */
|
|
61
|
+
api: string;
|
|
62
|
+
/** The server's view of the session; pass a fresh one on every server render. */
|
|
63
|
+
initialState?: AuthState;
|
|
64
|
+
client?: undefined;
|
|
65
|
+
} | {
|
|
66
|
+
client: ExwaveClient;
|
|
67
|
+
initialState?: AuthState;
|
|
68
|
+
api?: undefined;
|
|
69
|
+
});
|
|
70
|
+
/**
|
|
71
|
+
* Keeps the session in React state for hooks and components. Give it your
|
|
72
|
+
* publishable key (the browser talks to Exwave Auth), `api` (your server's
|
|
73
|
+
* `/api/exwave` routes keep the session in a cookie), or a client.
|
|
74
|
+
*/
|
|
75
|
+
export declare function ExwaveProvider(props: ExwaveProviderProps): import("react").JSX.Element;
|
|
76
|
+
export declare function useExwave(): ExwaveContextValue;
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exwave-nexus/auth-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Exwave Auth for React: the provider, and hooks for sign-in, sign-up, email links, password reset and sign-out.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/exwave-nexus/exwave-auth-js.git",
|
|
9
|
+
"directory": "packages/auth-react"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@exwave-nexus/auth": "0.3.0"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": ">=19.0.0"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"registry": "https://registry.npmjs.org",
|
|
27
|
+
"access": "public"
|
|
28
|
+
}
|
|
29
|
+
}
|