@supatype/react-auth 0.3.5 → 0.7.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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +11 -4
- package/.turbo/turbo-typecheck.log +2 -2
- package/LICENSE +1 -1
- package/README.md +103 -0
- package/dist/EmailLinkForm.d.ts +42 -0
- package/dist/EmailLinkForm.d.ts.map +1 -0
- package/dist/EmailLinkForm.js +14 -0
- package/dist/EmailLinkForm.js.map +1 -0
- package/dist/ForgotPasswordForm.d.ts +19 -0
- package/dist/ForgotPasswordForm.d.ts.map +1 -0
- package/dist/ForgotPasswordForm.js +33 -0
- package/dist/ForgotPasswordForm.js.map +1 -0
- package/dist/LoginForm.d.ts +4 -1
- package/dist/LoginForm.d.ts.map +1 -1
- package/dist/LoginForm.js +8 -24
- package/dist/LoginForm.js.map +1 -1
- package/dist/MagicLinkForm.d.ts +19 -0
- package/dist/MagicLinkForm.d.ts.map +1 -0
- package/dist/MagicLinkForm.js +33 -0
- package/dist/MagicLinkForm.js.map +1 -0
- package/dist/OAuthButton.d.ts +3 -2
- package/dist/OAuthButton.d.ts.map +1 -1
- package/dist/OAuthButton.js +79 -42
- package/dist/OAuthButton.js.map +1 -1
- package/dist/SignUpForm.d.ts +4 -1
- package/dist/SignUpForm.d.ts.map +1 -1
- package/dist/SignUpForm.js +13 -32
- package/dist/SignUpForm.js.map +1 -1
- package/dist/UpdatePasswordForm.d.ts +38 -0
- package/dist/UpdatePasswordForm.d.ts.map +1 -0
- package/dist/UpdatePasswordForm.js +36 -0
- package/dist/UpdatePasswordForm.js.map +1 -0
- package/dist/classNames.d.ts +36 -0
- package/dist/classNames.d.ts.map +1 -0
- package/dist/classNames.js +8 -0
- package/dist/classNames.js.map +1 -0
- package/dist/formParts.d.ts +68 -0
- package/dist/formParts.d.ts.map +1 -0
- package/dist/formParts.js +72 -0
- package/dist/formParts.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/usePasswordRecovery.d.ts +18 -0
- package/dist/usePasswordRecovery.d.ts.map +1 -0
- package/dist/usePasswordRecovery.js +27 -0
- package/dist/usePasswordRecovery.js.map +1 -0
- package/package.json +6 -4
- package/src/EmailLinkForm.tsx +62 -0
- package/src/ForgotPasswordForm.tsx +43 -0
- package/src/LoginForm.tsx +35 -62
- package/src/MagicLinkForm.tsx +43 -0
- package/src/OAuthButton.tsx +84 -60
- package/src/SignUpForm.tsx +40 -74
- package/src/UpdatePasswordForm.tsx +74 -0
- package/src/classNames.ts +41 -0
- package/src/formParts.tsx +153 -0
- package/src/index.ts +14 -0
- package/src/usePasswordRecovery.ts +32 -0
- package/tests/auth-ui.test.tsx +48 -0
- package/tests/email-link-forms.test.tsx +214 -0
- package/tests/helpers.tsx +41 -0
- package/tests/login-form.test.tsx +63 -0
- package/tests/oauth-button.test.tsx +75 -0
- package/tests/password-recovery.test.tsx +62 -0
- package/tests/props.ts +24 -0
- package/tests/signup-confirmation.test.tsx +50 -0
- package/tests/tsconfig.json +9 -0
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
package/.turbo/turbo-build.log
CHANGED
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
|
|
2
|
-
> @supatype/react-auth@0.
|
|
2
|
+
> @supatype/react-auth@0.7.0 test /home/runner/work/supatype/supatype/packages/react-auth
|
|
3
3
|
> vitest run --passWithNoTests
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
[1m[46m RUN [49m[22m [36mv4.0.18 [39m[90m/home/runner/work/supatype/supatype/packages/react-auth[39m
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
[32m✓[39m tests/oauth-button.test.tsx [2m([22m[2m4 tests[22m[2m)[22m[32m 86[2mms[22m[39m
|
|
9
|
+
[32m✓[39m tests/login-form.test.tsx [2m([22m[2m3 tests[22m[2m)[22m[32m 280[2mms[22m[39m
|
|
10
|
+
[32m✓[39m tests/email-link-forms.test.tsx [2m([22m[2m11 tests[22m[2m)[22m[33m 345[2mms[22m[39m
|
|
11
|
+
[32m✓[39m tests/password-recovery.test.tsx [2m([22m[2m2 tests[22m[2m)[22m[32m 229[2mms[22m[39m
|
|
12
|
+
[32m✓[39m tests/auth-ui.test.tsx [2m([22m[2m6 tests[22m[2m)[22m[32m 56[2mms[22m[39m
|
|
13
|
+
[32m✓[39m tests/signup-confirmation.test.tsx [2m([22m[2m1 test[22m[2m)[22m[32m 180[2mms[22m[39m
|
|
9
14
|
|
|
10
|
-
[
|
|
11
|
-
[
|
|
15
|
+
[2m Test Files [22m [1m[32m6 passed[39m[22m[90m (6)[39m
|
|
16
|
+
[2m Tests [22m [1m[32m27 passed[39m[22m[90m (27)[39m
|
|
17
|
+
[2m Start at [22m 12:11:58
|
|
18
|
+
[2m Duration [22m 6.02s[2m (transform 5.30s, setup 0ms, import 10.83s, tests 1.18s, environment 5ms)[22m
|
|
12
19
|
|
package/LICENSE
CHANGED
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# `@supatype/react-auth`
|
|
2
|
+
|
|
3
|
+
Pre-built auth UI for React on Supatype: `LoginForm`, `SignUpForm`, `ForgotPasswordForm`,
|
|
4
|
+
`UpdatePasswordForm`, `MagicLinkForm` and `OAuthButton`.
|
|
5
|
+
|
|
6
|
+
Every component must render inside a `<SupatypeProvider>` from `@supatype/react`.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @supatype/client @supatype/react @supatype/react-auth
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Styling: the forms ship no stylesheet
|
|
15
|
+
|
|
16
|
+
The forms render plain, accessible markup and apply no styles of their own.
|
|
17
|
+
There is no CSS file to import and nothing to add to a Tailwind `content` list. Rendered with no
|
|
18
|
+
classes, they look like browser defaults: each label inline beside a narrow input, and a grey button.
|
|
19
|
+
|
|
20
|
+
Style them with your app's own classes. `className` goes on the `<form>`; `classNames` puts a class on
|
|
21
|
+
each element inside it:
|
|
22
|
+
|
|
23
|
+
| Slot | Element |
|
|
24
|
+
|---|---|
|
|
25
|
+
| `title` | the `<h2>` heading |
|
|
26
|
+
| `error` | the message shown when the auth service refuses the request |
|
|
27
|
+
| `field` | the wrapper around each label and input |
|
|
28
|
+
| `label` | each `<label>` |
|
|
29
|
+
| `input` | each `<input>` |
|
|
30
|
+
| `submit` | the submit `<button>` |
|
|
31
|
+
| `message` | the line a form shows in its place when done: "check your email" from `SignUpForm`, `ForgotPasswordForm` and `MagicLinkForm`, "password updated" from `UpdatePasswordForm` |
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { LoginForm, SignUpForm, type AuthFormClassNames } from "@supatype/react-auth"
|
|
35
|
+
|
|
36
|
+
const classNames: AuthFormClassNames = {
|
|
37
|
+
title: "text-lg font-semibold",
|
|
38
|
+
field: "flex flex-col gap-1",
|
|
39
|
+
label: "text-sm font-medium",
|
|
40
|
+
input: "rounded-md border px-3 py-2",
|
|
41
|
+
submit: "rounded-md bg-emerald-700 px-4 py-2 text-white",
|
|
42
|
+
error: "text-sm text-red-700",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
<LoginForm className="flex flex-col gap-4" classNames={classNames} onSuccess={() => navigate("/")} />
|
|
46
|
+
<SignUpForm className="flex flex-col gap-4" classNames={classNames} />
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Define the classes once and spread them onto every form, so they cannot drift apart. The error
|
|
50
|
+
message is red by default and loses that colour once `classNames.error` is set.
|
|
51
|
+
|
|
52
|
+
An app that styles bare elements globally (`label`, `input`, `button`) can skip `classNames`.
|
|
53
|
+
|
|
54
|
+
`OAuthButton` is styled by default and drops its own styles when given a `className`:
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
<OAuthButton provider="github" redirectTo="/dashboard" />
|
|
58
|
+
<OAuthButton provider="google" className="btn" />
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The sign-in finishes on its own when the provider sends the browser back: the client reads the
|
|
62
|
+
session from the URL on start (`auth.detectSessionInUrl`, on by default), removes it from the address
|
|
63
|
+
bar, and `useAuth()` shows the user. `onError` is called with a clear message when the provider is
|
|
64
|
+
not enabled for the project (before any redirect), and when the server sends the browser back with
|
|
65
|
+
a refusal for the sign-in that button started. The button's text is its accessible name, so a label
|
|
66
|
+
given as `children` is what a screen reader announces.
|
|
67
|
+
|
|
68
|
+
## Password reset and magic links
|
|
69
|
+
|
|
70
|
+
Three more forms take the same `className`, `classNames`, `labels`, `onSuccess` and `onError`:
|
|
71
|
+
|
|
72
|
+
| Form | Sends | Then shows |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| `ForgotPasswordForm` | `auth.resetPasswordForEmail(email, { redirectTo })`, `POST /recover` | "Check your email for the password reset link." |
|
|
75
|
+
| `MagicLinkForm` | `auth.signInWithOtp({ email, options: { emailRedirectTo: redirectTo } })`, `POST /otp` | "Check your email for the magic link." |
|
|
76
|
+
| `UpdatePasswordForm` | `auth.updateUser({ password })`, `PUT /user` | "Your password has been updated." |
|
|
77
|
+
|
|
78
|
+
`onSuccess` receives the address for the two email forms and the updated user for
|
|
79
|
+
`UpdatePasswordForm`. A refusal (a rate limit, a weak password) is shown in the `error` slot and
|
|
80
|
+
passed to `onError`, and the form stays so the user can try again.
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
<ForgotPasswordForm className="flex flex-col gap-4" classNames={classNames} redirectTo={window.location.origin} />
|
|
84
|
+
<MagicLinkForm className="flex flex-col gap-4" classNames={classNames} redirectTo={window.location.origin} />
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`redirectTo` is where the emailed link lands. The auth server honours it when it is on the site
|
|
88
|
+
URL's host or its redirect allow list, and sends the link to the site URL otherwise.
|
|
89
|
+
|
|
90
|
+
A magic link lands signed in, like an OAuth redirect. A reset link lands signed in too, and the
|
|
91
|
+
client then emits `PASSWORD_RECOVERY`. `usePasswordRecovery()` turns that into state: show
|
|
92
|
+
`UpdatePasswordForm` instead of the app while it is active, or the user arrives inside it with the
|
|
93
|
+
password they forgot.
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
import { usePasswordRecovery, UpdatePasswordForm } from "@supatype/react-auth"
|
|
97
|
+
|
|
98
|
+
const recovery = usePasswordRecovery() // beside useAuth(), from the first render
|
|
99
|
+
if (recovery.active) return <UpdatePasswordForm onSuccess={recovery.finish} />
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
With no session, `UpdatePasswordForm` sends nothing and reports "Not authenticated" through
|
|
103
|
+
`onError`.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { SupatypeError } from "@supatype/client";
|
|
3
|
+
import type { AuthFormClassNames } from "./classNames.js";
|
|
4
|
+
import { type Filled } from "./formParts.js";
|
|
5
|
+
export interface EmailLinkFormLabels {
|
|
6
|
+
title?: string | undefined;
|
|
7
|
+
email?: string | undefined;
|
|
8
|
+
submit?: string | undefined;
|
|
9
|
+
/** Shown in place of the form once the email is on its way. */
|
|
10
|
+
successMessage?: string | undefined;
|
|
11
|
+
}
|
|
12
|
+
/** Props shared by the forms that email the user a link: `ForgotPasswordForm` and `MagicLinkForm`. */
|
|
13
|
+
export interface EmailLinkFormProps {
|
|
14
|
+
/** Where the emailed link sends the browser once the auth server has checked it. */
|
|
15
|
+
redirectTo?: string | undefined;
|
|
16
|
+
/** Called with the address once the auth service has accepted the request. */
|
|
17
|
+
onSuccess?: ((email: string) => void) | undefined;
|
|
18
|
+
/** Called with the error returned by the auth service. */
|
|
19
|
+
onError?: ((error: SupatypeError) => void) | undefined;
|
|
20
|
+
/** CSS class applied to the outermost <form> element. */
|
|
21
|
+
className?: string | undefined;
|
|
22
|
+
/** Override display labels. */
|
|
23
|
+
labels?: EmailLinkFormLabels | undefined;
|
|
24
|
+
/** Class names for the elements inside the form, so an app can style them with its own classes. */
|
|
25
|
+
classNames?: AuthFormClassNames | undefined;
|
|
26
|
+
}
|
|
27
|
+
/** What one email-link form does: its input's id, its words, and the request it sends. */
|
|
28
|
+
export interface EmailLinkConfig {
|
|
29
|
+
inputId: string;
|
|
30
|
+
labels: Filled<EmailLinkFormLabels>;
|
|
31
|
+
busyLabel: string;
|
|
32
|
+
send: (email: string) => Promise<{
|
|
33
|
+
data: unknown;
|
|
34
|
+
error: SupatypeError | null;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
/** An email field and a submit button that send one request, then "check your email". */
|
|
38
|
+
export declare function EmailLinkForm({ props, config }: {
|
|
39
|
+
props: EmailLinkFormProps;
|
|
40
|
+
config: EmailLinkConfig;
|
|
41
|
+
}): React.ReactElement;
|
|
42
|
+
//# sourceMappingURL=EmailLinkForm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EmailLinkForm.d.ts","sourceRoot":"","sources":["../src/EmailLinkForm.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAmB,MAAM,OAAO,CAAA;AACvC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AACzD,OAAO,EAAkE,KAAK,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAE5G,MAAM,WAAW,mBAAmB;IAClC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,+DAA+D;IAC/D,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACpC;AAED,sGAAsG;AACtG,MAAM,WAAW,kBAAkB;IACjC,oFAAoF;IACpF,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,8EAA8E;IAC9E,SAAS,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,SAAS,CAAA;IACjD,0DAA0D;IAC1D,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC,GAAG,SAAS,CAAA;IACtD,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC9B,+BAA+B;IAC/B,MAAM,CAAC,EAAE,mBAAmB,GAAG,SAAS,CAAA;IACxC,mGAAmG;IACnG,UAAU,CAAC,EAAE,kBAAkB,GAAG,SAAS,CAAA;CAC5C;AAED,0FAA0F;AAC1F,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAA;IACnC,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,IAAI,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;CACjF;AAED,yFAAyF;AACzF,wBAAgB,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;IAAE,KAAK,EAAE,kBAAkB,CAAC;IAAC,MAAM,EAAE,eAAe,CAAA;CAAE,GAAG,KAAK,CAAC,YAAY,CAuB3H"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { FormField, FormShell, FormMessage, useAuthSubmit, withDefaults } from "./formParts.js";
|
|
4
|
+
/** An email field and a submit button that send one request, then "check your email". */
|
|
5
|
+
export function EmailLinkForm({ props, config }) {
|
|
6
|
+
const { onSuccess, onError, className, classNames } = props;
|
|
7
|
+
const l = withDefaults(config.labels, props.labels);
|
|
8
|
+
const [email, setEmail] = useState("");
|
|
9
|
+
const request = useAuthSubmit(() => config.send(email), { onDone: () => onSuccess?.(email), onError });
|
|
10
|
+
if (request.done)
|
|
11
|
+
return _jsx(FormMessage, { text: l.successMessage, classNames: classNames });
|
|
12
|
+
return (_jsx(FormShell, { request: request, text: { title: l.title, submit: l.submit, busy: config.busyLabel }, className: className, classNames: classNames, children: _jsx(FormField, { id: config.inputId, label: l.email, classNames: classNames, type: "email", autoComplete: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value), disabled: request.loading }) }));
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=EmailLinkForm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EmailLinkForm.js","sourceRoot":"","sources":["../src/EmailLinkForm.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAGvC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,aAAa,EAAE,YAAY,EAAe,MAAM,gBAAgB,CAAA;AAkC5G,yFAAyF;AACzF,MAAM,UAAU,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,EAA0D;IACrG,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,KAAK,CAAA;IAC3D,MAAM,CAAC,GAAG,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;IACnD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAA;IACtC,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,CAAC,CAAA;IAEtG,IAAI,OAAO,CAAC,IAAI;QAAE,OAAO,KAAC,WAAW,IAAC,IAAI,EAAE,CAAC,CAAC,cAAc,EAAE,UAAU,EAAE,UAAU,GAAI,CAAA;IAExF,OAAO,CACL,KAAC,SAAS,IAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,YAC3I,KAAC,SAAS,IACR,EAAE,EAAE,MAAM,CAAC,OAAO,EAClB,KAAK,EAAE,CAAC,CAAC,KAAK,EACd,UAAU,EAAE,UAAU,EACtB,IAAI,EAAC,OAAO,EACZ,YAAY,EAAC,OAAO,EACpB,QAAQ,QACR,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EACzC,QAAQ,EAAE,OAAO,CAAC,OAAO,GACzB,GACQ,CACb,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { type EmailLinkFormLabels, type EmailLinkFormProps } from "./EmailLinkForm.js";
|
|
3
|
+
export type ForgotPasswordFormLabels = EmailLinkFormLabels;
|
|
4
|
+
export type ForgotPasswordFormProps = EmailLinkFormProps;
|
|
5
|
+
/**
|
|
6
|
+
* Asks the auth service to email a password reset link (`resetPasswordForEmail`, `POST /recover`).
|
|
7
|
+
* Must be rendered inside a `<SupatypeProvider>`.
|
|
8
|
+
*
|
|
9
|
+
* The link signs the user in and the client emits `PASSWORD_RECOVERY`; render `UpdatePasswordForm`
|
|
10
|
+
* when it does. `redirectTo` is where the link lands when it is on the site URL's host (or the auth
|
|
11
|
+
* server's redirect allow list); otherwise the link lands on the site URL.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* <ForgotPasswordForm redirectTo={`${window.location.origin}/reset`} />
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function ForgotPasswordForm(props: ForgotPasswordFormProps): React.ReactElement;
|
|
19
|
+
//# sourceMappingURL=ForgotPasswordForm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ForgotPasswordForm.d.ts","sourceRoot":"","sources":["../src/ForgotPasswordForm.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAiB,KAAK,mBAAmB,EAAE,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAGrG,MAAM,MAAM,wBAAwB,GAAG,mBAAmB,CAAA;AAC1D,MAAM,MAAM,uBAAuB,GAAG,kBAAkB,CAAA;AASxD;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,uBAAuB,GAAG,KAAK,CAAC,YAAY,CAcrF"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useSupatype } from "@supatype/react";
|
|
3
|
+
import { EmailLinkForm } from "./EmailLinkForm.js";
|
|
4
|
+
const DEFAULT_LABELS = {
|
|
5
|
+
title: "Reset your password",
|
|
6
|
+
email: "Email address",
|
|
7
|
+
submit: "Send reset password instructions",
|
|
8
|
+
successMessage: "Check your email for the password reset link.",
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Asks the auth service to email a password reset link (`resetPasswordForEmail`, `POST /recover`).
|
|
12
|
+
* Must be rendered inside a `<SupatypeProvider>`.
|
|
13
|
+
*
|
|
14
|
+
* The link signs the user in and the client emits `PASSWORD_RECOVERY`; render `UpdatePasswordForm`
|
|
15
|
+
* when it does. `redirectTo` is where the link lands when it is on the site URL's host (or the auth
|
|
16
|
+
* server's redirect allow list); otherwise the link lands on the site URL.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* <ForgotPasswordForm redirectTo={`${window.location.origin}/reset`} />
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function ForgotPasswordForm(props) {
|
|
24
|
+
const { auth } = useSupatype();
|
|
25
|
+
const { redirectTo } = props;
|
|
26
|
+
return (_jsx(EmailLinkForm, { props: props, config: {
|
|
27
|
+
inputId: "st-forgot-email",
|
|
28
|
+
labels: DEFAULT_LABELS,
|
|
29
|
+
busyLabel: "Sending reset instructions…",
|
|
30
|
+
send: (email) => auth.resetPasswordForEmail(email, { redirectTo }),
|
|
31
|
+
} }));
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=ForgotPasswordForm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ForgotPasswordForm.js","sourceRoot":"","sources":["../src/ForgotPasswordForm.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAqD,MAAM,oBAAoB,CAAA;AAMrG,MAAM,cAAc,GAAqC;IACvD,KAAK,EAAE,qBAAqB;IAC5B,KAAK,EAAE,eAAe;IACtB,MAAM,EAAE,kCAAkC;IAC1C,cAAc,EAAE,+CAA+C;CAChE,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAA8B;IAC/D,MAAM,EAAE,IAAI,EAAE,GAAG,WAAW,EAAE,CAAA;IAC9B,MAAM,EAAE,UAAU,EAAE,GAAG,KAAK,CAAA;IAC5B,OAAO,CACL,KAAC,aAAa,IACZ,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE;YACN,OAAO,EAAE,iBAAiB;YAC1B,MAAM,EAAE,cAAc;YACtB,SAAS,EAAE,6BAA6B;YACxC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,qBAAqB,CAAC,KAAK,EAAE,EAAE,UAAU,EAAE,CAAC;SACnE,GACD,CACH,CAAA;AACH,CAAC"}
|
package/dist/LoginForm.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import type { Session, SupatypeError } from "@supatype/client";
|
|
3
|
+
import type { AuthFormClassNames } from "./classNames.js";
|
|
3
4
|
export interface LoginFormLabels {
|
|
4
5
|
title?: string | undefined;
|
|
5
6
|
email?: string | undefined;
|
|
@@ -16,6 +17,8 @@ export interface LoginFormProps {
|
|
|
16
17
|
className?: string | undefined;
|
|
17
18
|
/** Override display labels. */
|
|
18
19
|
labels?: LoginFormLabels | undefined;
|
|
20
|
+
/** Class names for the elements inside the form, so an app can style them with its own classes. */
|
|
21
|
+
classNames?: AuthFormClassNames | undefined;
|
|
19
22
|
}
|
|
20
23
|
/**
|
|
21
24
|
* A minimal, accessible login form that integrates with `useAuth()`.
|
|
@@ -29,5 +32,5 @@ export interface LoginFormProps {
|
|
|
29
32
|
* />
|
|
30
33
|
* ```
|
|
31
34
|
*/
|
|
32
|
-
export declare function LoginForm({ onSuccess, onError, className, labels }: LoginFormProps): React.ReactElement;
|
|
35
|
+
export declare function LoginForm({ onSuccess, onError, className, classNames, labels }: LoginFormProps): React.ReactElement;
|
|
33
36
|
//# sourceMappingURL=LoginForm.d.ts.map
|
package/dist/LoginForm.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LoginForm.d.ts","sourceRoot":"","sources":["../src/LoginForm.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"LoginForm.d.ts","sourceRoot":"","sources":["../src/LoginForm.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAmB,MAAM,OAAO,CAAA;AACvC,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAE9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAGzD,MAAM,WAAW,eAAe;IAC9B,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC7B,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACjC;AAED,MAAM,WAAW,cAAc;IAC7B,8DAA8D;IAC9D,SAAS,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC,GAAG,SAAS,CAAA;IACpD,0DAA0D;IAC1D,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC,GAAG,SAAS,CAAA;IACtD,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC9B,+BAA+B;IAC/B,MAAM,CAAC,EAAE,eAAe,GAAG,SAAS,CAAA;IACpC,mGAAmG;IACnG,UAAU,CAAC,EAAE,kBAAkB,GAAG,SAAS,CAAA;CAC5C;AAUD;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE,cAAc,GAAG,KAAK,CAAC,YAAY,CAqCnH"}
|
package/dist/LoginForm.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useState } from "react";
|
|
3
3
|
import { useAuth } from "@supatype/react";
|
|
4
|
+
import { FormField, FormShell, useAuthSubmit, withDefaults } from "./formParts.js";
|
|
4
5
|
const DEFAULT_LABELS = {
|
|
5
6
|
title: "Sign in",
|
|
6
7
|
email: "Email address",
|
|
@@ -20,32 +21,15 @@ const DEFAULT_LABELS = {
|
|
|
20
21
|
* />
|
|
21
22
|
* ```
|
|
22
23
|
*/
|
|
23
|
-
export function LoginForm({ onSuccess, onError, className, labels }) {
|
|
24
|
+
export function LoginForm({ onSuccess, onError, className, classNames, labels }) {
|
|
24
25
|
const { signIn } = useAuth();
|
|
25
|
-
const l =
|
|
26
|
+
const l = withDefaults(DEFAULT_LABELS, labels);
|
|
26
27
|
const [email, setEmail] = useState("");
|
|
27
28
|
const [password, setPassword] = useState("");
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
setLoading(true);
|
|
34
|
-
try {
|
|
35
|
-
const { data, error } = await signIn({ email, password });
|
|
36
|
-
if (error !== null) {
|
|
37
|
-
const msg = `${l.errorPrefix}${error.message}`;
|
|
38
|
-
setErrorMessage(msg);
|
|
39
|
-
onError?.(error);
|
|
40
|
-
}
|
|
41
|
-
else if (data.session !== null) {
|
|
42
|
-
onSuccess?.(data.session);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
finally {
|
|
46
|
-
setLoading(false);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return (_jsxs("form", { onSubmit: (e) => { void handleSubmit(e); }, className: className, noValidate: true, children: [_jsx("h2", { children: l.title }), errorMessage !== null && (_jsx("p", { role: "alert", "aria-live": "polite", style: { color: "red" }, children: errorMessage })), _jsxs("div", { children: [_jsx("label", { htmlFor: "st-login-email", children: l.email }), _jsx("input", { id: "st-login-email", type: "email", autoComplete: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value), disabled: loading })] }), _jsxs("div", { children: [_jsx("label", { htmlFor: "st-login-password", children: l.password }), _jsx("input", { id: "st-login-password", type: "password", autoComplete: "current-password", required: true, value: password, onChange: (e) => setPassword(e.target.value), disabled: loading })] }), _jsx("button", { type: "submit", disabled: loading, children: loading ? "Signing in…" : l.submit })] }));
|
|
29
|
+
const onDone = ({ session }) => { if (session !== null)
|
|
30
|
+
onSuccess?.(session); };
|
|
31
|
+
const request = useAuthSubmit(() => signIn({ email, password }), { onDone, onError });
|
|
32
|
+
const errorMessage = request.errorMessage === null ? null : `${l.errorPrefix}${request.errorMessage}`;
|
|
33
|
+
return (_jsxs(FormShell, { request: request, text: { title: l.title, submit: l.submit, busy: "Signing in…" }, className: className, classNames: classNames, errorMessage: errorMessage, children: [_jsx(FormField, { id: "st-login-email", label: l.email, classNames: classNames, type: "email", autoComplete: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value), disabled: request.loading }), _jsx(FormField, { id: "st-login-password", label: l.password, classNames: classNames, type: "password", autoComplete: "current-password", required: true, value: password, onChange: (e) => setPassword(e.target.value), disabled: request.loading })] }));
|
|
50
34
|
}
|
|
51
35
|
//# sourceMappingURL=LoginForm.js.map
|
package/dist/LoginForm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LoginForm.js","sourceRoot":"","sources":["../src/LoginForm.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"LoginForm.js","sourceRoot":"","sources":["../src/LoginForm.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEvC,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAEzC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE,YAAY,EAAe,MAAM,gBAAgB,CAAA;AAuB/F,MAAM,cAAc,GAA4B;IAC9C,KAAK,EAAE,SAAS;IAChB,KAAK,EAAE,eAAe;IACtB,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,SAAS;IACjB,WAAW,EAAE,EAAE;CAChB,CAAA;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,SAAS,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAkB;IAC7F,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,CAAA;IAC5B,MAAM,CAAC,GAAG,YAAY,CAAC,cAAc,EAAE,MAAM,CAAC,CAAA;IAE9C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAA;IACtC,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAA;IAC5C,MAAM,MAAM,GAAG,CAAC,EAAE,OAAO,EAA+B,EAAQ,EAAE,GAAG,IAAI,OAAO,KAAK,IAAI;QAAE,SAAS,EAAE,CAAC,OAAO,CAAC,CAAA,CAAC,CAAC,CAAA;IACjH,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAA;IACrF,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,WAAW,GAAG,OAAO,CAAC,YAAY,EAAE,CAAA;IAErG,OAAO,CACL,MAAC,SAAS,IAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,aACpK,KAAC,SAAS,IACR,EAAE,EAAC,gBAAgB,EACnB,KAAK,EAAE,CAAC,CAAC,KAAK,EACd,UAAU,EAAE,UAAU,EACtB,IAAI,EAAC,OAAO,EACZ,YAAY,EAAC,OAAO,EACpB,QAAQ,QACR,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EACzC,QAAQ,EAAE,OAAO,CAAC,OAAO,GACzB,EAEF,KAAC,SAAS,IACR,EAAE,EAAC,mBAAmB,EACtB,KAAK,EAAE,CAAC,CAAC,QAAQ,EACjB,UAAU,EAAE,UAAU,EACtB,IAAI,EAAC,UAAU,EACf,YAAY,EAAC,kBAAkB,EAC/B,QAAQ,QACR,KAAK,EAAE,QAAQ,EACf,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAC5C,QAAQ,EAAE,OAAO,CAAC,OAAO,GACzB,IACQ,CACb,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { type EmailLinkFormLabels, type EmailLinkFormProps } from "./EmailLinkForm.js";
|
|
3
|
+
export type MagicLinkFormLabels = EmailLinkFormLabels;
|
|
4
|
+
export type MagicLinkFormProps = EmailLinkFormProps;
|
|
5
|
+
/**
|
|
6
|
+
* Emails the user a link that signs them in without a password (`signInWithOtp`, `POST /otp`).
|
|
7
|
+
* Must be rendered inside a `<SupatypeProvider>`.
|
|
8
|
+
*
|
|
9
|
+
* Following the link signs the user in on `redirectTo` (honoured on the site URL's host or the auth
|
|
10
|
+
* server's redirect allow list, else the site URL); the client reads the session from the URL on
|
|
11
|
+
* start and `useAuth()` shows the user.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* <MagicLinkForm redirectTo={window.location.origin} />
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function MagicLinkForm(props: MagicLinkFormProps): React.ReactElement;
|
|
19
|
+
//# sourceMappingURL=MagicLinkForm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MagicLinkForm.d.ts","sourceRoot":"","sources":["../src/MagicLinkForm.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAiB,KAAK,mBAAmB,EAAE,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAGrG,MAAM,MAAM,mBAAmB,GAAG,mBAAmB,CAAA;AACrD,MAAM,MAAM,kBAAkB,GAAG,kBAAkB,CAAA;AASnD;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,KAAK,CAAC,YAAY,CAc3E"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useSupatype } from "@supatype/react";
|
|
3
|
+
import { EmailLinkForm } from "./EmailLinkForm.js";
|
|
4
|
+
const DEFAULT_LABELS = {
|
|
5
|
+
title: "Sign in with a magic link",
|
|
6
|
+
email: "Email address",
|
|
7
|
+
submit: "Send magic link",
|
|
8
|
+
successMessage: "Check your email for the magic link.",
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Emails the user a link that signs them in without a password (`signInWithOtp`, `POST /otp`).
|
|
12
|
+
* Must be rendered inside a `<SupatypeProvider>`.
|
|
13
|
+
*
|
|
14
|
+
* Following the link signs the user in on `redirectTo` (honoured on the site URL's host or the auth
|
|
15
|
+
* server's redirect allow list, else the site URL); the client reads the session from the URL on
|
|
16
|
+
* start and `useAuth()` shows the user.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* <MagicLinkForm redirectTo={window.location.origin} />
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function MagicLinkForm(props) {
|
|
24
|
+
const { auth } = useSupatype();
|
|
25
|
+
const { redirectTo } = props;
|
|
26
|
+
return (_jsx(EmailLinkForm, { props: props, config: {
|
|
27
|
+
inputId: "st-magic-email",
|
|
28
|
+
labels: DEFAULT_LABELS,
|
|
29
|
+
busyLabel: "Sending magic link…",
|
|
30
|
+
send: (email) => auth.signInWithOtp({ email, options: { emailRedirectTo: redirectTo } }),
|
|
31
|
+
} }));
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=MagicLinkForm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MagicLinkForm.js","sourceRoot":"","sources":["../src/MagicLinkForm.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAqD,MAAM,oBAAoB,CAAA;AAMrG,MAAM,cAAc,GAAgC;IAClD,KAAK,EAAE,2BAA2B;IAClC,KAAK,EAAE,eAAe;IACtB,MAAM,EAAE,iBAAiB;IACzB,cAAc,EAAE,sCAAsC;CACvD,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,aAAa,CAAC,KAAyB;IACrD,MAAM,EAAE,IAAI,EAAE,GAAG,WAAW,EAAE,CAAA;IAC9B,MAAM,EAAE,UAAU,EAAE,GAAG,KAAK,CAAA;IAC5B,OAAO,CACL,KAAC,aAAa,IACZ,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE;YACN,OAAO,EAAE,gBAAgB;YACzB,MAAM,EAAE,cAAc;YACtB,SAAS,EAAE,qBAAqB;YAChC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,eAAe,EAAE,UAAU,EAAE,EAAE,CAAC;SACzF,GACD,CACH,CAAA;AACH,CAAC"}
|
package/dist/OAuthButton.d.ts
CHANGED
|
@@ -33,7 +33,8 @@ export interface OAuthButtonProps {
|
|
|
33
33
|
/**
|
|
34
34
|
* A pre-styled OAuth sign-in button with provider logo.
|
|
35
35
|
*
|
|
36
|
-
* Must be rendered inside a `<SupatypeProvider>`.
|
|
36
|
+
* Must be rendered inside a `<SupatypeProvider>`. Its accessible name is its visible text, so a
|
|
37
|
+
* label passed as `children` is what a screen reader announces too.
|
|
37
38
|
*/
|
|
38
|
-
export declare function OAuthButton(
|
|
39
|
+
export declare function OAuthButton(props: OAuthButtonProps): React.ReactElement;
|
|
39
40
|
//# sourceMappingURL=OAuthButton.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OAuthButton.d.ts","sourceRoot":"","sources":["../src/OAuthButton.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"OAuthButton.d.ts","sourceRoot":"","sources":["../src/OAuthButton.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,EAA+B,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AAG1E,MAAM,WAAW,gBAAgB;IAC/B,8DAA8D;IAC9D,QAAQ,EAAE,MAAM,CAAA;IAChB,0DAA0D;IAC1D,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,+CAA+C;IAC/C,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC9B,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,SAAS,GAAG,SAAS,CAAA;IAChC,yCAAyC;IACzC,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC,GAAG,SAAS,CAAA;IAC5D,oEAAoE;IACpE,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IAC3B,6BAA6B;IAC7B,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC/B;AAqGD;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,KAAK,CAAC,YAAY,CAyBvE"}
|
package/dist/OAuthButton.js
CHANGED
|
@@ -12,11 +12,17 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
12
12
|
* <OAuthButton provider="apple" className="dark-btn">Sign in with Apple</OAuthButton>
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
|
-
import { useState } from "react";
|
|
15
|
+
import { useEffect, useRef, useState } from "react";
|
|
16
16
|
import { useAuth } from "@supatype/react";
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
/** Names whose capitalisation is not just a capital first letter. */
|
|
18
|
+
const PROVIDER_NAMES = {
|
|
19
|
+
github: "GitHub",
|
|
20
|
+
gitlab: "GitLab",
|
|
21
|
+
linkedin: "LinkedIn",
|
|
22
|
+
};
|
|
23
|
+
/** The provider's display name: its own spelling where known, else a capital first letter. */
|
|
24
|
+
function displayName(provider) {
|
|
25
|
+
return PROVIDER_NAMES[provider.toLowerCase()] ?? provider.charAt(0).toUpperCase() + provider.slice(1);
|
|
20
26
|
}
|
|
21
27
|
/** Provider logo SVGs (inline to avoid external dependencies). */
|
|
22
28
|
const PROVIDER_ICONS = {
|
|
@@ -24,39 +30,72 @@ const PROVIDER_ICONS = {
|
|
|
24
30
|
google: '<svg viewBox="0 0 24 24" width="20" height="20"><path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 01-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z"/><path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/><path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/><path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/></svg>',
|
|
25
31
|
apple: '<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor"><path d="M17.05 20.28c-.98.95-2.05.88-3.08.4-1.09-.5-2.08-.53-3.23 0-1.44.62-2.2.44-3.06-.4C3.79 16.17 4.36 9.33 8.93 9.07c1.23.07 2.09.72 2.81.77.99-.2 1.94-.78 3.01-.7 1.28.1 2.24.6 2.87 1.52-2.63 1.57-2 5.07.63 6.06-.5 1.3-.99 2.58-2.19 3.56zM12.03 9c-.12-2.08 1.55-3.82 3.47-3.97.26 2.29-2.07 4.01-3.47 3.97z"/></svg>',
|
|
26
32
|
};
|
|
33
|
+
/** Send the browser to the provider: this window, or a centred popup. */
|
|
34
|
+
function openOAuthUrl(url, provider, popup) {
|
|
35
|
+
if (!popup) {
|
|
36
|
+
window.location.href = url;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const width = 500;
|
|
40
|
+
const height = 700;
|
|
41
|
+
const left = window.screenX + (window.outerWidth - width) / 2;
|
|
42
|
+
const top = window.screenY + (window.outerHeight - height) / 2;
|
|
43
|
+
window.open(url, `supatype-oauth-${provider}`, `width=${width},height=${height},left=${left},top=${top}`);
|
|
44
|
+
}
|
|
45
|
+
/** The button's look when the app gives it no class. */
|
|
46
|
+
function defaultStyle(busy) {
|
|
47
|
+
return {
|
|
48
|
+
display: "inline-flex",
|
|
49
|
+
alignItems: "center",
|
|
50
|
+
gap: "8px",
|
|
51
|
+
padding: "10px 16px",
|
|
52
|
+
border: "1px solid #d1d5db",
|
|
53
|
+
borderRadius: "6px",
|
|
54
|
+
backgroundColor: "#fff",
|
|
55
|
+
color: "#374151",
|
|
56
|
+
fontSize: "14px",
|
|
57
|
+
fontWeight: 500,
|
|
58
|
+
cursor: busy ? "not-allowed" : "pointer",
|
|
59
|
+
opacity: busy ? 0.6 : 1,
|
|
60
|
+
transition: "background-color 0.15s, border-color 0.15s",
|
|
61
|
+
lineHeight: 1,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
27
64
|
/**
|
|
28
|
-
*
|
|
65
|
+
* Report a refusal the auth server sent back in the URL, when this button's provider started it.
|
|
66
|
+
*
|
|
67
|
+
* Through a ref so an inline `onError` does not report the same refusal on every render.
|
|
68
|
+
*/
|
|
69
|
+
function useRedirectError(provider, onError) {
|
|
70
|
+
const { error } = useAuth();
|
|
71
|
+
const onErrorRef = useRef(onError);
|
|
72
|
+
onErrorRef.current = onError;
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (error !== null && error.provider === provider)
|
|
75
|
+
onErrorRef.current?.(error);
|
|
76
|
+
}, [error, provider]);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The button's click: ask for the provider's URL and go there, or report why not.
|
|
29
80
|
*
|
|
30
|
-
*
|
|
81
|
+
* Also reports a refusal the server sent back in the URL for this provider (`useRedirectError`).
|
|
31
82
|
*/
|
|
32
|
-
|
|
83
|
+
function useOAuthStart(props) {
|
|
84
|
+
const { provider, redirectTo, onError, popup } = props;
|
|
33
85
|
const { signInWithOAuth } = useAuth();
|
|
34
86
|
const [loading, setLoading] = useState(false);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
async function handleClick() {
|
|
87
|
+
useRedirectError(provider, onError);
|
|
88
|
+
async function start() {
|
|
38
89
|
setLoading(true);
|
|
39
90
|
try {
|
|
40
91
|
const { data, error } = await signInWithOAuth({
|
|
41
92
|
provider,
|
|
42
93
|
...(redirectTo !== undefined && { options: { redirectTo } }),
|
|
43
94
|
});
|
|
44
|
-
if (error !== null)
|
|
95
|
+
if (error !== null)
|
|
45
96
|
onError?.(error);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (data.url) {
|
|
49
|
-
if (popup === true) {
|
|
50
|
-
const width = 500;
|
|
51
|
-
const height = 700;
|
|
52
|
-
const left = window.screenX + (window.outerWidth - width) / 2;
|
|
53
|
-
const top = window.screenY + (window.outerHeight - height) / 2;
|
|
54
|
-
window.open(data.url, `supatype-oauth-${provider}`, `width=${width},height=${height},left=${left},top=${top}`);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
window.location.href = data.url;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
97
|
+
else if (data.url)
|
|
98
|
+
openOAuthUrl(data.url, provider, popup === true);
|
|
60
99
|
}
|
|
61
100
|
catch (err) {
|
|
62
101
|
onError?.({ message: err instanceof Error ? err.message : "OAuth failed" });
|
|
@@ -65,22 +104,20 @@ export function OAuthButton({ provider, redirectTo, className, children, onError
|
|
|
65
104
|
setLoading(false);
|
|
66
105
|
}
|
|
67
106
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
};
|
|
84
|
-
return (_jsxs("button", { type: "button", className: className, style: className !== undefined ? undefined : defaultStyle, onClick: () => { void handleClick(); }, disabled: disabled === true || loading, "aria-label": `Sign in with ${providerName}`, children: [iconSvg !== undefined && (_jsx("span", { "aria-hidden": "true", dangerouslySetInnerHTML: { __html: iconSvg }, style: { display: "inline-flex", alignItems: "center" } })), children ?? (loading ? `Connecting to ${providerName}…` : `Sign in with ${providerName}`)] }));
|
|
107
|
+
return { loading, start };
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* A pre-styled OAuth sign-in button with provider logo.
|
|
111
|
+
*
|
|
112
|
+
* Must be rendered inside a `<SupatypeProvider>`. Its accessible name is its visible text, so a
|
|
113
|
+
* label passed as `children` is what a screen reader announces too.
|
|
114
|
+
*/
|
|
115
|
+
export function OAuthButton(props) {
|
|
116
|
+
const { provider, className, children, disabled } = props;
|
|
117
|
+
const { loading, start } = useOAuthStart(props);
|
|
118
|
+
const providerName = displayName(provider);
|
|
119
|
+
const iconSvg = PROVIDER_ICONS[provider.toLowerCase()];
|
|
120
|
+
const busy = disabled === true || loading;
|
|
121
|
+
return (_jsxs("button", { type: "button", className: className, style: className !== undefined ? undefined : defaultStyle(busy), onClick: () => { void start(); }, disabled: busy, children: [iconSvg !== undefined && (_jsx("span", { "aria-hidden": "true", dangerouslySetInnerHTML: { __html: iconSvg }, style: { display: "inline-flex", alignItems: "center" } })), children ?? (loading ? `Connecting to ${providerName}…` : `Sign in with ${providerName}`)] }));
|
|
85
122
|
}
|
|
86
123
|
//# sourceMappingURL=OAuthButton.js.map
|