@netlify/identity 0.1.1-alpha.1 → 0.1.1-alpha.10
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/README.md +127 -6
- package/dist/index.cjs +298 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -6
- package/dist/index.d.ts +19 -6
- package/dist/index.js +297 -23
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -74,7 +74,7 @@ Returns the current authenticated user, or `null` if not logged in. Synchronous,
|
|
|
74
74
|
isAuthenticated(): boolean
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
-
Returns `true` if a user is currently authenticated. Equivalent to `getUser() !== null`.
|
|
77
|
+
Returns `true` if a user is currently authenticated. Equivalent to `getUser() !== null`. Never throws.
|
|
78
78
|
|
|
79
79
|
#### `getIdentityConfig`
|
|
80
80
|
|
|
@@ -90,7 +90,9 @@ Returns the Identity endpoint URL (and operator token on the server), or `null`
|
|
|
90
90
|
getSettings(): Promise<Settings>
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
-
Fetches your project's Identity settings (enabled providers, autoconfirm, signup disabled).
|
|
93
|
+
Fetches your project's Identity settings (enabled providers, autoconfirm, signup disabled).
|
|
94
|
+
|
|
95
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the endpoint is unreachable.
|
|
94
96
|
|
|
95
97
|
#### `login`
|
|
96
98
|
|
|
@@ -98,7 +100,11 @@ Fetches your project's Identity settings (enabled providers, autoconfirm, signup
|
|
|
98
100
|
login(email: string, password: string): Promise<User>
|
|
99
101
|
```
|
|
100
102
|
|
|
101
|
-
Logs in with email and password.
|
|
103
|
+
Logs in with email and password. Works in both browser and server contexts.
|
|
104
|
+
|
|
105
|
+
In the browser, uses gotrue-js and emits a `'login'` event. On the server (Netlify Functions, Edge Functions), calls the GoTrue HTTP API directly and sets the `nf_jwt` cookie via the Netlify runtime.
|
|
106
|
+
|
|
107
|
+
**Throws:** `AuthError` on invalid credentials or network failure. In the browser, `MissingIdentityError` if Identity is not configured. On the server, `AuthError` if the Netlify Functions runtime is not available.
|
|
102
108
|
|
|
103
109
|
#### `signup`
|
|
104
110
|
|
|
@@ -106,7 +112,11 @@ Logs in with email and password. Browser only.
|
|
|
106
112
|
signup(email: string, password: string, data?: Record<string, unknown>): Promise<User>
|
|
107
113
|
```
|
|
108
114
|
|
|
109
|
-
Creates a new account.
|
|
115
|
+
Creates a new account. Works in both browser and server contexts.
|
|
116
|
+
|
|
117
|
+
In the browser, uses gotrue-js and emits `'login'` if autoconfirm is enabled. On the server, calls the GoTrue HTTP API directly and sets the `nf_jwt` cookie if the user is auto-confirmed.
|
|
118
|
+
|
|
119
|
+
**Throws:** `AuthError` on failure (e.g., email already registered, signup disabled). In the browser, `MissingIdentityError` if Identity is not configured. On the server, `AuthError` if the Netlify Functions runtime is not available.
|
|
110
120
|
|
|
111
121
|
#### `logout`
|
|
112
122
|
|
|
@@ -114,7 +124,11 @@ Creates a new account. Emits `'login'` if autoconfirm is enabled. Browser only.
|
|
|
114
124
|
logout(): Promise<void>
|
|
115
125
|
```
|
|
116
126
|
|
|
117
|
-
Logs out the current user and clears the session.
|
|
127
|
+
Logs out the current user and clears the session. Works in both browser and server contexts.
|
|
128
|
+
|
|
129
|
+
In the browser, uses gotrue-js and emits a `'logout'` event. On the server, calls GoTrue's `/logout` endpoint with the JWT from the `nf_jwt` cookie, then deletes the cookie.
|
|
130
|
+
|
|
131
|
+
**Throws:** `AuthError` on network failure. In the browser, `MissingIdentityError` if Identity is not configured. On the server, `AuthError` if the Netlify Functions runtime is not available.
|
|
118
132
|
|
|
119
133
|
#### `oauthLogin`
|
|
120
134
|
|
|
@@ -122,7 +136,11 @@ Logs out the current user and clears the session. Browser only.
|
|
|
122
136
|
oauthLogin(provider: string): never
|
|
123
137
|
```
|
|
124
138
|
|
|
125
|
-
Redirects to an OAuth provider.
|
|
139
|
+
Redirects to an OAuth provider. The page navigates away, so this function never returns normally. Browser only.
|
|
140
|
+
|
|
141
|
+
The `provider` argument should be one of the `AuthProvider` values: `'google'`, `'github'`, `'gitlab'`, `'bitbucket'`, `'facebook'`, or `'saml'`.
|
|
142
|
+
|
|
143
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `Error` if called on the server.
|
|
126
144
|
|
|
127
145
|
#### `handleAuthCallback`
|
|
128
146
|
|
|
@@ -132,6 +150,8 @@ handleAuthCallback(): Promise<CallbackResult | null>
|
|
|
132
150
|
|
|
133
151
|
Processes the URL hash after an OAuth redirect, email confirmation, password recovery, invite acceptance, or email change. Call on page load. Returns `null` if the hash contains no auth parameters. Browser only.
|
|
134
152
|
|
|
153
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if token exchange fails.
|
|
154
|
+
|
|
135
155
|
#### `onAuthChange`
|
|
136
156
|
|
|
137
157
|
```ts
|
|
@@ -148,6 +168,8 @@ requestPasswordRecovery(email: string): Promise<void>
|
|
|
148
168
|
|
|
149
169
|
Sends a password recovery email to the given address.
|
|
150
170
|
|
|
171
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` on network failure.
|
|
172
|
+
|
|
151
173
|
#### `confirmEmail`
|
|
152
174
|
|
|
153
175
|
```ts
|
|
@@ -156,6 +178,8 @@ confirmEmail(token: string): Promise<User>
|
|
|
156
178
|
|
|
157
179
|
Confirms an email address using the token from a confirmation email. Logs the user in on success.
|
|
158
180
|
|
|
181
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the token is invalid or expired.
|
|
182
|
+
|
|
159
183
|
#### `acceptInvite`
|
|
160
184
|
|
|
161
185
|
```ts
|
|
@@ -164,6 +188,8 @@ acceptInvite(token: string, password: string): Promise<User>
|
|
|
164
188
|
|
|
165
189
|
Accepts an invite token and sets a password for the new account. Logs the user in on success.
|
|
166
190
|
|
|
191
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the token is invalid or expired.
|
|
192
|
+
|
|
167
193
|
#### `verifyEmailChange`
|
|
168
194
|
|
|
169
195
|
```ts
|
|
@@ -172,6 +198,18 @@ verifyEmailChange(token: string): Promise<User>
|
|
|
172
198
|
|
|
173
199
|
Verifies an email change using the token from a verification email.
|
|
174
200
|
|
|
201
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the token is invalid.
|
|
202
|
+
|
|
203
|
+
#### `recoverPassword`
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
recoverPassword(token: string, newPassword: string): Promise<User>
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Redeems a recovery token and sets a new password. Logs the user in on success.
|
|
210
|
+
|
|
211
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the token is invalid or expired.
|
|
212
|
+
|
|
175
213
|
#### `updateUser`
|
|
176
214
|
|
|
177
215
|
```ts
|
|
@@ -180,6 +218,8 @@ updateUser(updates: Record<string, unknown>): Promise<User>
|
|
|
180
218
|
|
|
181
219
|
Updates the current user's metadata or credentials. Requires an active session.
|
|
182
220
|
|
|
221
|
+
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if no user is logged in, or the update fails.
|
|
222
|
+
|
|
183
223
|
### Types
|
|
184
224
|
|
|
185
225
|
#### `User`
|
|
@@ -256,6 +296,11 @@ interface CallbackResult {
|
|
|
256
296
|
}
|
|
257
297
|
```
|
|
258
298
|
|
|
299
|
+
The `token` field is only present for `invite` callbacks, where the user hasn't set a password yet. Pass `token` to `acceptInvite(token, password)` to finish.
|
|
300
|
+
|
|
301
|
+
For all other types (`oauth`, `confirmation`, `recovery`, `email_change`), the user is logged in directly and `token` is not set.
|
|
302
|
+
|
|
303
|
+
|
|
259
304
|
### Errors
|
|
260
305
|
|
|
261
306
|
#### `AuthError`
|
|
@@ -277,6 +322,61 @@ Thrown when Identity is not configured in the current environment.
|
|
|
277
322
|
|
|
278
323
|
## Guides
|
|
279
324
|
|
|
325
|
+
### Listening for auth changes
|
|
326
|
+
|
|
327
|
+
Use `onAuthChange` to keep your UI in sync with auth state. It fires on login, logout, token refresh, and user updates. It also detects session changes in other browser tabs (via `localStorage`).
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
import { onAuthChange } from '@netlify/identity'
|
|
331
|
+
|
|
332
|
+
const unsubscribe = onAuthChange((event, user) => {
|
|
333
|
+
switch (event) {
|
|
334
|
+
case 'login':
|
|
335
|
+
console.log('Logged in:', user?.email)
|
|
336
|
+
break
|
|
337
|
+
case 'logout':
|
|
338
|
+
console.log('Logged out')
|
|
339
|
+
break
|
|
340
|
+
case 'token_refresh':
|
|
341
|
+
console.log('Token refreshed for:', user?.email)
|
|
342
|
+
break
|
|
343
|
+
case 'user_updated':
|
|
344
|
+
console.log('User updated:', user?.email)
|
|
345
|
+
break
|
|
346
|
+
}
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
// Later, to stop listening:
|
|
350
|
+
unsubscribe()
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
On the server, `onAuthChange` is a no-op and the returned unsubscribe function does nothing.
|
|
354
|
+
|
|
355
|
+
### OAuth login
|
|
356
|
+
|
|
357
|
+
OAuth login is a two-step flow: redirect the user to the provider, then process the callback when they return.
|
|
358
|
+
|
|
359
|
+
**Step by step:**
|
|
360
|
+
|
|
361
|
+
```ts
|
|
362
|
+
import { oauthLogin, handleAuthCallback } from '@netlify/identity'
|
|
363
|
+
|
|
364
|
+
// 1. Kick off the OAuth flow (e.g., from a "Sign in with GitHub" button).
|
|
365
|
+
// This navigates away from the page and does not return.
|
|
366
|
+
oauthLogin('github')
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
```ts
|
|
370
|
+
// 2. On page load, handle the redirect back from the provider.
|
|
371
|
+
const result = await handleAuthCallback()
|
|
372
|
+
|
|
373
|
+
if (result?.type === 'oauth') {
|
|
374
|
+
console.log('Logged in via OAuth:', result.user?.email)
|
|
375
|
+
}
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
`handleAuthCallback()` exchanges the token in the URL hash, logs the user in, clears the hash, and emits a `'login'` event via `onAuthChange`.
|
|
379
|
+
|
|
280
380
|
### Password recovery
|
|
281
381
|
|
|
282
382
|
Password recovery is a two-step flow. The library handles the token exchange automatically via `handleAuthCallback()`, which logs the user in and returns `{type: 'recovery', user}`. You then show a "set new password" form and call `updateUser()` to save it.
|
|
@@ -300,6 +400,27 @@ if (result?.type === 'recovery') {
|
|
|
300
400
|
}
|
|
301
401
|
```
|
|
302
402
|
|
|
403
|
+
### Invite acceptance
|
|
404
|
+
|
|
405
|
+
When an admin invites a user, they receive an email with an invite link. Clicking it redirects to your site with an `invite_token` in the URL hash. Unlike other callback types, the user is not logged in automatically because they need to set a password first.
|
|
406
|
+
|
|
407
|
+
**Step by step:**
|
|
408
|
+
|
|
409
|
+
```ts
|
|
410
|
+
import { handleAuthCallback, acceptInvite } from '@netlify/identity'
|
|
411
|
+
|
|
412
|
+
// 1. On page load, handle the callback.
|
|
413
|
+
const result = await handleAuthCallback()
|
|
414
|
+
|
|
415
|
+
if (result?.type === 'invite' && result.token) {
|
|
416
|
+
// 2. The user is NOT logged in yet. Show a "set your password" form.
|
|
417
|
+
// When they submit:
|
|
418
|
+
const password = document.getElementById('password').value
|
|
419
|
+
const user = await acceptInvite(result.token, password)
|
|
420
|
+
console.log('Account created:', user.email)
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
303
424
|
## License
|
|
304
425
|
|
|
305
426
|
MIT
|