@netlify/identity 0.1.1-alpha.8 → 0.1.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/README.md +2 -258
- package/dist/index.cjs +25 -500
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -53
- package/dist/index.d.ts +1 -53
- package/dist/index.js +24 -487
- 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`.
|
|
78
78
|
|
|
79
79
|
#### `getIdentityConfig`
|
|
80
80
|
|
|
@@ -90,135 +90,7 @@ 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).
|
|
94
|
-
|
|
95
|
-
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the endpoint is unreachable.
|
|
96
|
-
|
|
97
|
-
#### `login`
|
|
98
|
-
|
|
99
|
-
```ts
|
|
100
|
-
login(email: string, password: string): Promise<User>
|
|
101
|
-
```
|
|
102
|
-
|
|
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.
|
|
108
|
-
|
|
109
|
-
#### `signup`
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
signup(email: string, password: string, data?: Record<string, unknown>): Promise<User>
|
|
113
|
-
```
|
|
114
|
-
|
|
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.
|
|
120
|
-
|
|
121
|
-
#### `logout`
|
|
122
|
-
|
|
123
|
-
```ts
|
|
124
|
-
logout(): Promise<void>
|
|
125
|
-
```
|
|
126
|
-
|
|
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.
|
|
132
|
-
|
|
133
|
-
#### `oauthLogin`
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
oauthLogin(provider: string): never
|
|
137
|
-
```
|
|
138
|
-
|
|
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.
|
|
144
|
-
|
|
145
|
-
#### `handleAuthCallback`
|
|
146
|
-
|
|
147
|
-
```ts
|
|
148
|
-
handleAuthCallback(): Promise<CallbackResult | null>
|
|
149
|
-
```
|
|
150
|
-
|
|
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.
|
|
152
|
-
|
|
153
|
-
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if token exchange fails.
|
|
154
|
-
|
|
155
|
-
#### `onAuthChange`
|
|
156
|
-
|
|
157
|
-
```ts
|
|
158
|
-
onAuthChange(callback: AuthCallback): () => void
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
Subscribes to auth state changes (login, logout, token refresh, user updates). Returns an unsubscribe function. Also fires on cross-tab session changes. No-op on the server.
|
|
162
|
-
|
|
163
|
-
#### `requestPasswordRecovery`
|
|
164
|
-
|
|
165
|
-
```ts
|
|
166
|
-
requestPasswordRecovery(email: string): Promise<void>
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
Sends a password recovery email to the given address.
|
|
170
|
-
|
|
171
|
-
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` on network failure.
|
|
172
|
-
|
|
173
|
-
#### `confirmEmail`
|
|
174
|
-
|
|
175
|
-
```ts
|
|
176
|
-
confirmEmail(token: string): Promise<User>
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
Confirms an email address using the token from a confirmation email. Logs the user in on success.
|
|
180
|
-
|
|
181
|
-
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the token is invalid or expired.
|
|
182
|
-
|
|
183
|
-
#### `acceptInvite`
|
|
184
|
-
|
|
185
|
-
```ts
|
|
186
|
-
acceptInvite(token: string, password: string): Promise<User>
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
Accepts an invite token and sets a password for the new account. Logs the user in on success.
|
|
190
|
-
|
|
191
|
-
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the token is invalid or expired.
|
|
192
|
-
|
|
193
|
-
#### `verifyEmailChange`
|
|
194
|
-
|
|
195
|
-
```ts
|
|
196
|
-
verifyEmailChange(token: string): Promise<User>
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
Verifies an email change using the token from a verification email.
|
|
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
|
-
|
|
213
|
-
#### `updateUser`
|
|
214
|
-
|
|
215
|
-
```ts
|
|
216
|
-
updateUser(updates: Record<string, unknown>): Promise<User>
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
Updates the current user's metadata or credentials. Requires an active session.
|
|
220
|
-
|
|
221
|
-
**Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if no user is logged in, or the update fails.
|
|
93
|
+
Fetches your project's Identity settings (enabled providers, autoconfirm, signup disabled). Throws `MissingIdentityError` if not configured; throws `AuthError` if the endpoint is unreachable.
|
|
222
94
|
|
|
223
95
|
### Types
|
|
224
96
|
|
|
@@ -274,33 +146,6 @@ interface AppMetadata {
|
|
|
274
146
|
}
|
|
275
147
|
```
|
|
276
148
|
|
|
277
|
-
#### `AuthEvent`
|
|
278
|
-
|
|
279
|
-
```ts
|
|
280
|
-
type AuthEvent = 'login' | 'logout' | 'token_refresh' | 'user_updated'
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
#### `AuthCallback`
|
|
284
|
-
|
|
285
|
-
```ts
|
|
286
|
-
type AuthCallback = (event: AuthEvent, user: User | null) => void
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
#### `CallbackResult`
|
|
290
|
-
|
|
291
|
-
```ts
|
|
292
|
-
interface CallbackResult {
|
|
293
|
-
type: 'oauth' | 'confirmation' | 'recovery' | 'invite' | 'email_change'
|
|
294
|
-
user: User | null
|
|
295
|
-
token?: string
|
|
296
|
-
}
|
|
297
|
-
```
|
|
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
|
-
|
|
304
149
|
### Errors
|
|
305
150
|
|
|
306
151
|
#### `AuthError`
|
|
@@ -320,107 +165,6 @@ class MissingIdentityError extends Error {}
|
|
|
320
165
|
|
|
321
166
|
Thrown when Identity is not configured in the current environment.
|
|
322
167
|
|
|
323
|
-
## Guides
|
|
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
|
-
|
|
380
|
-
### Password recovery
|
|
381
|
-
|
|
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.
|
|
383
|
-
|
|
384
|
-
**Step by step:**
|
|
385
|
-
|
|
386
|
-
```ts
|
|
387
|
-
import { requestPasswordRecovery, handleAuthCallback, updateUser } from '@netlify/identity'
|
|
388
|
-
|
|
389
|
-
// 1. Send recovery email (e.g., from a "forgot password" form)
|
|
390
|
-
await requestPasswordRecovery('jane@example.com')
|
|
391
|
-
|
|
392
|
-
// 2-3. On page load, handle the callback
|
|
393
|
-
const result = await handleAuthCallback()
|
|
394
|
-
|
|
395
|
-
if (result?.type === 'recovery') {
|
|
396
|
-
// 4. User is now logged in. Show your "set new password" form.
|
|
397
|
-
// When they submit:
|
|
398
|
-
const newPassword = document.getElementById('new-password').value
|
|
399
|
-
await updateUser({ password: newPassword })
|
|
400
|
-
}
|
|
401
|
-
```
|
|
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
|
-
|
|
424
168
|
## License
|
|
425
169
|
|
|
426
170
|
MIT
|