@netlify/identity 0.1.1-alpha.9 → 0.2.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/dist/index.d.ts CHANGED
@@ -14,6 +14,21 @@ interface Settings {
14
14
  disableSignup: boolean;
15
15
  providers: Record<AuthProvider, boolean>;
16
16
  }
17
+ /**
18
+ * Fields accepted by {@link updateUser}. All fields are optional.
19
+ * Pass `data` to update user metadata (e.g., `{ data: { full_name: 'New Name' } }`).
20
+ */
21
+ interface UserUpdates {
22
+ email?: string;
23
+ password?: string;
24
+ data?: Record<string, unknown>;
25
+ [key: string]: unknown;
26
+ }
27
+ /**
28
+ * User metadata passed during signup (e.g., `{ full_name: 'Jane Doe' }`).
29
+ * Stored in the user's `user_metadata` field.
30
+ */
31
+ type SignupData = Record<string, unknown>;
17
32
 
18
33
  interface User {
19
34
  id: string;
@@ -29,12 +44,16 @@ interface User {
29
44
  }
30
45
  /**
31
46
  * Returns the currently authenticated user, or `null` if not logged in.
32
- * Synchronous. Never throws.
47
+ * Synchronous.
33
48
  *
34
49
  * In the browser, checks gotrue-js localStorage first. If no localStorage
35
50
  * session exists, falls back to decoding the `nf_jwt` cookie (set by
36
- * server-side login). This gives immediate synchronous read access without
37
- * waiting for async hydration via `hydrateSession()`.
51
+ * server-side login) and kicks off background hydration of the gotrue-js
52
+ * session so that subsequent operations (updateUser, logout, etc.) work.
53
+ *
54
+ * On the server in a Next.js App Router context, calls `headers()` from
55
+ * `next/headers` to opt the route into dynamic rendering. Without this,
56
+ * Next.js may statically cache the page at build time.
38
57
  */
39
58
  declare const getUser: () => User | null;
40
59
  /**
@@ -50,27 +69,83 @@ declare const isAuthenticated: () => boolean;
50
69
  */
51
70
  declare const getIdentityConfig: () => IdentityConfig | null;
52
71
  /**
53
- * Fetches the GoTrue `/settings` endpoint.
54
- * Throws `MissingIdentityError` if Identity is not configured.
55
- * Throws `AuthError` if the endpoint is unreachable.
72
+ * Fetches your project's Identity settings (enabled providers, autoconfirm, signup disabled).
73
+ *
74
+ * @throws {MissingIdentityError} If Identity is not configured.
75
+ * @throws {AuthError} If the endpoint is unreachable.
56
76
  */
57
77
  declare const getSettings: () => Promise<Settings>;
58
78
 
59
- type AuthEvent = 'login' | 'logout' | 'token_refresh' | 'user_updated';
60
-
79
+ declare const AUTH_EVENTS: {
80
+ readonly LOGIN: "login";
81
+ readonly LOGOUT: "logout";
82
+ readonly TOKEN_REFRESH: "token_refresh";
83
+ readonly USER_UPDATED: "user_updated";
84
+ readonly RECOVERY: "recovery";
85
+ };
86
+ type AuthEvent = (typeof AUTH_EVENTS)[keyof typeof AUTH_EVENTS];
61
87
  type AuthCallback = (event: AuthEvent, user: User | null) => void;
62
88
  /**
63
- * Subscribes to auth state changes (login, logout, token refresh, user updates).
64
- * Returns an unsubscribe function. No-op on the server.
89
+ * Subscribes to auth state changes (login, logout, token refresh, user updates,
90
+ * and recovery). Returns an unsubscribe function. No-op on the server.
91
+ *
92
+ * The `'recovery'` event fires when {@link handleAuthCallback} processes a
93
+ * password recovery token. The user is logged in but has not yet set a new
94
+ * password. Redirect them to a password reset form and call
95
+ * `updateUser({ password })` to complete the flow.
65
96
  */
66
97
  declare const onAuthChange: (callback: AuthCallback) => (() => void);
67
- /** Logs in with email and password. Works in both browser and server contexts. */
98
+
99
+ /**
100
+ * Logs in with email and password. Works in both browser and server contexts.
101
+ *
102
+ * On success, sets `nf_jwt` and `nf_refresh` cookies and returns the authenticated {@link User}.
103
+ * In the browser, also emits a `'login'` event via {@link onAuthChange}.
104
+ *
105
+ * @throws {AuthError} On invalid credentials, network failure, or missing Netlify runtime.
106
+ *
107
+ * @remarks
108
+ * In Next.js server actions, call `redirect()` **after** `login()` returns, not inside a
109
+ * try/catch. Next.js implements `redirect()` by throwing a special error; wrapping it in
110
+ * try/catch will swallow the redirect.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * // Next.js server action
115
+ * const user = await login(email, password)
116
+ * redirect('/dashboard') // after login, not inside try/catch
117
+ * ```
118
+ */
68
119
  declare const login: (email: string, password: string) => Promise<User>;
69
- /** Creates a new account. Emits 'login' if autoconfirm is enabled. Works in both browser and server contexts. */
70
- declare const signup: (email: string, password: string, data?: Record<string, unknown>) => Promise<User>;
71
- /** Logs out the current user and clears the session. Works in both browser and server contexts. */
120
+ /**
121
+ * Creates a new account. Works in both browser and server contexts.
122
+ *
123
+ * If autoconfirm is enabled in your Identity settings, the user is logged in immediately:
124
+ * cookies are set and a `'login'` event is emitted. If autoconfirm is **disabled** (the default),
125
+ * the user receives a confirmation email and must click the link before they can log in.
126
+ * In that case, no cookies are set and no auth event is emitted.
127
+ *
128
+ * @throws {AuthError} On duplicate email, validation failure, network error, or missing Netlify runtime.
129
+ */
130
+ declare const signup: (email: string, password: string, data?: SignupData) => Promise<User>;
131
+ /**
132
+ * Logs out the current user and clears the session. Works in both browser and server contexts.
133
+ *
134
+ * Always deletes `nf_jwt` and `nf_refresh` cookies, even if the server-side token
135
+ * invalidation request fails. In the browser, emits a `'logout'` event via {@link onAuthChange}.
136
+ *
137
+ * @throws {AuthError} On missing Netlify runtime (server) or logout failure (browser).
138
+ */
72
139
  declare const logout: () => Promise<void>;
73
- /** Redirects to an OAuth provider. Always throws (the page navigates away). Browser only. */
140
+ /**
141
+ * Initiates an OAuth login by redirecting to the given provider (e.g., `'google'`, `'github'`).
142
+ * The page navigates away; this function never returns normally. Browser only.
143
+ *
144
+ * After the provider redirects back, call {@link handleAuthCallback} on page load
145
+ * to complete the login and obtain the {@link User}.
146
+ *
147
+ * @throws {AuthError} If called on the server.
148
+ */
74
149
  declare const oauthLogin: (provider: string) => never;
75
150
  interface CallbackResult {
76
151
  type: 'oauth' | 'confirmation' | 'recovery' | 'invite' | 'email_change';
@@ -81,8 +156,28 @@ interface CallbackResult {
81
156
  * Processes the URL hash after an OAuth redirect, email confirmation, password
82
157
  * recovery, invite acceptance, or email change. Call on page load. Browser only.
83
158
  * Returns `null` if the hash contains no auth parameters.
159
+ *
160
+ * Call this early in your app's initialization (e.g., in a layout component or
161
+ * root loader), **not** inside a route that requires authentication, because
162
+ * the callback URL must match the page where this function runs.
163
+ *
164
+ * For recovery callbacks (`result.type === 'recovery'`), the user is logged in
165
+ * but has **not** set a new password yet. Your app must check the result type
166
+ * and redirect to a password form that calls `updateUser({ password })`.
167
+ * A `'recovery'` event (not `'login'`) is emitted via {@link onAuthChange}.
168
+ *
169
+ * @throws {AuthError} If the callback token is invalid or the verification request fails.
84
170
  */
85
171
  declare const handleAuthCallback: () => Promise<CallbackResult | null>;
172
+ /**
173
+ * Hydrates the browser-side gotrue-js session from server-set auth cookies.
174
+ * Call this on page load when using server-side login to enable browser
175
+ * account operations (updateUser, verifyEmailChange, etc.).
176
+ *
177
+ * No-op if a browser session already exists or no auth cookies are present.
178
+ * No-op on the server.
179
+ */
180
+ declare const hydrateSession: () => Promise<User | null>;
86
181
 
87
182
  declare class AuthError extends Error {
88
183
  name: string;
@@ -91,29 +186,52 @@ declare class AuthError extends Error {
91
186
  constructor(message: string, status?: number, options?: {
92
187
  cause?: unknown;
93
188
  });
189
+ static from(error: unknown): AuthError;
94
190
  }
95
191
  declare class MissingIdentityError extends Error {
96
192
  name: string;
97
193
  constructor(message?: string);
98
194
  }
99
195
 
100
- /** Sends a password recovery email to the given address. */
196
+ /**
197
+ * Sends a password recovery email to the given address.
198
+ *
199
+ * @throws {AuthError} On network failure or if the request is rejected.
200
+ */
101
201
  declare const requestPasswordRecovery: (email: string) => Promise<void>;
102
- /** Redeems a recovery token and sets a new password. Logs the user in on success. */
202
+ /**
203
+ * Redeems a recovery token and sets a new password. Logs the user in on success.
204
+ *
205
+ * @throws {AuthError} If the token is invalid, expired, or the update fails.
206
+ */
103
207
  declare const recoverPassword: (token: string, newPassword: string) => Promise<User>;
104
- /** Confirms an email address using the token from a confirmation email. Logs the user in on success. */
208
+ /**
209
+ * Confirms an email address using the token from a confirmation email. Logs the user in on success.
210
+ *
211
+ * @throws {AuthError} If the token is invalid or expired.
212
+ */
105
213
  declare const confirmEmail: (token: string) => Promise<User>;
106
- /** Accepts an invite token and sets a password for the new account. Logs the user in on success. */
214
+ /**
215
+ * Accepts an invite token and sets a password for the new account. Logs the user in on success.
216
+ *
217
+ * @throws {AuthError} If the token is invalid or expired.
218
+ */
107
219
  declare const acceptInvite: (token: string, password: string) => Promise<User>;
108
220
  /**
109
221
  * Verifies an email change using the token from a verification email.
110
- * Auto-hydrates from auth cookies if no browser session exists.
222
+ * Auto-hydrates from auth cookies if no browser session exists. Browser only.
223
+ *
224
+ * @throws {AuthError} If called on the server, no user is logged in, or the token is invalid.
111
225
  */
112
226
  declare const verifyEmailChange: (token: string) => Promise<User>;
113
227
  /**
114
- * Updates the current user's metadata or credentials.
228
+ * Updates the current user's email, password, or user metadata.
115
229
  * Auto-hydrates from auth cookies if no browser session exists.
230
+ *
231
+ * @param updates - Fields to update. Pass `email` or `password` to change credentials,
232
+ * or `data` to update user metadata (e.g., `{ data: { full_name: 'New Name' } }`).
233
+ * @throws {AuthError} If no user is logged in or the update fails.
116
234
  */
117
- declare const updateUser: (updates: Record<string, unknown>) => Promise<User>;
235
+ declare const updateUser: (updates: UserUpdates) => Promise<User>;
118
236
 
119
- export { type AppMetadata, type AuthCallback, AuthError, type AuthEvent, type AuthProvider, type CallbackResult, type IdentityConfig, MissingIdentityError, type Settings, type User, acceptInvite, confirmEmail, getIdentityConfig, getSettings, getUser, handleAuthCallback, isAuthenticated, login, logout, oauthLogin, onAuthChange, recoverPassword, requestPasswordRecovery, signup, updateUser, verifyEmailChange };
237
+ export { AUTH_EVENTS, type AppMetadata, type AuthCallback, AuthError, type AuthEvent, type AuthProvider, type CallbackResult, type IdentityConfig, MissingIdentityError, type Settings, type SignupData, type User, type UserUpdates, acceptInvite, confirmEmail, getIdentityConfig, getSettings, getUser, handleAuthCallback, hydrateSession, isAuthenticated, login, logout, oauthLogin, onAuthChange, recoverPassword, requestPasswordRecovery, signup, updateUser, verifyEmailChange };