@jgb-solutions/kit 0.4.4 → 0.5.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.
@@ -0,0 +1,293 @@
1
+ import { type SessionUser } from '../auth';
2
+ import { type AuthUser } from './rules';
3
+ /** What an action answers: done, or Better Auth's code (or one of this module's) for the app to word. */
4
+ export type AuthResult = {
5
+ ok: true;
6
+ } | {
7
+ ok: false;
8
+ code: string;
9
+ };
10
+ /** Runs a Better Auth call: its APIError becomes `{ ok: false, code }`; any other error is thrown on. */
11
+ export declare function authResult(action: () => Promise<unknown>): Promise<AuthResult>;
12
+ /** Codes this module answers with besides Better Auth's own. */
13
+ export declare const AUTH_SERVER_CODES: {
14
+ /** No signed-in person. */
15
+ readonly unauthorized: "UNAUTHORIZED";
16
+ /** Deleting an account with a password, without giving it. */
17
+ readonly passwordRequired: "INVALID_PASSWORD";
18
+ /** Deleting an account without a password: the typed username didn't match. */
19
+ readonly confirmUsername: "CONFIRM_USERNAME";
20
+ /** Sign-in with an account's placeholder address (it isn't a real one). */
21
+ readonly invalidLogin: "INVALID_EMAIL_OR_PASSWORD";
22
+ /** Better Auth answered a Google start without an address to go to. */
23
+ readonly noRedirect: "NO_REDIRECT_URL";
24
+ /** An admin acting on their own account (role, ban, password reset): they use their account page. */
25
+ readonly self: "CANNOT_ACT_ON_SELF";
26
+ };
27
+ type Headers_ = Headers;
28
+ type Call<Body> = {
29
+ body: Body;
30
+ headers: Headers_;
31
+ };
32
+ /**
33
+ * The part of Better Auth's `auth.api` this module calls (the standard preset's plugins: username,
34
+ * passkey, admin, bearer). Declared here so any app's instance fits, whatever its roles and options.
35
+ */
36
+ export type AuthApi<Role extends string = string> = {
37
+ getSession(input: {
38
+ headers: Headers_;
39
+ query?: {
40
+ disableCookieCache?: boolean;
41
+ };
42
+ }): Promise<{
43
+ user: SessionUser;
44
+ } | null>;
45
+ signInEmail(input: Call<{
46
+ email: string;
47
+ password: string;
48
+ }>): Promise<unknown>;
49
+ signInUsername(input: Call<{
50
+ username: string;
51
+ password: string;
52
+ }>): Promise<unknown>;
53
+ signUpEmail(input: Call<{
54
+ name: string;
55
+ username: string;
56
+ email: string;
57
+ password: string;
58
+ }>): Promise<unknown>;
59
+ signOut(input: {
60
+ headers: Headers_;
61
+ }): Promise<unknown>;
62
+ changePassword(input: Call<{
63
+ currentPassword: string;
64
+ newPassword: string;
65
+ revokeOtherSessions: boolean;
66
+ }>): Promise<unknown>;
67
+ setPassword(input: Call<{
68
+ newPassword: string;
69
+ }>): Promise<unknown>;
70
+ listPasskeys(input: {
71
+ headers: Headers_;
72
+ }): Promise<readonly {
73
+ id: string;
74
+ name?: string | null | undefined;
75
+ createdAt: Date | string;
76
+ }[]>;
77
+ deletePasskey(input: Call<{
78
+ id: string;
79
+ }>): Promise<unknown>;
80
+ listUserAccounts(input: {
81
+ headers: Headers_;
82
+ }): Promise<readonly {
83
+ providerId: string;
84
+ }[]>;
85
+ signInSocial(input: Call<{
86
+ provider: 'google';
87
+ callbackURL: string;
88
+ errorCallbackURL: string;
89
+ }>): Promise<{
90
+ url?: string | undefined;
91
+ }>;
92
+ linkSocialAccount(input: Call<{
93
+ provider: 'google';
94
+ callbackURL: string;
95
+ errorCallbackURL: string;
96
+ }>): Promise<{
97
+ url?: string | undefined;
98
+ }>;
99
+ deleteUser(input: Call<{
100
+ password?: string;
101
+ }>): Promise<unknown>;
102
+ listUsers(input: {
103
+ headers: Headers_;
104
+ query: {
105
+ limit: number;
106
+ offset: number;
107
+ sortBy: string;
108
+ sortDirection: 'asc' | 'desc';
109
+ searchValue?: string;
110
+ searchField?: 'email' | 'name';
111
+ searchOperator?: 'contains';
112
+ };
113
+ }): Promise<{
114
+ users: readonly (SessionUser & {
115
+ createdAt: Date | string;
116
+ })[];
117
+ total: number;
118
+ }>;
119
+ setRole(input: Call<{
120
+ userId: string;
121
+ role: Role;
122
+ }>): Promise<unknown>;
123
+ banUser(input: Call<{
124
+ userId: string;
125
+ banReason?: string;
126
+ }>): Promise<unknown>;
127
+ unbanUser(input: Call<{
128
+ userId: string;
129
+ }>): Promise<unknown>;
130
+ setUserPassword(input: Call<{
131
+ userId: string;
132
+ newPassword: string;
133
+ }>): Promise<unknown>;
134
+ revokeUserSessions(input: Call<{
135
+ userId: string;
136
+ }>): Promise<unknown>;
137
+ };
138
+ /** Anything with Better Auth's `api` (the instance itself). */
139
+ export type AuthInstance<Role extends string = string> = {
140
+ api: AuthApi<Role>;
141
+ };
142
+ export type AuthServerConfig<Role extends string> = {
143
+ /** The Better Auth instance, or a function that makes it on first use. */
144
+ auth: NoInfer<AuthInstance<Role> | (() => AuthInstance<Role> | Promise<AuthInstance<Role>>)>;
145
+ /** The current request's headers (TanStack Start: `getRequestHeaders`). */
146
+ headers: () => Headers;
147
+ /** From `defineRoles`. */
148
+ roleOf: (stored: string | null | undefined) => Role;
149
+ /** From `defineRoles`: who may open the admin panel. */
150
+ canOpenAdmin: (stored: string | null | undefined) => boolean;
151
+ /** A new placeholder address's random part (Hermes has no global crypto; servers keep the default). */
152
+ randomId?: () => string;
153
+ };
154
+ /** A passkey as an account page shows it. */
155
+ export type PasskeyRow = {
156
+ id: string;
157
+ name: string | null;
158
+ createdAt: string;
159
+ };
160
+ /** One row of the admin's user list. `email` is null for an account made without one. */
161
+ export type AdminUserRow<Role extends string = string> = {
162
+ id: string;
163
+ name: string;
164
+ username: string;
165
+ email: string | null;
166
+ role: Role;
167
+ banned: boolean;
168
+ createdAt: string;
169
+ };
170
+ /** A password nobody chose: 18 random bytes as base64url (24 characters). */
171
+ export declare function temporaryPassword(): string;
172
+ /** The account functions for one app. Every function reads the current request's headers itself. */
173
+ export declare function authServer<Role extends string>(config: AuthServerConfig<Role>): {
174
+ viewer: (options?: {
175
+ fresh?: boolean;
176
+ }) => Promise<AuthUser<Role> | null>;
177
+ signInMethods: () => Promise<{
178
+ password: boolean;
179
+ google: boolean;
180
+ }>;
181
+ /** Sign-in with a username or an email address, the same password either way. */
182
+ signIn(input: {
183
+ login: string;
184
+ password: string;
185
+ }): Promise<AuthResult>;
186
+ /** Sign-up with a username, a password and, when given, an email address (else a placeholder, never shown or mailed). */
187
+ signUp(input: {
188
+ username: string;
189
+ password: string;
190
+ email?: string | undefined;
191
+ name?: string | undefined;
192
+ }): Promise<AuthResult>;
193
+ signOut(): Promise<AuthResult>;
194
+ /** Changes the password and signs out the person's other devices. */
195
+ changePassword(input: {
196
+ currentPassword: string;
197
+ newPassword: string;
198
+ }): Promise<AuthResult>;
199
+ /** A first password for an account made with Google (Better Auth refuses if it already has one). */
200
+ setFirstPassword(input: {
201
+ newPassword: string;
202
+ }): Promise<AuthResult>;
203
+ /** The signed-in person's passkeys, newest first (empty when signed out). */
204
+ passkeys(): Promise<PasskeyRow[]>;
205
+ /** Removes one of the signed-in person's passkeys (Better Auth checks it is theirs). */
206
+ removePasskey(id: string): Promise<AuthResult>;
207
+ /**
208
+ * The address of Google's consent page for "Continue with Google". Google comes back to
209
+ * /api/auth/callback/google, then to `redirect` (when it is a safe path on this site) or `home`;
210
+ * errors go back to `signInPath` with `?error=` (and the redirect kept).
211
+ */
212
+ googleStartUrl(input: {
213
+ redirect?: string | undefined;
214
+ home: string;
215
+ signInPath: string;
216
+ }): Promise<{
217
+ ok: true;
218
+ url: string;
219
+ } | {
220
+ ok: false;
221
+ code: string;
222
+ }>;
223
+ /**
224
+ * Connects Google to the signed-in account. Comes back to `accountPath` with `?connected=google`,
225
+ * or with `?error=` (another account has this Google account, or Google didn't confirm the address).
226
+ */
227
+ googleLinkUrl(input: {
228
+ accountPath: string;
229
+ }): Promise<{
230
+ ok: true;
231
+ url: string;
232
+ } | {
233
+ ok: false;
234
+ code: string;
235
+ }>;
236
+ /**
237
+ * Deletes the signed-in person's account (the app's `beforeDeleteUser` removes what they saved).
238
+ * The server decides from the account itself, never from what the page sends: an account with a
239
+ * password must give it (without one, Better Auth deletes any account whose session is under a day
240
+ * old); an account without one (made with Google) types its username, and Better Auth then asks for
241
+ * a sign-in from the last day (SESSION_EXPIRED otherwise).
242
+ */
243
+ deleteOwnAccount(input: {
244
+ password?: string | undefined;
245
+ confirmUsername?: string | undefined;
246
+ }): Promise<AuthResult>;
247
+ admin: {
248
+ /** The signed-in admin, or null (also when suspended). */
249
+ viewer: () => Promise<AuthUser<Role> | null>;
250
+ /**
251
+ * One page of accounts, newest first. A search with "@" looks in addresses, anything else in
252
+ * names (an account's name is its username when made with one). Placeholder addresses are never shown.
253
+ */
254
+ listUsers(input: {
255
+ page: number;
256
+ pageSize: number;
257
+ search?: string | undefined;
258
+ }): Promise<{
259
+ ok: true;
260
+ total: number;
261
+ users: AdminUserRow<Role>[];
262
+ } | {
263
+ ok: false;
264
+ code: string;
265
+ }>;
266
+ setRole: (input: {
267
+ userId: string;
268
+ role: Role;
269
+ }) => Promise<AuthResult>;
270
+ ban: (input: {
271
+ userId: string;
272
+ reason?: string | undefined;
273
+ }) => Promise<AuthResult>;
274
+ unban: (input: {
275
+ userId: string;
276
+ }) => Promise<AuthResult>;
277
+ /**
278
+ * A new random password for someone who forgot theirs (no reset emails): the admin passes it on
279
+ * privately; shown once. Their sessions end, so only the new password signs in.
280
+ */
281
+ resetPassword(input: {
282
+ userId: string;
283
+ }): Promise<{
284
+ ok: true;
285
+ password: string;
286
+ } | {
287
+ ok: false;
288
+ code: string;
289
+ }>;
290
+ };
291
+ };
292
+ export type AuthServer<Role extends string> = ReturnType<typeof authServer<Role>>;
293
+ export {};