next-sanctum 0.1.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/LICENSE +21 -0
- package/README.md +647 -0
- package/dist/actions.cjs +236 -0
- package/dist/actions.d.cts +81 -0
- package/dist/actions.d.ts +81 -0
- package/dist/actions.js +228 -0
- package/dist/index.cjs +1395 -0
- package/dist/index.d.cts +508 -0
- package/dist/index.d.ts +508 -0
- package/dist/index.js +1379 -0
- package/dist/proxy.cjs +49 -0
- package/dist/proxy.d.cts +29 -0
- package/dist/proxy.d.ts +29 -0
- package/dist/proxy.js +47 -0
- package/dist/server.cjs +358 -0
- package/dist/server.d.cts +78 -0
- package/dist/server.d.ts +78 -0
- package/dist/server.js +353 -0
- package/package.json +140 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Error types for next-sanctum. All failures are normalized to `SanctumError`
|
|
6
|
+
* so consumers can handle them consistently (see plan §10: errors must not leak).
|
|
7
|
+
*/
|
|
8
|
+
type SanctumErrorKind = "config" | "network" | "unauthorized" | "forbidden" | "csrf" | "validation" | "http" | "unknown";
|
|
9
|
+
interface SanctumErrorOptions {
|
|
10
|
+
kind: SanctumErrorKind;
|
|
11
|
+
status?: number;
|
|
12
|
+
/** The already-parsed response body (JSON when possible). */
|
|
13
|
+
data?: unknown;
|
|
14
|
+
cause?: unknown;
|
|
15
|
+
}
|
|
16
|
+
/** Base error for all module failures. */
|
|
17
|
+
declare class SanctumError extends Error {
|
|
18
|
+
readonly kind: SanctumErrorKind;
|
|
19
|
+
readonly status?: number;
|
|
20
|
+
readonly data?: unknown;
|
|
21
|
+
constructor(message: string, options: SanctumErrorOptions);
|
|
22
|
+
}
|
|
23
|
+
/** Invalid configuration — fail-fast on init (see resolveConfig). */
|
|
24
|
+
declare class ConfigError extends SanctumError {
|
|
25
|
+
constructor(message: string, cause?: unknown);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* HTTP 422 from Laravel. Exposes field errors (`{ field: string[] }`) so
|
|
29
|
+
* consumers can map them to their forms.
|
|
30
|
+
*/
|
|
31
|
+
declare class ValidationError extends SanctumError {
|
|
32
|
+
readonly errors: Record<string, string[]>;
|
|
33
|
+
constructor(message: string, errors: Record<string, string[]>, data?: unknown);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Public & internal type surface of next-sanctum.
|
|
38
|
+
* TypeScript-first, generic User model, no public `any`.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
type DeepPartial<T> = {
|
|
42
|
+
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
|
|
43
|
+
};
|
|
44
|
+
/** Authentication mode. `cookie` = CSRF/SPA (default), `token` = Bearer. */
|
|
45
|
+
type AuthMode = "cookie" | "token";
|
|
46
|
+
/** 0 silent · 1 error · 2 warn · 3 info (default) · 4 debug · 5 verbose. */
|
|
47
|
+
type LogLevel = 0 | 1 | 2 | 3 | 4 | 5;
|
|
48
|
+
/** Generic user shape; consumers cast it via the generic on the client/hook. */
|
|
49
|
+
type SanctumUser = Record<string, unknown>;
|
|
50
|
+
interface TwoFactorEndpoints {
|
|
51
|
+
challenge: string;
|
|
52
|
+
enable: string;
|
|
53
|
+
confirm: string;
|
|
54
|
+
disable: string;
|
|
55
|
+
qrCode: string;
|
|
56
|
+
secretKey: string;
|
|
57
|
+
recoveryCodes: string;
|
|
58
|
+
}
|
|
59
|
+
interface PasskeyEndpoints {
|
|
60
|
+
loginOptions: string;
|
|
61
|
+
login: string;
|
|
62
|
+
confirmOptions: string;
|
|
63
|
+
confirm: string;
|
|
64
|
+
registerOptions: string;
|
|
65
|
+
register: string;
|
|
66
|
+
/** Base path; the passkey id is appended (`${delete}/${id}`). */
|
|
67
|
+
delete: string;
|
|
68
|
+
}
|
|
69
|
+
interface SessionEndpoints {
|
|
70
|
+
list: string;
|
|
71
|
+
logoutOthers: string;
|
|
72
|
+
logout: string;
|
|
73
|
+
}
|
|
74
|
+
interface SanctumEndpoints {
|
|
75
|
+
csrf: string;
|
|
76
|
+
login: string;
|
|
77
|
+
logout: string;
|
|
78
|
+
user: string;
|
|
79
|
+
register: string;
|
|
80
|
+
forgotPassword: string;
|
|
81
|
+
resetPassword: string;
|
|
82
|
+
emailVerificationNotification: string;
|
|
83
|
+
/** Base path for completing verification; `/{id}/{hash}` is appended. */
|
|
84
|
+
verifyEmail: string;
|
|
85
|
+
confirmPassword: string;
|
|
86
|
+
confirmedPasswordStatus: string;
|
|
87
|
+
profileInformation: string;
|
|
88
|
+
updatePassword: string;
|
|
89
|
+
twoFactor: TwoFactorEndpoints;
|
|
90
|
+
passkeys: PasskeyEndpoints;
|
|
91
|
+
sessions: SessionEndpoints;
|
|
92
|
+
}
|
|
93
|
+
interface TwoFactorFeature {
|
|
94
|
+
/** Require a code confirmation step after enabling (default true). */
|
|
95
|
+
confirm?: boolean;
|
|
96
|
+
/** Require password confirmation before managing 2FA (default true). */
|
|
97
|
+
confirmPassword?: boolean;
|
|
98
|
+
}
|
|
99
|
+
interface PasskeysFeature {
|
|
100
|
+
confirmPassword?: boolean;
|
|
101
|
+
}
|
|
102
|
+
interface FeatureFlags {
|
|
103
|
+
registration?: boolean;
|
|
104
|
+
resetPasswords?: boolean;
|
|
105
|
+
emailVerification?: boolean;
|
|
106
|
+
updateProfileInformation?: boolean;
|
|
107
|
+
updatePasswords?: boolean;
|
|
108
|
+
twoFactorAuthentication?: boolean | TwoFactorFeature;
|
|
109
|
+
passkeys?: boolean | PasskeysFeature;
|
|
110
|
+
/** v1.1 — not yet implemented. */
|
|
111
|
+
deviceSessions?: boolean;
|
|
112
|
+
}
|
|
113
|
+
interface CsrfConfig {
|
|
114
|
+
cookie: string;
|
|
115
|
+
header: string;
|
|
116
|
+
}
|
|
117
|
+
interface RedirectConfig {
|
|
118
|
+
onLogin: string;
|
|
119
|
+
onLogout: string;
|
|
120
|
+
onAuthOnly: string;
|
|
121
|
+
onGuestOnly: string;
|
|
122
|
+
keepRequestedRoute: boolean;
|
|
123
|
+
}
|
|
124
|
+
interface SanctumEventMap<TUser = SanctumUser> {
|
|
125
|
+
init: {
|
|
126
|
+
user: TUser | null;
|
|
127
|
+
};
|
|
128
|
+
login: {
|
|
129
|
+
user: TUser;
|
|
130
|
+
};
|
|
131
|
+
logout: Record<string, never>;
|
|
132
|
+
refresh: {
|
|
133
|
+
user: TUser | null;
|
|
134
|
+
};
|
|
135
|
+
"two-factor-required": Record<string, never>;
|
|
136
|
+
error: {
|
|
137
|
+
error: SanctumError;
|
|
138
|
+
};
|
|
139
|
+
redirect: {
|
|
140
|
+
to: string;
|
|
141
|
+
reason: string;
|
|
142
|
+
};
|
|
143
|
+
request: {
|
|
144
|
+
url: string;
|
|
145
|
+
init: RequestInit;
|
|
146
|
+
};
|
|
147
|
+
response: {
|
|
148
|
+
url: string;
|
|
149
|
+
response: Response;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
type SanctumEventName = keyof SanctumEventMap;
|
|
153
|
+
type SanctumEventHandler<TUser = SanctumUser, K extends SanctumEventName = SanctumEventName> = (payload: SanctumEventMap<TUser>[K]) => void;
|
|
154
|
+
type RequestInterceptor = (request: Request) => Request | Promise<Request>;
|
|
155
|
+
type ResponseInterceptor = (response: Response, request: Request) => Response | Promise<Response>;
|
|
156
|
+
interface Interceptors {
|
|
157
|
+
request?: RequestInterceptor[];
|
|
158
|
+
response?: ResponseInterceptor[];
|
|
159
|
+
}
|
|
160
|
+
/** Bearer token storage interface (token mode). Swappable. */
|
|
161
|
+
interface SanctumTokenStorage {
|
|
162
|
+
get(): Promise<string | null> | string | null;
|
|
163
|
+
set(token: string): Promise<void> | void;
|
|
164
|
+
remove(): Promise<void> | void;
|
|
165
|
+
}
|
|
166
|
+
/** Config provided by the consumer. Only `baseUrl` is required. */
|
|
167
|
+
interface SanctumConfig {
|
|
168
|
+
baseUrl: string;
|
|
169
|
+
mode?: AuthMode;
|
|
170
|
+
/** App origin for the Referer header (default: window.location.origin when available). */
|
|
171
|
+
origin?: string;
|
|
172
|
+
features?: FeatureFlags;
|
|
173
|
+
endpoints?: DeepPartial<SanctumEndpoints>;
|
|
174
|
+
csrf?: Partial<CsrfConfig>;
|
|
175
|
+
redirect?: Partial<RedirectConfig>;
|
|
176
|
+
logLevel?: LogLevel;
|
|
177
|
+
/** Fetch the user on init (default true). */
|
|
178
|
+
initialRequest?: boolean;
|
|
179
|
+
/** Retry once on a CSRF failure (419). Defaults to true for cookie mode. */
|
|
180
|
+
retryOnCsrfMismatch?: boolean;
|
|
181
|
+
/** Token storage (token mode). Default MemoryStorage. */
|
|
182
|
+
storage?: SanctumTokenStorage;
|
|
183
|
+
interceptors?: Interceptors;
|
|
184
|
+
/** Lifecycle event handlers (a declarative alternative to emitter.on). */
|
|
185
|
+
events?: Partial<{
|
|
186
|
+
[K in SanctumEventName]: SanctumEventHandler<SanctumUser, K>;
|
|
187
|
+
}>;
|
|
188
|
+
/** When a request returns 401: clear state + redirect to this path (false = don't). */
|
|
189
|
+
redirectIfUnauthenticated?: string | false;
|
|
190
|
+
/** Custom fetch implementation (default: globalThis.fetch). */
|
|
191
|
+
fetch?: typeof fetch;
|
|
192
|
+
}
|
|
193
|
+
/** Config after resolveConfig: all defaults filled in. */
|
|
194
|
+
interface ResolvedSanctumConfig {
|
|
195
|
+
baseUrl: string;
|
|
196
|
+
mode: AuthMode;
|
|
197
|
+
origin: string | undefined;
|
|
198
|
+
features: Required<Omit<FeatureFlags, "twoFactorAuthentication" | "passkeys">> & {
|
|
199
|
+
twoFactorAuthentication: false | Required<TwoFactorFeature>;
|
|
200
|
+
passkeys: false | Required<PasskeysFeature>;
|
|
201
|
+
};
|
|
202
|
+
endpoints: SanctumEndpoints;
|
|
203
|
+
csrf: CsrfConfig;
|
|
204
|
+
redirect: RedirectConfig;
|
|
205
|
+
logLevel: LogLevel;
|
|
206
|
+
initialRequest: boolean;
|
|
207
|
+
retryOnCsrfMismatch: boolean;
|
|
208
|
+
storage: SanctumTokenStorage | undefined;
|
|
209
|
+
interceptors: Required<Interceptors>;
|
|
210
|
+
events: Partial<{
|
|
211
|
+
[K in SanctumEventName]: SanctumEventHandler<SanctumUser, K>;
|
|
212
|
+
}>;
|
|
213
|
+
redirectIfUnauthenticated: string | false;
|
|
214
|
+
fetch: typeof fetch;
|
|
215
|
+
}
|
|
216
|
+
interface LoginCredentials {
|
|
217
|
+
email?: string;
|
|
218
|
+
/** Supports backends that use `username` (config/fortify.php). */
|
|
219
|
+
username?: string;
|
|
220
|
+
password: string;
|
|
221
|
+
remember?: boolean;
|
|
222
|
+
[key: string]: unknown;
|
|
223
|
+
}
|
|
224
|
+
/** Discriminated login result — consumers MUST check `status` (so 2FA isn't missed). */
|
|
225
|
+
type LoginResult<TUser = SanctumUser> = {
|
|
226
|
+
status: "authenticated";
|
|
227
|
+
user: TUser;
|
|
228
|
+
} | {
|
|
229
|
+
status: "two-factor-required";
|
|
230
|
+
};
|
|
231
|
+
interface TwoFactorChallengePayload {
|
|
232
|
+
code?: string;
|
|
233
|
+
recovery_code?: string;
|
|
234
|
+
}
|
|
235
|
+
interface RegisterPayload {
|
|
236
|
+
name?: string;
|
|
237
|
+
email?: string;
|
|
238
|
+
username?: string;
|
|
239
|
+
password?: string;
|
|
240
|
+
password_confirmation?: string;
|
|
241
|
+
[key: string]: unknown;
|
|
242
|
+
}
|
|
243
|
+
interface ForgotPasswordPayload {
|
|
244
|
+
email: string;
|
|
245
|
+
[key: string]: unknown;
|
|
246
|
+
}
|
|
247
|
+
/** Params from a Fortify email-verification link (`/email/verify/{id}/{hash}?expires&signature`). */
|
|
248
|
+
interface VerifyEmailPayload {
|
|
249
|
+
id: string | number;
|
|
250
|
+
hash: string;
|
|
251
|
+
expires: string | number;
|
|
252
|
+
signature: string;
|
|
253
|
+
}
|
|
254
|
+
interface ResetPasswordPayload {
|
|
255
|
+
token: string;
|
|
256
|
+
email: string;
|
|
257
|
+
password: string;
|
|
258
|
+
password_confirmation: string;
|
|
259
|
+
[key: string]: unknown;
|
|
260
|
+
}
|
|
261
|
+
interface ConfirmPasswordPayload {
|
|
262
|
+
password: string;
|
|
263
|
+
}
|
|
264
|
+
interface UpdatePasswordPayload {
|
|
265
|
+
current_password: string;
|
|
266
|
+
password: string;
|
|
267
|
+
password_confirmation: string;
|
|
268
|
+
[key: string]: unknown;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
interface SanctumRequestInit extends Omit<RequestInit, "body"> {
|
|
272
|
+
body?: BodyInit | null;
|
|
273
|
+
/** Shortcut: serialize to JSON + set content-type automatically. */
|
|
274
|
+
json?: unknown;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
interface SanctumClient {
|
|
278
|
+
readonly config: ResolvedSanctumConfig;
|
|
279
|
+
/** Ensure the CSRF cookie is present (cookie mode). No-op in token mode. */
|
|
280
|
+
ensureCsrf(force?: boolean): Promise<void>;
|
|
281
|
+
/** Authenticated request → raw Response. Throws SanctumError on non-2xx. */
|
|
282
|
+
raw(path: string, init?: SanctumRequestInit): Promise<Response>;
|
|
283
|
+
/** Authenticated request → parsed JSON body. */
|
|
284
|
+
request<T>(path: string, init?: SanctumRequestInit): Promise<T>;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
interface SanctumProviderProps<TUser = SanctumUser> {
|
|
288
|
+
config: SanctumConfig;
|
|
289
|
+
/**
|
|
290
|
+
* User prefetched on the server (getUser()). Seeds state to avoid hydration mismatch.
|
|
291
|
+
* `undefined` = not prefetched (fetched on the client when initialRequest is enabled);
|
|
292
|
+
* `null` = the server confirmed the user is not logged in.
|
|
293
|
+
*/
|
|
294
|
+
initialUser?: TUser | null;
|
|
295
|
+
children: ReactNode;
|
|
296
|
+
}
|
|
297
|
+
declare function SanctumProvider<TUser = SanctumUser>({ config, initialUser, children, }: SanctumProviderProps<TUser>): react.JSX.Element;
|
|
298
|
+
|
|
299
|
+
interface UseAuthResult<TUser = SanctumUser> {
|
|
300
|
+
user: TUser | null;
|
|
301
|
+
isAuthenticated: boolean;
|
|
302
|
+
isLoading: boolean;
|
|
303
|
+
login: (credentials: LoginCredentials) => Promise<LoginResult<TUser>>;
|
|
304
|
+
logout: () => Promise<void>;
|
|
305
|
+
refresh: () => Promise<TUser | null>;
|
|
306
|
+
register: (payload: RegisterPayload) => Promise<void>;
|
|
307
|
+
forgotPassword: (payload: ForgotPasswordPayload) => Promise<void>;
|
|
308
|
+
resetPassword: (payload: ResetPasswordPayload) => Promise<void>;
|
|
309
|
+
confirmPassword: (payload: ConfirmPasswordPayload) => Promise<void>;
|
|
310
|
+
confirmedPasswordStatus: () => Promise<boolean>;
|
|
311
|
+
updatePassword: (payload: UpdatePasswordPayload) => Promise<void>;
|
|
312
|
+
updateProfile: (payload: Record<string, unknown>) => Promise<void>;
|
|
313
|
+
resendEmailVerification: () => Promise<void>;
|
|
314
|
+
verifyEmail: (payload: VerifyEmailPayload) => Promise<void>;
|
|
315
|
+
}
|
|
316
|
+
/** Authentication & account state and actions (login, register, password, profile, email verification). */
|
|
317
|
+
declare function useAuth<TUser = SanctumUser>(): UseAuthResult<TUser>;
|
|
318
|
+
|
|
319
|
+
/** Reactive user (null when not authenticated). Cast via the generic. */
|
|
320
|
+
declare function useUser<TUser = SanctumUser>(): TUser | null;
|
|
321
|
+
|
|
322
|
+
interface UseApiOptions extends SanctumRequestInit {
|
|
323
|
+
/** Run automatically on mount / when the path changes (default true). */
|
|
324
|
+
enabled?: boolean;
|
|
325
|
+
}
|
|
326
|
+
interface UseApiResult<T> {
|
|
327
|
+
data: T | undefined;
|
|
328
|
+
error: SanctumError | null;
|
|
329
|
+
isLoading: boolean;
|
|
330
|
+
refetch: () => Promise<void>;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Authenticated fetch on the client. Minimal but sufficient for most cases;
|
|
334
|
+
* SWR/TanStack Query adapters can be built on top of `useSanctumContext().client`.
|
|
335
|
+
*/
|
|
336
|
+
declare function useApi<T = unknown>(path: string, options?: UseApiOptions): UseApiResult<T>;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* The authenticated HTTP client for imperative requests — i.e. CRUD beyond auth
|
|
340
|
+
* (create/update/delete or on-demand reads). `client.request<T>(path, { method, json })`
|
|
341
|
+
* returns parsed JSON; `client.raw(...)` returns the Response. It automatically attaches
|
|
342
|
+
* CSRF (cookie mode) or Bearer (token mode), the base URL, and credentials.
|
|
343
|
+
*/
|
|
344
|
+
declare function useClient(): SanctumClient;
|
|
345
|
+
|
|
346
|
+
interface ResourceClient<T = unknown, TList = T[]> {
|
|
347
|
+
/** `GET {base}` */
|
|
348
|
+
list(init?: SanctumRequestInit): Promise<TList>;
|
|
349
|
+
/** `GET {base}/{id}` */
|
|
350
|
+
get(id: string | number, init?: SanctumRequestInit): Promise<T>;
|
|
351
|
+
/** `POST {base}` */
|
|
352
|
+
create(data: unknown, init?: SanctumRequestInit): Promise<T>;
|
|
353
|
+
/** `PUT {base}/{id}` */
|
|
354
|
+
update(id: string | number, data: unknown, init?: SanctumRequestInit): Promise<T>;
|
|
355
|
+
/** `PATCH {base}/{id}` */
|
|
356
|
+
patch(id: string | number, data: unknown, init?: SanctumRequestInit): Promise<T>;
|
|
357
|
+
/** `DELETE {base}/{id}` */
|
|
358
|
+
delete(id: string | number, init?: SanctumRequestInit): Promise<void>;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* A typed REST resource over the authenticated client — convenience sugar for CRUD.
|
|
362
|
+
* Credentials (CSRF/cookie or Bearer) are attached automatically. `TList` defaults to
|
|
363
|
+
* `T[]`; set it (e.g. `{ data: T[]; meta: … }`) for paginated Laravel resources.
|
|
364
|
+
*
|
|
365
|
+
* ```ts
|
|
366
|
+
* const posts = useResource<Post>("/api/posts")
|
|
367
|
+
* await posts.list() // GET /api/posts
|
|
368
|
+
* await posts.create({ title }) // POST /api/posts
|
|
369
|
+
* await posts.update(1, { title })// PUT /api/posts/1
|
|
370
|
+
* await posts.delete(1) // DELETE /api/posts/1
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
declare function useResource<T = unknown, TList = T[]>(basePath: string): ResourceClient<T, TList>;
|
|
374
|
+
|
|
375
|
+
interface UseMutationOptions<TData, TVars> {
|
|
376
|
+
/** Runs just before the request. Return `false` to cancel it. */
|
|
377
|
+
onBefore?: (vars: TVars) => boolean | void | Promise<boolean | void>;
|
|
378
|
+
onSuccess?: (data: TData, vars: TVars) => void;
|
|
379
|
+
onError?: (error: SanctumError, vars: TVars) => void;
|
|
380
|
+
/** Always runs after success or error (not when cancelled in onBefore). */
|
|
381
|
+
onFinish?: (vars: TVars) => void;
|
|
382
|
+
}
|
|
383
|
+
interface UseMutationResult<TData, TVars> {
|
|
384
|
+
/** Fire-and-forget (rejections are swallowed; read them from `error`). */
|
|
385
|
+
mutate: (vars: TVars) => void;
|
|
386
|
+
/** Awaitable; resolves with the data or throws the SanctumError. */
|
|
387
|
+
mutateAsync: (vars: TVars) => Promise<TData>;
|
|
388
|
+
isPending: boolean;
|
|
389
|
+
error: SanctumError | null;
|
|
390
|
+
data: TData | undefined;
|
|
391
|
+
reset: () => void;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* A lightweight mutation hook (Inertia-style lifecycle) for imperative requests —
|
|
395
|
+
* pair it with `useClient` / `useResource`. Manages `isPending` / `error` / `data`
|
|
396
|
+
* and fires `onBefore` / `onSuccess` / `onError` / `onFinish`.
|
|
397
|
+
*
|
|
398
|
+
* ```tsx
|
|
399
|
+
* const { request } = useClient()
|
|
400
|
+
* const create = useMutation(
|
|
401
|
+
* (vars: { title: string }) => request<Post>("/api/posts", { method: "POST", json: vars }),
|
|
402
|
+
* { onSuccess: (post) => toast("Created"), onError: (e) => toast(e.message) },
|
|
403
|
+
* )
|
|
404
|
+
* <button disabled={create.isPending} onClick={() => create.mutate({ title })}>Save</button>
|
|
405
|
+
* ```
|
|
406
|
+
*/
|
|
407
|
+
declare function useMutation<TData = unknown, TVars = void>(mutationFn: (vars: TVars) => Promise<TData>, options?: UseMutationOptions<TData, TVars>): UseMutationResult<TData, TVars>;
|
|
408
|
+
|
|
409
|
+
interface TwoFactorApi {
|
|
410
|
+
/** Complete the 2FA login (`POST /two-factor-challenge`, code / recovery_code). */
|
|
411
|
+
challenge(payload: TwoFactorChallengePayload): Promise<void>;
|
|
412
|
+
/** Enable 2FA (`POST /user/two-factor-authentication`). Requires password confirmation. */
|
|
413
|
+
enable(): Promise<void>;
|
|
414
|
+
/** Confirm 2FA with a code (`POST /user/confirmed-two-factor-authentication`). */
|
|
415
|
+
confirm(code: string): Promise<void>;
|
|
416
|
+
/** Disable 2FA (`DELETE /user/two-factor-authentication`). */
|
|
417
|
+
disable(): Promise<void>;
|
|
418
|
+
/** QR code SVG (`GET /user/two-factor-qr-code`). */
|
|
419
|
+
getQrCode(): Promise<{
|
|
420
|
+
svg: string;
|
|
421
|
+
}>;
|
|
422
|
+
/** Secret key (`GET /user/two-factor-secret-key`). */
|
|
423
|
+
getSecretKey(): Promise<{
|
|
424
|
+
secretKey: string;
|
|
425
|
+
}>;
|
|
426
|
+
/** Recovery codes (`GET /user/two-factor-recovery-codes`). */
|
|
427
|
+
getRecoveryCodes(): Promise<string[]>;
|
|
428
|
+
/** Regenerate recovery codes (`POST /user/two-factor-recovery-codes`). */
|
|
429
|
+
regenerateRecoveryCodes(): Promise<void>;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Two-factor API (Fortify): challenge during login + management (enable/confirm/disable,
|
|
434
|
+
* QR, recovery codes). `challenge()` automatically refreshes the identity on success.
|
|
435
|
+
*/
|
|
436
|
+
declare function useTwoFactor(): TwoFactorApi;
|
|
437
|
+
|
|
438
|
+
interface PasskeyRegistration {
|
|
439
|
+
id: string;
|
|
440
|
+
name: string;
|
|
441
|
+
}
|
|
442
|
+
interface PasskeysApi {
|
|
443
|
+
/** Whether the browser supports passkeys (WebAuthn). */
|
|
444
|
+
isSupported(): Promise<boolean>;
|
|
445
|
+
/** Register a new passkey for the authenticated user. */
|
|
446
|
+
register(name: string): Promise<PasskeyRegistration>;
|
|
447
|
+
/** Passwordless login via passkey (auto-refreshes identity on success). */
|
|
448
|
+
login(): Promise<void>;
|
|
449
|
+
/** Confirm the session password via passkey. */
|
|
450
|
+
confirmPassword(): Promise<void>;
|
|
451
|
+
/** Delete a passkey (`DELETE /user/passkeys/{id}`). */
|
|
452
|
+
delete(id: string): Promise<void>;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Passkeys API (interop with @laravel/passkeys). `login()` automatically refreshes
|
|
457
|
+
* the identity on success. Requires the `@laravel/passkeys` package in the consumer.
|
|
458
|
+
*/
|
|
459
|
+
declare function usePasskeys(): PasskeysApi;
|
|
460
|
+
|
|
461
|
+
/** In-memory token storage (lost on reload). Default for token mode. */
|
|
462
|
+
declare class MemoryStorage implements SanctumTokenStorage {
|
|
463
|
+
private token;
|
|
464
|
+
get(): string | null;
|
|
465
|
+
set(token: string): void;
|
|
466
|
+
remove(): void;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** Token storage in localStorage. OPT-IN — vulnerable to XSS (see PRD §12). */
|
|
470
|
+
declare class LocalStorage implements SanctumTokenStorage {
|
|
471
|
+
private readonly key;
|
|
472
|
+
constructor(key?: string);
|
|
473
|
+
private warnOnce;
|
|
474
|
+
get(): string | null;
|
|
475
|
+
set(token: string): void;
|
|
476
|
+
remove(): void;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
interface CookieStorageOptions {
|
|
480
|
+
name?: string;
|
|
481
|
+
/** Cookie lifetime (seconds). Default 14 days. */
|
|
482
|
+
maxAge?: number;
|
|
483
|
+
/** Default `Strict` for a credential cookie. */
|
|
484
|
+
sameSite?: "Lax" | "Strict" | "None";
|
|
485
|
+
secure?: boolean;
|
|
486
|
+
path?: string;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Cookie-based token storage written by the client. NOTE: cookies written by
|
|
490
|
+
* JS CANNOT be HttpOnly. For true HttpOnly, set the cookie via a Route Handler/Server
|
|
491
|
+
* Action and then attach the Bearer on the server (catch-all proxy). This storage is for
|
|
492
|
+
* simple persistence, NOT a replacement for HttpOnly.
|
|
493
|
+
*/
|
|
494
|
+
declare class CookieTokenStorage implements SanctumTokenStorage {
|
|
495
|
+
private readonly name;
|
|
496
|
+
private readonly maxAge;
|
|
497
|
+
private readonly sameSite;
|
|
498
|
+
private readonly secure;
|
|
499
|
+
private readonly path;
|
|
500
|
+
constructor(options?: CookieStorageOptions);
|
|
501
|
+
private warnOnce;
|
|
502
|
+
get(): string | null;
|
|
503
|
+
set(token: string): void;
|
|
504
|
+
remove(): void;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export { ConfigError, CookieTokenStorage, LocalStorage, MemoryStorage, SanctumError, SanctumProvider, ValidationError, useApi, useAuth, useClient, useMutation, usePasskeys, useResource, useTwoFactor, useUser };
|
|
508
|
+
export type { AuthMode, ConfirmPasswordPayload, CookieStorageOptions, FeatureFlags, ForgotPasswordPayload, Interceptors, LogLevel, LoginCredentials, LoginResult, PasskeyRegistration, PasskeysApi, RegisterPayload, RequestInterceptor, ResetPasswordPayload, ResourceClient, ResponseInterceptor, SanctumClient, SanctumConfig, SanctumEndpoints, SanctumErrorKind, SanctumProviderProps, SanctumRequestInit, SanctumTokenStorage, SanctumUser, TwoFactorApi, TwoFactorChallengePayload, UpdatePasswordPayload, UseApiOptions, UseApiResult, UseAuthResult, UseMutationOptions, UseMutationResult, VerifyEmailPayload };
|