@zenauth/react 0.0.0-stage → 0.5.1
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 +186 -2
- package/dist/index.cjs +1468 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +470 -0
- package/dist/index.d.ts +470 -0
- package/dist/index.js +1429 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ZenAuth contributors
|
|
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,187 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://zenauth.kenilpatel.in"><img src="https://raw.githubusercontent.com/patelkenil26/ZenAuth/main/apps/web/public/logo/zenauth-app-icon-512.png" width="72" height="72" alt="ZenAuth logo" /></a>
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">@zenauth/react</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">Authentication for React. Add sign-in to your app with a few components and hooks.</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/@zenauth/react"><img src="https://img.shields.io/npm/v/@zenauth/react?color=ea6a20&label=npm" alt="npm version" /></a>
|
|
11
|
+
<img src="https://img.shields.io/badge/license-MIT-ea6a20" alt="MIT license" />
|
|
12
|
+
<img src="https://img.shields.io/badge/TypeScript-types%20included-3178c6" alt="TypeScript types included" />
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<a href="https://zenauth.kenilpatel.in"><b>Website</b></a> ·
|
|
17
|
+
<a href="https://zenauth.kenilpatel.in/docs#react-sdk"><b>Documentation</b></a> ·
|
|
18
|
+
<a href="https://zenauth.kenilpatel.in/developer"><b>Get your keys</b></a> ·
|
|
19
|
+
<a href="https://github.com/patelkenil26/ZenAuth"><b>GitHub</b></a>
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
[ZenAuth](https://zenauth.kenilpatel.in) is an OpenID Connect authentication service. This SDK runs the **Authorization Code flow with PKCE** for you. It is a public client: no client secret ever reaches the browser.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
| | |
|
|
27
|
+
| --- | --- |
|
|
28
|
+
| **Ready-made components** | `<SignIn />`, `<UserButton />`, `<SignedIn>`, `<SignedOut>` |
|
|
29
|
+
| **Embedded forms** | Sign-in inside your own pages, in your theme. Sign-up and password reset too, [on approved domains](#sign-up-and-password-reset-on-your-own-pages) |
|
|
30
|
+
| **Sign-in methods** | Password, Google or GitHub, email code, and two-factor |
|
|
31
|
+
| **Roles and teams** | `user.role` and `user.orgs` from your ZenAuth app |
|
|
32
|
+
| **Single logout** | End the ZenAuth session too, then return to your app |
|
|
33
|
+
| **Headless** | `useZenAuth()` and `ZenAuthClient` if you want your own UI |
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install @zenauth/react
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Requires React 18 or later.
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
44
|
+
|
|
45
|
+
1. In the ZenAuth developer console, create an application. Its redirect URI must match the one you pass to the SDK **exactly**.
|
|
46
|
+
2. Wrap your app:
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
"use client";
|
|
50
|
+
import { ZenAuthProvider } from "@zenauth/react";
|
|
51
|
+
|
|
52
|
+
export function Providers({ children }: { children: React.ReactNode }) {
|
|
53
|
+
return (
|
|
54
|
+
<ZenAuthProvider
|
|
55
|
+
clientId={process.env.NEXT_PUBLIC_ZENAUTH_CLIENT_ID!}
|
|
56
|
+
redirectUri="https://yourapp.com/auth/callback"
|
|
57
|
+
issuer="https://auth-api.kenilpatel.in" // base URL of the ZenAuth API
|
|
58
|
+
>
|
|
59
|
+
{children}
|
|
60
|
+
</ZenAuthProvider>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The provider finishes sign-in on whatever page your redirect URI points to. That page only has to render inside the provider.
|
|
66
|
+
|
|
67
|
+
3. Use it:
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import { SignIn, SignedIn, SignedOut, UserButton, useZenAuth } from "@zenauth/react";
|
|
71
|
+
|
|
72
|
+
<SignedOut><SignIn returnTo="/dashboard" /></SignedOut>
|
|
73
|
+
<SignedIn><UserButton accountUrl="https://zenauth.kenilpatel.in/dashboard" /></SignedIn>
|
|
74
|
+
|
|
75
|
+
const { user, isLoading, getAccessToken, signOut } = useZenAuth();
|
|
76
|
+
const token = await getAccessToken(); // refreshed when about to expire; null when signed out
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## API
|
|
80
|
+
|
|
81
|
+
| Export | What it does |
|
|
82
|
+
| --- | --- |
|
|
83
|
+
| `ZenAuthProvider` | Props: `clientId`, `redirectUri`, `issuer`, optional `scope` (default `openid profile email`) and `storage`. |
|
|
84
|
+
| `useZenAuth()` | `{ status, isLoading, isAuthenticated, user, error, signIn, signOut, getAccessToken }` |
|
|
85
|
+
| `<SignIn returnTo? className?>` | Sign-in button. `returnTo` must be a path on your site. |
|
|
86
|
+
| `<UserButton accountUrl? afterSignOutUrl?>` | Avatar menu with the user's details and "Sign out". Renders nothing when signed out. |
|
|
87
|
+
| `<SignedIn>` / `<SignedOut>` | Render children only in that state. |
|
|
88
|
+
| `<SignInForm />`, `<SignUpForm />` | Embedded forms, described below. |
|
|
89
|
+
| `<ForgotPasswordForm />`, `<ResetPasswordForm />`, `<VerifyEmail />` | Account pages on your own site, described below. |
|
|
90
|
+
| `ZenAuthClient` | The framework-independent client the provider is built on. |
|
|
91
|
+
|
|
92
|
+
`signIn()` never throws: failures (for example ZenAuth being unreachable, or the user cancelling) show up in `error`.
|
|
93
|
+
|
|
94
|
+
## Roles, organizations and single logout
|
|
95
|
+
|
|
96
|
+
`user.role` is the role you gave this person in your app. Add the `organizations` scope to also get `user.orgs`.
|
|
97
|
+
|
|
98
|
+
`signOut()` ends the session in your app. To end the ZenAuth session too, pass a return URL that is registered
|
|
99
|
+
as a logout redirect URI in the developer console:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
await signOut({ postLogoutRedirectUri: "https://your-app.com/" });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Embedded sign-in form (no redirect to ZenAuth's page)
|
|
106
|
+
|
|
107
|
+
`<SignInForm />` renders email + password (and optional social buttons) **inside your own page**, in your own theme.
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
import { SignInForm } from "@zenauth/react";
|
|
111
|
+
|
|
112
|
+
<SignInForm
|
|
113
|
+
returnTo="/dashboard"
|
|
114
|
+
providers={["google"]}
|
|
115
|
+
forgotPasswordHref="/forgot-password"
|
|
116
|
+
onSuccess={(to) => router.replace(to ?? "/dashboard")}
|
|
117
|
+
appearance={{ variables: { primary: "#65a30d", radius: "0.75rem" } }}
|
|
118
|
+
/>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
- **Theming:** unset tokens inherit your page's text colour and font. Pass `appearance.variables` (CSS variables) and/or `appearance.classNames` (Tailwind etc.). Prefer your own UI? Use `useZenAuth().signInWithPassword()` / `signInWithProvider()`.
|
|
122
|
+
- **Required console setting:** set the app's **Application URL** to the site that hosts the form (e.g. `https://forms.example.com`). ZenAuth only accepts embedded sign-ins from that origin.
|
|
123
|
+
- **Backend mode:** if your server keeps the client secret (confidential client), pass `getAuthContext` returning the context from your server. The browser posts the credentials to ZenAuth, then navigates to your server's redirect URI with the code; your server finishes the exchange. The PKCE verifier never reaches the browser.
|
|
124
|
+
- **Two-factor** accounts get a code step inside the form (authenticator or backup code). Nothing to configure.
|
|
125
|
+
- **Email code** (passwordless): pass `emailCode` to `<SignInForm />` to offer "Email me a sign-in code". It is the inline replacement for magic links, which would have to leave your page.
|
|
126
|
+
- **Sign-up:** `<SignUpForm turnstileSiteKey="..." />`. The user verifies their email, then signs in. See the requirement below.
|
|
127
|
+
- **Headless:** `useZenAuth()` also exposes `signInWithEmailCode`, `sendEmailCode`, `verifyMfa` and `signUp` for fully custom UI.
|
|
128
|
+
- **Not in the forms yet:** passkeys.
|
|
129
|
+
|
|
130
|
+
### Sign-up and password reset on your own pages
|
|
131
|
+
|
|
132
|
+
`<SignUpForm />` and `<ForgotPasswordForm />` are protected by ZenAuth's Cloudflare Turnstile check. They work only
|
|
133
|
+
if whoever runs your ZenAuth server has added your domain to its Turnstile widget and given you the site key.
|
|
134
|
+
Without that, send people to the hosted [sign-up](https://zenauth.kenilpatel.in/register) and
|
|
135
|
+
[forgot password](https://zenauth.kenilpatel.in/forgot-password) pages. `<SignInForm />` does not need it.
|
|
136
|
+
|
|
137
|
+
## Login context (device, IP, approximate location)
|
|
138
|
+
|
|
139
|
+
Add the `login_context` scope and every sign-in comes with the security facts, so you never write device or location detection yourself:
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
<ZenAuthProvider scope="openid profile email login_context" ... />
|
|
143
|
+
|
|
144
|
+
const { user } = useZenAuth();
|
|
145
|
+
user?.login_context?.device.browser; // "Chrome"
|
|
146
|
+
user?.login_context?.location.city; // "Pune" (from the IP: approximate, not GPS)
|
|
147
|
+
user?.login_context?.is_new_device; // true the first time this browser + OS + country is seen
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
It is a signed claim of the ID token (`login_context`), delivered to your server in backend mode. Store it with `event_id` as a unique key. ZenAuth also keeps its own login history and emails the user about sign-ins from unfamiliar devices. Full field list: the *Login context* section of the ZenAuth docs.
|
|
151
|
+
|
|
152
|
+
## Branded emails (your users never see ZenAuth)
|
|
153
|
+
|
|
154
|
+
Emails triggered from your own forms (sign-up verification, password reset, email codes, new-device alerts) go out as **your app**: your name, logo, colour and sender name, with links to your own site. Set **Application URL**, **Sender name**, **Logo URL** and **Brand colour** on your app in the developer console, then add three pages:
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
import { ForgotPasswordForm, ResetPasswordForm, VerifyEmail } from "@zenauth/react";
|
|
158
|
+
|
|
159
|
+
<ForgotPasswordForm turnstileSiteKey={...} /> // /forgot-password
|
|
160
|
+
<ResetPasswordForm token={searchParams.token} /> // /reset-password?token=... (link in the email)
|
|
161
|
+
<VerifyEmail token={searchParams.token} /> // /verify-email?token=... (link in the email)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
App-branded email is only sent to users who signed up or signed in through your app. Headless equivalents: `forgotPassword`, `resetPassword`, `verifyEmail` on `useZenAuth()`.
|
|
165
|
+
|
|
166
|
+
## Security notes
|
|
167
|
+
|
|
168
|
+
- Tokens are kept in `sessionStorage` (per tab, cleared when the tab closes), so there are no cross-tab refresh races. Like any browser-stored token, they are exposed to XSS: keep a strict Content-Security-Policy.
|
|
169
|
+
- `returnTo` only accepts same-origin paths, so it cannot be used as an open redirect.
|
|
170
|
+
- The ID token's `aud`, `iss`, `nonce` and `exp` are checked. The signature is not verified in the browser because the token arrives directly from the token endpoint over TLS (OIDC Core 3.1.3.7). Verify it on your server with the JWKS endpoint if you forward it.
|
|
171
|
+
- Web Crypto is required, so serve your app over HTTPS (`localhost` is fine in development).
|
|
172
|
+
|
|
173
|
+
## ZenAuth SDKs
|
|
174
|
+
|
|
175
|
+
| Package | Use it for |
|
|
176
|
+
| --- | --- |
|
|
177
|
+
| **@zenauth/react** (this one) | Sign-in components, hooks and embedded forms for React apps |
|
|
178
|
+
| [@zenauth/nextjs](https://www.npmjs.com/package/@zenauth/nextjs) | Server-side sign-in, session cookie and route protection for Next.js |
|
|
179
|
+
| [@zenauth/node](https://www.npmjs.com/package/@zenauth/node) | Verify access tokens and webhooks in any Node backend |
|
|
180
|
+
|
|
181
|
+
## Support
|
|
182
|
+
|
|
183
|
+
Found a bug or need help? [Open an issue](https://github.com/patelkenil26/ZenAuth/issues) or read the [documentation](https://zenauth.kenilpatel.in/docs). Please do not include client secrets, API keys or tokens in an issue.
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
MIT
|