@netlify/identity 0.1.1-alpha.0 → 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 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). Throws `MissingIdentityError` if not configured; throws `AuthError` if the endpoint is unreachable.
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. Browser only.
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. Emits `'login'` if autoconfirm is enabled. Browser only.
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. Browser only.
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. Always throws (the page navigates away). Browser only.
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,13 +168,7 @@ requestPasswordRecovery(email: string): Promise<void>
148
168
 
149
169
  Sends a password recovery email to the given address.
150
170
 
151
- #### `recoverPassword`
152
-
153
- ```ts
154
- recoverPassword(token: string, newPassword: string): Promise<User>
155
- ```
156
-
157
- Redeems a recovery token and sets a new password. Logs the user in on success.
171
+ **Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` on network failure.
158
172
 
159
173
  #### `confirmEmail`
160
174
 
@@ -164,6 +178,8 @@ confirmEmail(token: string): Promise<User>
164
178
 
165
179
  Confirms an email address using the token from a confirmation email. Logs the user in on success.
166
180
 
181
+ **Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the token is invalid or expired.
182
+
167
183
  #### `acceptInvite`
168
184
 
169
185
  ```ts
@@ -172,6 +188,8 @@ acceptInvite(token: string, password: string): Promise<User>
172
188
 
173
189
  Accepts an invite token and sets a password for the new account. Logs the user in on success.
174
190
 
191
+ **Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if the token is invalid or expired.
192
+
175
193
  #### `verifyEmailChange`
176
194
 
177
195
  ```ts
@@ -180,6 +198,18 @@ verifyEmailChange(token: string): Promise<User>
180
198
 
181
199
  Verifies an email change using the token from a verification email.
182
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
+
183
213
  #### `updateUser`
184
214
 
185
215
  ```ts
@@ -188,6 +218,8 @@ updateUser(updates: Record<string, unknown>): Promise<User>
188
218
 
189
219
  Updates the current user's metadata or credentials. Requires an active session.
190
220
 
221
+ **Throws:** `MissingIdentityError` if Identity is not configured. `AuthError` if no user is logged in, or the update fails.
222
+
191
223
  ### Types
192
224
 
193
225
  #### `User`
@@ -264,6 +296,11 @@ interface CallbackResult {
264
296
  }
265
297
  ```
266
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
+
267
304
  ### Errors
268
305
 
269
306
  #### `AuthError`
@@ -283,6 +320,107 @@ class MissingIdentityError extends Error {}
283
320
 
284
321
  Thrown when Identity is not configured in the current environment.
285
322
 
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
+
286
424
  ## License
287
425
 
288
426
  MIT