@fluixi/auth 1.0.0-alpha.100
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 +72 -0
- package/dist/client.cjs +219 -0
- package/dist/client.d.ts +177 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +291 -0
- package/dist/client.mjs +199 -0
- package/dist/cookies.cjs +43 -0
- package/dist/cookies.d.ts +7 -0
- package/dist/cookies.d.ts.map +1 -0
- package/dist/cookies.js +19 -0
- package/dist/cookies.mjs +22 -0
- package/dist/remote.cjs +52 -0
- package/dist/remote.d.ts +20 -0
- package/dist/remote.d.ts.map +1 -0
- package/dist/remote.js +30 -0
- package/dist/remote.mjs +31 -0
- package/dist/server.cjs +203 -0
- package/dist/server.d.ts +131 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +167 -0
- package/dist/server.mjs +183 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/dist/types.cjs +26 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +10 -0
- package/dist/types.mjs +5 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ibrahima Touré
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @fluixi/auth
|
|
2
|
+
|
|
3
|
+
Server-first auth for Fluixi — **native bindings over [Better Auth](https://better-auth.com)**.
|
|
4
|
+
Better Auth owns the security primitives (password hashing, sessions, CSRF, secure cookies,
|
|
5
|
+
OAuth, 2FA/TOTP, organizations, RBAC, DB adapters — audited and maintained). This package
|
|
6
|
+
owns the *Fluixi integration* Better Auth can't provide: request-context server helpers,
|
|
7
|
+
SSR-seeded reactive hooks, middleware wiring, and route protection.
|
|
8
|
+
|
|
9
|
+
`better-auth` is a **peer dependency** — the bindings consume it structurally, so the app owns the version.
|
|
10
|
+
|
|
11
|
+
## Server (`@fluixi/auth/server`)
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// auth.config.ts
|
|
15
|
+
import { betterAuth } from 'better-auth';
|
|
16
|
+
import { defineAuth } from '@fluixi/auth/server';
|
|
17
|
+
export const auth = defineAuth(betterAuth({ /* db, providers, plugins */ }));
|
|
18
|
+
```
|
|
19
|
+
```ts
|
|
20
|
+
// src/middleware.ts — resolve the session once per request → locals.auth
|
|
21
|
+
import { defineMiddleware } from '@fluixi/start';
|
|
22
|
+
import { authMiddleware } from '@fluixi/auth/server';
|
|
23
|
+
export default defineMiddleware([ authMiddleware() ]);
|
|
24
|
+
```
|
|
25
|
+
```ts
|
|
26
|
+
// src/api/auth/[...all].ts — mount Better Auth
|
|
27
|
+
import { mountAuthRoutes } from '@fluixi/auth/server';
|
|
28
|
+
export const ALL = mountAuthRoutes(); // (request) => auth.handler(request)
|
|
29
|
+
```
|
|
30
|
+
```ts
|
|
31
|
+
// loaders / server functions — no token parsing, reads request-scoped locals
|
|
32
|
+
const user = await requireUser(); // throws AuthError(401, '/login')
|
|
33
|
+
const admin = await requireRole('admin'); // throws AuthError(403)
|
|
34
|
+
await requirePermission(() => can(user, 'edit', post));
|
|
35
|
+
const maybe = await optionalUser(); // U | null
|
|
36
|
+
```
|
|
37
|
+
```ts
|
|
38
|
+
// API route contracts (@fluixi/api) take a guard instead: it runs before the input is read
|
|
39
|
+
// and types `auth` in the handler.
|
|
40
|
+
import { route } from '@fluixi/api';
|
|
41
|
+
import { member, signedIn } from '@fluixi/auth/server';
|
|
42
|
+
|
|
43
|
+
export const GET = route({ guard: signedIn(), handler: ({ auth }) => auth.user });
|
|
44
|
+
export const DELETE = route({
|
|
45
|
+
guard: member({ roles: ['owner', 'admin'] }), // 403 no_active_organization, not_a_member, forbidden
|
|
46
|
+
handler: ({ auth }) => projects.remove(auth.member.organizationId),
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
> The app entry must forward the request so middleware locals reach the render:
|
|
50
|
+
> `export const render = (url, request) => renderRequestAsync(App, { url, request })`.
|
|
51
|
+
|
|
52
|
+
## Client (`@fluixi/auth/client`)
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { createAuthClient } from 'better-auth/client';
|
|
56
|
+
import { createAuthHooks } from '@fluixi/auth/client';
|
|
57
|
+
|
|
58
|
+
const authClient = createAuthClient({ baseURL: '/api/auth' });
|
|
59
|
+
export const { useUser, useSession, useAuth, useSignIn, useSignOut, protectedRoute } =
|
|
60
|
+
createAuthHooks({ client: authClient });
|
|
61
|
+
```
|
|
62
|
+
```ts
|
|
63
|
+
const user = useUser(); // reactive accessor, SEEDED from SSR (no flash/refetch)
|
|
64
|
+
const signIn = useSignIn();
|
|
65
|
+
export default protectedRoute(() => <Dashboard/>); // authed only
|
|
66
|
+
export default protectedRoute(Login, { guestOnly: true }); // redirect authed away
|
|
67
|
+
export default protectedRoute(Admin, { role: 'admin' }); // role-gated
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The session is a `createIsomorphicStore` — resolved on the server from `locals.auth`,
|
|
71
|
+
serialized into the page, and seeded synchronously on the client. No auth flash, no
|
|
72
|
+
post-hydration refetch. `@fluixi/auth/client` never imports the server engine or Node.
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/*! @fluixi/auth v1.0.0-alpha.100 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/client.ts
|
|
22
|
+
var client_exports = {};
|
|
23
|
+
__export(client_exports, {
|
|
24
|
+
createAuthHooks: () => createAuthHooks
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(client_exports);
|
|
27
|
+
var import_core = require("@fluixi/core");
|
|
28
|
+
var import_dom = require("@fluixi/dom");
|
|
29
|
+
|
|
30
|
+
// src/types.ts
|
|
31
|
+
var AUTH_LOCALS_KEY = "auth";
|
|
32
|
+
|
|
33
|
+
// src/client.ts
|
|
34
|
+
function createAuthHooks(options) {
|
|
35
|
+
const sess = options.session;
|
|
36
|
+
const store = (0, import_core.createIsomorphicStore)("auth", {
|
|
37
|
+
// Server: read what authMiddleware put on locals → serialized for the client to seed.
|
|
38
|
+
server: () => (0, import_dom.getLocals)()[AUTH_LOCALS_KEY] ?? { user: null, session: null },
|
|
39
|
+
// SPA cold load: from the provided session, else the Better Auth client.
|
|
40
|
+
client: async () => sess ? { user: sess.user(), session: null } : normalize(await options.client.getSession()),
|
|
41
|
+
fallback: { user: null, session: null }
|
|
42
|
+
});
|
|
43
|
+
if (sess) {
|
|
44
|
+
const apply = () => {
|
|
45
|
+
if (options.ready && !options.ready()) return;
|
|
46
|
+
store.set({ user: sess.user(), session: null });
|
|
47
|
+
};
|
|
48
|
+
apply();
|
|
49
|
+
(0, import_core.createRoot)(() => (0, import_core.createEffect)(apply, void 0, { name: "auth ← session" }));
|
|
50
|
+
}
|
|
51
|
+
const refresh = async () => {
|
|
52
|
+
store.set(sess ? { user: sess.user(), session: null } : normalize(await options.client.getSession()));
|
|
53
|
+
};
|
|
54
|
+
const signIn = sess ? ((...args) => sess.login?.(...args) ?? Promise.resolve()) : async (creds) => {
|
|
55
|
+
await options.client.signIn.email(creds);
|
|
56
|
+
await refresh();
|
|
57
|
+
};
|
|
58
|
+
const signOut = sess ? () => sess.logout?.() ?? Promise.resolve() : async () => {
|
|
59
|
+
await options.client.signOut();
|
|
60
|
+
store.set({ user: null, session: null });
|
|
61
|
+
};
|
|
62
|
+
const go = (to, opts) => {
|
|
63
|
+
const fromRoute = typeof opts?.handler === "function" ? opts.handler() : void 0;
|
|
64
|
+
const fromHooks = typeof options.navigate === "function" ? options.navigate : typeof options.navigate?.handler === "function" ? (
|
|
65
|
+
// Called, not passed along. Handing back the factory made `fn(to)` build a
|
|
66
|
+
// navigate function and return it, so the redirect silently did nothing.
|
|
67
|
+
options.navigate.handler()
|
|
68
|
+
) : void 0;
|
|
69
|
+
const fn = fromRoute ?? fromHooks;
|
|
70
|
+
if (fn) {
|
|
71
|
+
const navigateOptions = opts?.options ?? (typeof options.navigate === "object" ? options.navigate?.options : void 0);
|
|
72
|
+
if (navigateOptions) fn(to, navigateOptions);
|
|
73
|
+
else fn(to);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
import_core.env.window?.location.assign(to);
|
|
77
|
+
};
|
|
78
|
+
const permissions = () => sess?.permissions?.();
|
|
79
|
+
const can = (query) => sess?.can?.(query) ?? false;
|
|
80
|
+
const roleList = () => {
|
|
81
|
+
if (sess?.roles) return sess.roles();
|
|
82
|
+
const r = store.fields.user()?.role;
|
|
83
|
+
return r ? [r] : [];
|
|
84
|
+
};
|
|
85
|
+
const passesRole = (opts) => {
|
|
86
|
+
if (opts?.role && !roleList().includes(opts.role)) return false;
|
|
87
|
+
if (opts?.roles && !opts.roles.some((r) => roleList().includes(r))) return false;
|
|
88
|
+
if (opts?.permission !== void 0 && !can(opts.permission)) return false;
|
|
89
|
+
return true;
|
|
90
|
+
};
|
|
91
|
+
function Can(props) {
|
|
92
|
+
return () => can(props.perform) ? props.children : props.fallback ?? null;
|
|
93
|
+
}
|
|
94
|
+
function Protect(props) {
|
|
95
|
+
return () => {
|
|
96
|
+
if (!store.fields.user()) return props.fallback ?? null;
|
|
97
|
+
return passesRole(props) ? props.children : props.fallback ?? null;
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function protectedRoute(Comp, opts) {
|
|
101
|
+
return (props) => {
|
|
102
|
+
const user = store.fields.user();
|
|
103
|
+
if (opts?.guestOnly) {
|
|
104
|
+
if (user) return go(options.homePath ?? "/", opts?.navigator), null;
|
|
105
|
+
} else {
|
|
106
|
+
if (!user) return go(options.loginPath ?? "/login", opts?.navigator), null;
|
|
107
|
+
if (!passesRole(opts)) return go(options.forbiddenPath ?? "/403"), null;
|
|
108
|
+
}
|
|
109
|
+
return Comp(props);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
/**
|
|
114
|
+
* The signed-in user, or null.
|
|
115
|
+
*
|
|
116
|
+
* Returns the accessor rather than the value, so a component reads it where it
|
|
117
|
+
* renders and re-renders when it changes.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```tsx
|
|
121
|
+
* const user = useUser();
|
|
122
|
+
* <span>{() => user()?.email ?? 'Signed out'}</span>
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
useUser: () => store.fields.user,
|
|
126
|
+
/**
|
|
127
|
+
* The session record beside the user: when it expires, and whatever else the
|
|
128
|
+
* backend attached. Null when signed out.
|
|
129
|
+
*/
|
|
130
|
+
useSession: () => store.fields.session,
|
|
131
|
+
/**
|
|
132
|
+
* Everything at once, for a component that needs more than one of them.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```tsx
|
|
136
|
+
* const { user, isAuthenticated, can, signOut } = useAuth();
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
useAuth: () => ({
|
|
140
|
+
user: store.fields.user,
|
|
141
|
+
session: store.fields.session,
|
|
142
|
+
isAuthenticated: () => store.fields.user() !== null,
|
|
143
|
+
roles: roleList,
|
|
144
|
+
can,
|
|
145
|
+
signIn,
|
|
146
|
+
signOut
|
|
147
|
+
}),
|
|
148
|
+
/**
|
|
149
|
+
* Start a sign-in.
|
|
150
|
+
*
|
|
151
|
+
* What it takes depends on what the hooks were given: credentials for a Better Auth
|
|
152
|
+
* client, and for a redirect flow whatever that session's `login` expects.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```tsx
|
|
156
|
+
* const signIn = useSignIn();
|
|
157
|
+
* <button onClick={() => signIn({ email, password })}>Sign in</button>
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
useSignIn: () => signIn,
|
|
161
|
+
/**
|
|
162
|
+
* End the session.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```tsx
|
|
166
|
+
* const signOut = useSignOut();
|
|
167
|
+
* <button onClick={() => signOut()}>Sign out</button>
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
useSignOut: () => signOut,
|
|
171
|
+
/**
|
|
172
|
+
* The permission set for the current roles, reactively.
|
|
173
|
+
*
|
|
174
|
+
* A rendering hint and nothing more: a client can claim any permission, so the
|
|
175
|
+
* server decides what is allowed on every request.
|
|
176
|
+
*/
|
|
177
|
+
usePermissions: () => permissions,
|
|
178
|
+
/**
|
|
179
|
+
* Ask whether the current user may do something.
|
|
180
|
+
*
|
|
181
|
+
* `false` when the session supplies no RBAC, so a missing `access` config hides a
|
|
182
|
+
* control rather than revealing it.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```tsx
|
|
186
|
+
* const can = useCan();
|
|
187
|
+
* <button disabled={() => !can((p) => p.users.delete)}>Delete</button>
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
useCan: () => can,
|
|
191
|
+
Can,
|
|
192
|
+
Protect,
|
|
193
|
+
/**
|
|
194
|
+
* Re-read the session from the backend.
|
|
195
|
+
*
|
|
196
|
+
* For after something changed outside this tab: a role granted elsewhere, or a
|
|
197
|
+
* return from a flow that altered the account.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* await refresh();
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
refresh,
|
|
205
|
+
protectedRoute,
|
|
206
|
+
/**
|
|
207
|
+
* The underlying isomorphic store.
|
|
208
|
+
*
|
|
209
|
+
* Escape hatch. It is here for a test that wants to seed a user, and for the rare
|
|
210
|
+
* caller that needs to write what the hooks read. Prefer the accessors above:
|
|
211
|
+
* writing to the store directly puts the interface out of step with the session
|
|
212
|
+
* that owns it.
|
|
213
|
+
*/
|
|
214
|
+
store
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function normalize(r) {
|
|
218
|
+
return { user: r.data?.user ?? null, session: r.data?.session ?? null };
|
|
219
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { type ComponentChild } from '@fluixi/dom';
|
|
2
|
+
import { type AuthUser, type BetterAuthClient, type SessionData } from './types.js';
|
|
3
|
+
export interface ProtectedRouteOptions<Q = (p: any) => boolean> {
|
|
4
|
+
guestOnly?: boolean;
|
|
5
|
+
/** Require this exact role. */
|
|
6
|
+
role?: string;
|
|
7
|
+
/** Require ANY of these roles. */
|
|
8
|
+
roles?: string[];
|
|
9
|
+
/** Require a permission: your access matcher's query (default: a predicate). */
|
|
10
|
+
permission?: Q;
|
|
11
|
+
navigator?: Partial<Navigator>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Any reactive session with a `user` accessor (e.g. @fluixi/session's createSession).
|
|
15
|
+
* The RBAC accessors (`roles` / `permissions` / `can`) are optional, provide them (as
|
|
16
|
+
* createSession's `access` config does) to unlock `<Can>`, `usePermissions`, and
|
|
17
|
+
* permission-gated routes. They are a UI affordance only; the server enforces authz.
|
|
18
|
+
*/
|
|
19
|
+
export interface SessionLike<U = AuthUser, P = unknown, Q = (p: P) => boolean> {
|
|
20
|
+
user: () => U | null;
|
|
21
|
+
login?: (...args: any[]) => Promise<unknown>;
|
|
22
|
+
logout?: () => Promise<unknown>;
|
|
23
|
+
roles?: () => string[];
|
|
24
|
+
permissions?: () => P | undefined;
|
|
25
|
+
can?: (query: Q) => boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface NavigateOptions {
|
|
28
|
+
/** Replace the current history entry instead of pushing a new one. */
|
|
29
|
+
replace?: boolean;
|
|
30
|
+
/** Opaque history state to attach. */
|
|
31
|
+
state?: unknown;
|
|
32
|
+
transition?: unknown;
|
|
33
|
+
}
|
|
34
|
+
export interface Navigator {
|
|
35
|
+
handler: () => (to: string, opts?: any) => void;
|
|
36
|
+
options?: NavigateOptions;
|
|
37
|
+
}
|
|
38
|
+
export interface AuthHooksOptions<U = AuthUser, P = unknown, Q = (p: P) => boolean> {
|
|
39
|
+
/** A Better Auth client (`createAuthClient(...)`). Mutually exclusive with `session`. */
|
|
40
|
+
client?: BetterAuthClient;
|
|
41
|
+
/** An existing reactive session: the framework syncs it into the hooks for you. */
|
|
42
|
+
session?: SessionLike<U, P, Q>;
|
|
43
|
+
/**
|
|
44
|
+
* Whether `session` knows yet. Until it does, the hooks keep what they started from: in the
|
|
45
|
+
* browser, the user the server rendered with. Default: ready from the start.
|
|
46
|
+
*/
|
|
47
|
+
ready?: () => boolean;
|
|
48
|
+
/** Navigate for `protectedRoute` redirects (default: `window.location.assign`). */
|
|
49
|
+
navigate?: ((to: string, opts?: NavigateOptions) => void) | Navigator;
|
|
50
|
+
loginPath?: string;
|
|
51
|
+
homePath?: string;
|
|
52
|
+
forbiddenPath?: string;
|
|
53
|
+
}
|
|
54
|
+
export declare function createAuthHooks<U = AuthUser, P = unknown, Q = (p: P) => boolean>(options: AuthHooksOptions<U, P, Q>): {
|
|
55
|
+
/**
|
|
56
|
+
* The signed-in user, or null.
|
|
57
|
+
*
|
|
58
|
+
* Returns the accessor rather than the value, so a component reads it where it
|
|
59
|
+
* renders and re-renders when it changes.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* const user = useUser();
|
|
64
|
+
* <span>{() => user()?.email ?? 'Signed out'}</span>
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
useUser: () => import("@fluixi/core").Accessor<U | null>;
|
|
68
|
+
/**
|
|
69
|
+
* The session record beside the user: when it expires, and whatever else the
|
|
70
|
+
* backend attached. Null when signed out.
|
|
71
|
+
*/
|
|
72
|
+
useSession: () => import("@fluixi/core").Accessor<import("./types.js").AuthSession | null>;
|
|
73
|
+
/**
|
|
74
|
+
* Everything at once, for a component that needs more than one of them.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```tsx
|
|
78
|
+
* const { user, isAuthenticated, can, signOut } = useAuth();
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
useAuth: () => {
|
|
82
|
+
user: import("@fluixi/core").Accessor<U | null>;
|
|
83
|
+
session: import("@fluixi/core").Accessor<import("./types.js").AuthSession | null>;
|
|
84
|
+
isAuthenticated: () => boolean;
|
|
85
|
+
roles: () => string[];
|
|
86
|
+
can: (query: Q) => boolean;
|
|
87
|
+
signIn: ((...args: any[]) => Promise<unknown>) | ((creds: {
|
|
88
|
+
email: string;
|
|
89
|
+
password: string;
|
|
90
|
+
rememberMe?: boolean;
|
|
91
|
+
}) => Promise<void>);
|
|
92
|
+
signOut: () => Promise<unknown>;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Start a sign-in.
|
|
96
|
+
*
|
|
97
|
+
* What it takes depends on what the hooks were given: credentials for a Better Auth
|
|
98
|
+
* client, and for a redirect flow whatever that session's `login` expects.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```tsx
|
|
102
|
+
* const signIn = useSignIn();
|
|
103
|
+
* <button onClick={() => signIn({ email, password })}>Sign in</button>
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
useSignIn: () => ((...args: any[]) => Promise<unknown>) | ((creds: {
|
|
107
|
+
email: string;
|
|
108
|
+
password: string;
|
|
109
|
+
rememberMe?: boolean;
|
|
110
|
+
}) => Promise<void>);
|
|
111
|
+
/**
|
|
112
|
+
* End the session.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```tsx
|
|
116
|
+
* const signOut = useSignOut();
|
|
117
|
+
* <button onClick={() => signOut()}>Sign out</button>
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
useSignOut: () => () => Promise<unknown>;
|
|
121
|
+
/**
|
|
122
|
+
* The permission set for the current roles, reactively.
|
|
123
|
+
*
|
|
124
|
+
* A rendering hint and nothing more: a client can claim any permission, so the
|
|
125
|
+
* server decides what is allowed on every request.
|
|
126
|
+
*/
|
|
127
|
+
usePermissions: () => () => P | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* Ask whether the current user may do something.
|
|
130
|
+
*
|
|
131
|
+
* `false` when the session supplies no RBAC, so a missing `access` config hides a
|
|
132
|
+
* control rather than revealing it.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```tsx
|
|
136
|
+
* const can = useCan();
|
|
137
|
+
* <button disabled={() => !can((p) => p.users.delete)}>Delete</button>
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
useCan: () => (query: Q) => boolean;
|
|
141
|
+
Can: (props: {
|
|
142
|
+
perform: Q;
|
|
143
|
+
fallback?: ComponentChild;
|
|
144
|
+
children: ComponentChild;
|
|
145
|
+
}) => () => ComponentChild;
|
|
146
|
+
Protect: (props: {
|
|
147
|
+
role?: string;
|
|
148
|
+
roles?: string[];
|
|
149
|
+
permission?: Q;
|
|
150
|
+
fallback?: ComponentChild;
|
|
151
|
+
children: ComponentChild;
|
|
152
|
+
}) => () => ComponentChild;
|
|
153
|
+
/**
|
|
154
|
+
* Re-read the session from the backend.
|
|
155
|
+
*
|
|
156
|
+
* For after something changed outside this tab: a role granted elsewhere, or a
|
|
157
|
+
* return from a flow that altered the account.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* await refresh();
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
refresh: () => Promise<void>;
|
|
165
|
+
protectedRoute: <Props>(Comp: (props: Props) => unknown, opts?: ProtectedRouteOptions<Q>) => (props: Props) => unknown;
|
|
166
|
+
/**
|
|
167
|
+
* The underlying isomorphic store.
|
|
168
|
+
*
|
|
169
|
+
* Escape hatch. It is here for a test that wants to seed a user, and for the rare
|
|
170
|
+
* caller that needs to write what the hooks read. Prefer the accessors above:
|
|
171
|
+
* writing to the store directly puts the interface out of step with the session
|
|
172
|
+
* that owns it.
|
|
173
|
+
*/
|
|
174
|
+
store: import("@fluixi/core").IsomorphicStore<SessionData<U>>;
|
|
175
|
+
};
|
|
176
|
+
export type { SessionData, AuthUser, BetterAuthClient } from './types.js';
|
|
177
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAcA,OAAO,EAAa,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,gBAAgB,EACrB,KAAK,WAAW,EACjB,MAAM,YAAY,CAAC;AAGpB,MAAM,WAAW,qBAAqB,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,KAAK,OAAO;IAC5D,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,+BAA+B;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,gFAAgF;IAChF,UAAU,CAAC,EAAE,CAAC,CAAC;IAEf,SAAS,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;CAChC;AAED;;;;;GAKG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,QAAQ,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO;IAC3E,IAAI,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC7C,MAAM,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,C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