@stacksjs/auth 0.70.23 → 0.70.25
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.js +155 -5168
- package/dist/src/authentication.d.ts +43 -0
- package/dist/src/authenticator.d.ts +17 -0
- package/dist/src/authorizable.d.ts +77 -0
- package/dist/src/client.d.ts +2 -0
- package/dist/src/email-verification.d.ts +29 -0
- package/dist/src/gate.d.ts +161 -0
- package/dist/src/index.d.ts +29 -0
- package/dist/src/middleware.d.ts +14 -0
- package/dist/src/passkey.d.ts +46 -0
- package/dist/src/password/reset.d.ts +10 -0
- package/dist/src/policy.d.ts +33 -0
- package/dist/src/rate-limiter.d.ts +6 -0
- package/dist/src/rbac.d.ts +219 -0
- package/dist/src/register.d.ts +3 -0
- package/dist/src/session-auth.d.ts +30 -0
- package/dist/src/tokens.d.ts +251 -0
- package/dist/src/user.d.ts +34 -0
- package/package.json +22 -12
- package/dist/authentication.d.ts +0 -150
- package/dist/authenticator.d.ts +0 -6
- package/dist/index.d.ts +0 -3
- package/dist/passkey.d.ts +0 -64
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import type { UserModel as OrmUserModel } from '@stacksjs/orm';
|
|
2
|
+
/**
|
|
3
|
+
* Set the RBAC store implementation
|
|
4
|
+
*/
|
|
5
|
+
export declare function setRbacStore(rbacStore: RbacStore): void;
|
|
6
|
+
/**
|
|
7
|
+
* Flush the RBAC cache
|
|
8
|
+
*/
|
|
9
|
+
export declare function flushRbacCache(): void;
|
|
10
|
+
/**
|
|
11
|
+
* Create a new role
|
|
12
|
+
*/
|
|
13
|
+
export declare function createRole(name: string, guardName?: string, description?: string): Promise<RoleRecord>;
|
|
14
|
+
/**
|
|
15
|
+
* Find a role by name
|
|
16
|
+
*/
|
|
17
|
+
export declare function findRole(name: string, guardName?: string): Promise<RoleRecord | null>;
|
|
18
|
+
/**
|
|
19
|
+
* Delete a role
|
|
20
|
+
*/
|
|
21
|
+
export declare function deleteRole(name: string, guardName?: string): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Get all roles
|
|
24
|
+
*/
|
|
25
|
+
export declare function getAllRoles(guardName?: string): Promise<RoleRecord[]>;
|
|
26
|
+
/**
|
|
27
|
+
* Create a new permission
|
|
28
|
+
*/
|
|
29
|
+
export declare function createPermission(name: string, guardName?: string, description?: string): Promise<PermissionRecord>;
|
|
30
|
+
/**
|
|
31
|
+
* Find a permission by name
|
|
32
|
+
*/
|
|
33
|
+
export declare function findPermission(name: string, guardName?: string): Promise<PermissionRecord | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Delete a permission
|
|
36
|
+
*/
|
|
37
|
+
export declare function deletePermission(name: string, guardName?: string): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Get all permissions
|
|
40
|
+
*/
|
|
41
|
+
export declare function getAllPermissions(guardName?: string): Promise<PermissionRecord[]>;
|
|
42
|
+
/**
|
|
43
|
+
* Get all roles for a user
|
|
44
|
+
*/
|
|
45
|
+
export declare function getUserRoles(user: UserModel | { id: number } | number): Promise<RoleRecord[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Assign a role to a user
|
|
48
|
+
*/
|
|
49
|
+
export declare function assignRole(user: UserModel | { id: number } | number, roleName: string, guardName?: string): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Remove a role from a user
|
|
52
|
+
*/
|
|
53
|
+
export declare function removeRole(user: UserModel | { id: number } | number, roleName: string, guardName?: string): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Remove all roles from a user
|
|
56
|
+
*/
|
|
57
|
+
export declare function removeAllRoles(user: UserModel | { id: number } | number): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Sync roles for a user (replaces all current roles)
|
|
60
|
+
*/
|
|
61
|
+
export declare function syncRoles(user: UserModel | { id: number } | number, roleNames: string[], guardName?: string): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Check if a user has a specific role
|
|
64
|
+
*/
|
|
65
|
+
export declare function hasRole(user: UserModel | { id: number } | number, roleName: string, guardName?: string): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Check if a user has any of the given roles
|
|
68
|
+
*/
|
|
69
|
+
export declare function hasAnyRole(user: UserModel | { id: number } | number, roleNames: string[], guardName?: string): Promise<boolean>;
|
|
70
|
+
/**
|
|
71
|
+
* Check if a user has all of the given roles
|
|
72
|
+
*/
|
|
73
|
+
export declare function hasAllRoles(user: UserModel | { id: number } | number, roleNames: string[], guardName?: string): Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* Get all permissions for a user (direct + via roles)
|
|
76
|
+
*/
|
|
77
|
+
export declare function getUserPermissions(user: UserModel | { id: number } | number): Promise<PermissionRecord[]>;
|
|
78
|
+
/**
|
|
79
|
+
* Give a direct permission to a user
|
|
80
|
+
*/
|
|
81
|
+
export declare function givePermission(user: UserModel | { id: number } | number, permissionName: string, guardName?: string): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Revoke a direct permission from a user
|
|
84
|
+
*/
|
|
85
|
+
export declare function revokePermission(user: UserModel | { id: number } | number, permissionName: string, guardName?: string): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Revoke all direct permissions from a user
|
|
88
|
+
*/
|
|
89
|
+
export declare function revokeAllPermissions(user: UserModel | { id: number } | number): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Sync direct permissions for a user
|
|
92
|
+
*/
|
|
93
|
+
export declare function syncPermissions(user: UserModel | { id: number } | number, permissionNames: string[], guardName?: string): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Check if a user has a specific permission (direct or via role)
|
|
96
|
+
*/
|
|
97
|
+
export declare function hasPermission(user: UserModel | { id: number } | number, permissionName: string, guardName?: string): Promise<boolean>;
|
|
98
|
+
/**
|
|
99
|
+
* Check if a user has any of the given permissions
|
|
100
|
+
*/
|
|
101
|
+
export declare function hasAnyPermission(user: UserModel | { id: number } | number, permissionNames: string[], guardName?: string): Promise<boolean>;
|
|
102
|
+
/**
|
|
103
|
+
* Check if a user has all of the given permissions
|
|
104
|
+
*/
|
|
105
|
+
export declare function hasAllPermissions(user: UserModel | { id: number } | number, permissionNames: string[], guardName?: string): Promise<boolean>;
|
|
106
|
+
/**
|
|
107
|
+
* Get all permissions for a role
|
|
108
|
+
*/
|
|
109
|
+
export declare function getRolePermissions(roleId: number): Promise<PermissionRecord[]>;
|
|
110
|
+
/**
|
|
111
|
+
* Assign a permission to a role
|
|
112
|
+
*/
|
|
113
|
+
export declare function givePermissionToRole(roleName: string, permissionName: string, guardName?: string): Promise<void>;
|
|
114
|
+
/**
|
|
115
|
+
* Remove a permission from a role
|
|
116
|
+
*/
|
|
117
|
+
export declare function revokePermissionFromRole(roleName: string, permissionName: string, guardName?: string): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Sync permissions for a role
|
|
120
|
+
*/
|
|
121
|
+
export declare function syncRolePermissions(roleName: string, permissionNames: string[], guardName?: string): Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Add RBAC methods to a user object
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* const user = withRbac(authenticatedUser)
|
|
127
|
+
* if (await user.hasRole('admin')) { ... }
|
|
128
|
+
* await user.assignRole('editor')
|
|
129
|
+
*/
|
|
130
|
+
export declare function withRbac<T extends UserModel | { id: number }>(user: T): T & RbacMethods;
|
|
131
|
+
/**
|
|
132
|
+
* RBAC facade for convenient access
|
|
133
|
+
*/
|
|
134
|
+
export declare const Rbac: {
|
|
135
|
+
setStore: unknown;
|
|
136
|
+
flushCache: unknown
|
|
137
|
+
};
|
|
138
|
+
export declare interface RoleRecord {
|
|
139
|
+
id: number
|
|
140
|
+
name: string
|
|
141
|
+
guard_name: string
|
|
142
|
+
description?: string
|
|
143
|
+
created_at?: string
|
|
144
|
+
updated_at?: string
|
|
145
|
+
}
|
|
146
|
+
export declare interface PermissionRecord {
|
|
147
|
+
id: number
|
|
148
|
+
name: string
|
|
149
|
+
guard_name: string
|
|
150
|
+
description?: string
|
|
151
|
+
created_at?: string
|
|
152
|
+
updated_at?: string
|
|
153
|
+
}
|
|
154
|
+
export declare interface RolePermissionPivot {
|
|
155
|
+
role_id: number
|
|
156
|
+
permission_id: number
|
|
157
|
+
created_at?: string
|
|
158
|
+
}
|
|
159
|
+
export declare interface UserRolePivot {
|
|
160
|
+
user_id: number
|
|
161
|
+
role_id: number
|
|
162
|
+
created_at?: string
|
|
163
|
+
}
|
|
164
|
+
export declare interface UserPermissionPivot {
|
|
165
|
+
user_id: number
|
|
166
|
+
permission_id: number
|
|
167
|
+
created_at?: string
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* RBAC database adapter interface.
|
|
171
|
+
* Implement this to connect RBAC to your database layer.
|
|
172
|
+
*/
|
|
173
|
+
export declare interface RbacStore {
|
|
174
|
+
findRoleByName(name: string, guardName?: string): Promise<RoleRecord | null>
|
|
175
|
+
findRoleById(id: number): Promise<RoleRecord | null>
|
|
176
|
+
createRole(name: string, guardName?: string, description?: string): Promise<RoleRecord>
|
|
177
|
+
deleteRole(id: number): Promise<void>
|
|
178
|
+
getAllRoles(guardName?: string): Promise<RoleRecord[]>
|
|
179
|
+
findPermissionByName(name: string, guardName?: string): Promise<PermissionRecord | null>
|
|
180
|
+
findPermissionById(id: number): Promise<PermissionRecord | null>
|
|
181
|
+
createPermission(name: string, guardName?: string, description?: string): Promise<PermissionRecord>
|
|
182
|
+
deletePermission(id: number): Promise<void>
|
|
183
|
+
getAllPermissions(guardName?: string): Promise<PermissionRecord[]>
|
|
184
|
+
getUserRoles(userId: number): Promise<RoleRecord[]>
|
|
185
|
+
assignRoleToUser(userId: number, roleId: number): Promise<void>
|
|
186
|
+
removeRoleFromUser(userId: number, roleId: number): Promise<void>
|
|
187
|
+
removeAllRolesFromUser(userId: number): Promise<void>
|
|
188
|
+
syncUserRoles(userId: number, roleIds: number[]): Promise<void>
|
|
189
|
+
getUserDirectPermissions(userId: number): Promise<PermissionRecord[]>
|
|
190
|
+
assignPermissionToUser(userId: number, permissionId: number): Promise<void>
|
|
191
|
+
removePermissionFromUser(userId: number, permissionId: number): Promise<void>
|
|
192
|
+
removeAllPermissionsFromUser(userId: number): Promise<void>
|
|
193
|
+
syncUserPermissions(userId: number, permissionIds: number[]): Promise<void>
|
|
194
|
+
getRolePermissions(roleId: number): Promise<PermissionRecord[]>
|
|
195
|
+
assignPermissionToRole(roleId: number, permissionId: number): Promise<void>
|
|
196
|
+
removePermissionFromRole(roleId: number, permissionId: number): Promise<void>
|
|
197
|
+
syncRolePermissions(roleId: number, permissionIds: number[]): Promise<void>
|
|
198
|
+
}
|
|
199
|
+
// ─── Authorizable Mixin (add to user objects) ───────────────────
|
|
200
|
+
export declare interface RbacMethods {
|
|
201
|
+
hasRole(roleName: string, guardName?: string): Promise<boolean>
|
|
202
|
+
hasAnyRole(roleNames: string[], guardName?: string): Promise<boolean>
|
|
203
|
+
hasAllRoles(roleNames: string[], guardName?: string): Promise<boolean>
|
|
204
|
+
hasPermission(permissionName: string, guardName?: string): Promise<boolean>
|
|
205
|
+
hasAnyPermission(permissionNames: string[], guardName?: string): Promise<boolean>
|
|
206
|
+
hasAllPermissions(permissionNames: string[], guardName?: string): Promise<boolean>
|
|
207
|
+
getRoles(): Promise<RoleRecord[]>
|
|
208
|
+
getPermissions(): Promise<PermissionRecord[]>
|
|
209
|
+
assignRole(roleName: string, guardName?: string): Promise<void>
|
|
210
|
+
removeRole(roleName: string, guardName?: string): Promise<void>
|
|
211
|
+
syncRoles(roleNames: string[], guardName?: string): Promise<void>
|
|
212
|
+
givePermission(permissionName: string, guardName?: string): Promise<void>
|
|
213
|
+
revokePermission(permissionName: string, guardName?: string): Promise<void>
|
|
214
|
+
syncPermissions(permissionNames: string[], guardName?: string): Promise<void>
|
|
215
|
+
}
|
|
216
|
+
// Use the row/instance shape from orm so role helpers operate on the
|
|
217
|
+
// authenticated user object, not the User class constructor.
|
|
218
|
+
declare type UserModel = OrmUserModel;
|
|
219
|
+
export default Rbac;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { User } from '@stacksjs/orm';
|
|
2
|
+
/**
|
|
3
|
+
* Authenticate a user via email and password, creating a session.
|
|
4
|
+
* Sessions are persisted to the database so they survive server restarts.
|
|
5
|
+
*/
|
|
6
|
+
export declare function sessionLogin(email: string, password: string): Promise<{ user: UserModel, sessionId: string }>;
|
|
7
|
+
/**
|
|
8
|
+
* Destroy the session for the given session ID.
|
|
9
|
+
*/
|
|
10
|
+
export declare function sessionLogout(sessionId: string): Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Get the authenticated user from a session ID.
|
|
13
|
+
*/
|
|
14
|
+
export declare function sessionUser(sessionId: string): Promise<UserModel | undefined>;
|
|
15
|
+
/**
|
|
16
|
+
* Check if a session is authenticated.
|
|
17
|
+
*/
|
|
18
|
+
export declare function sessionCheck(sessionId: string): Promise<boolean>;
|
|
19
|
+
/**
|
|
20
|
+
* Refresh a session's expiry time.
|
|
21
|
+
*/
|
|
22
|
+
export declare function sessionRefresh(sessionId: string, ttlMs?: unknown): Promise<boolean>;
|
|
23
|
+
export declare const SessionAuth: {
|
|
24
|
+
login: unknown;
|
|
25
|
+
logout: unknown;
|
|
26
|
+
user: unknown;
|
|
27
|
+
check: unknown;
|
|
28
|
+
refresh: unknown
|
|
29
|
+
};
|
|
30
|
+
declare type UserModel = InstanceType<typeof User>;
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import type { AccessToken, CreateClientOptions, CreateClientResult, OAuthClient, PersonalAccessTokenResult, RefreshTokenResult, TokenScopes } from '@stacksjs/types';
|
|
2
|
+
/**
|
|
3
|
+
* Get all access tokens for a user
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* import { tokens } from '@stacksjs/auth'
|
|
7
|
+
* const userTokens = await tokens(user.id)
|
|
8
|
+
*/
|
|
9
|
+
export declare function tokens(userId: number): Promise<AccessToken[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Get a specific token by its plain text value
|
|
12
|
+
* Uses hash comparison for security
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { findToken } from '@stacksjs/auth'
|
|
16
|
+
* const token = await findToken('abc123...')
|
|
17
|
+
*/
|
|
18
|
+
export declare function findToken(plainTextToken: string): Promise<AccessToken | null>;
|
|
19
|
+
/**
|
|
20
|
+
* Get the current access token from the request context
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* import { currentAccessToken } from '@stacksjs/auth'
|
|
24
|
+
* const token = await currentAccessToken()
|
|
25
|
+
*/
|
|
26
|
+
export declare function currentAccessToken(): Promise<AccessToken | null>;
|
|
27
|
+
/**
|
|
28
|
+
* Check if the current token has a given scope/ability
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* import { tokenCan } from '@stacksjs/auth'
|
|
32
|
+
* if (await tokenCan('posts:create')) {
|
|
33
|
+
* // user can create posts
|
|
34
|
+
* }
|
|
35
|
+
*/
|
|
36
|
+
export declare function tokenCan(scope: string): Promise<boolean>;
|
|
37
|
+
/**
|
|
38
|
+
* Check if the current token does NOT have a given scope/ability
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* import { tokenCant } from '@stacksjs/auth'
|
|
42
|
+
* if (await tokenCant('admin')) {
|
|
43
|
+
* throw new Error('Admin access required')
|
|
44
|
+
* }
|
|
45
|
+
*/
|
|
46
|
+
export declare function tokenCant(scope: string): Promise<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* Check if token has ALL of the given scopes
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* import { tokenCanAll } from '@stacksjs/auth'
|
|
52
|
+
* if (await tokenCanAll(['posts:read', 'posts:write'])) {
|
|
53
|
+
* // user has both scopes
|
|
54
|
+
* }
|
|
55
|
+
*/
|
|
56
|
+
export declare function tokenCanAll(scopes: string[]): Promise<boolean>;
|
|
57
|
+
/**
|
|
58
|
+
* Check if token has ANY of the given scopes
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* import { tokenCanAny } from '@stacksjs/auth'
|
|
62
|
+
* if (await tokenCanAny(['admin', 'moderator'])) {
|
|
63
|
+
* // user has at least one of the scopes
|
|
64
|
+
* }
|
|
65
|
+
*/
|
|
66
|
+
export declare function tokenCanAny(scopes: string[]): Promise<boolean>;
|
|
67
|
+
/**
|
|
68
|
+
* Get all scopes/abilities for the current token
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* import { tokenAbilities } from '@stacksjs/auth'
|
|
72
|
+
* const abilities = await tokenAbilities()
|
|
73
|
+
* // ['read', 'write', 'posts:create']
|
|
74
|
+
*/
|
|
75
|
+
export declare function tokenAbilities(): Promise<string[]>;
|
|
76
|
+
/**
|
|
77
|
+
* Create a new personal access token for a user
|
|
78
|
+
* Tokens are hashed before storage for security
|
|
79
|
+
*
|
|
80
|
+
* @param userId - The user ID to create the token for
|
|
81
|
+
* @param name - A name/description for the token
|
|
82
|
+
* @param scopes - Array of scopes/abilities for the token
|
|
83
|
+
* @param options - Additional options
|
|
84
|
+
* @param options.expiresInMinutes - Token expiry in minutes (default: 60)
|
|
85
|
+
* @param options.withRefreshToken - Whether to create a refresh token (default: true)
|
|
86
|
+
* @param options.refreshExpiresInDays - Refresh token expiry in days (default: 30)
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* import { createToken } from '@stacksjs/auth'
|
|
90
|
+
* const result = await createToken(user.id, 'My API Token', ['read', 'write'])
|
|
91
|
+
* console.log(result.plainTextToken) // Save this - it won't be shown again!
|
|
92
|
+
* console.log(result.refreshToken) // Use this to get new access tokens
|
|
93
|
+
*/
|
|
94
|
+
export declare function createToken(userId: number, name?: string, scopes?: string[], options?: {
|
|
95
|
+
expiresInMinutes?: number
|
|
96
|
+
withRefreshToken?: boolean
|
|
97
|
+
refreshExpiresInDays?: number
|
|
98
|
+
}): Promise<PersonalAccessTokenResult>;
|
|
99
|
+
/**
|
|
100
|
+
* Exchange a refresh token for a new access token
|
|
101
|
+
*
|
|
102
|
+
* @param refreshTokenPlain - The plain text refresh token
|
|
103
|
+
* @param options - Additional options
|
|
104
|
+
* @param options.expiresInMinutes - New access token expiry in minutes (default: 60)
|
|
105
|
+
* @param options.refreshExpiresInDays - New refresh token expiry in days (default: 30)
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* import { refreshToken } from '@stacksjs/auth'
|
|
109
|
+
* const result = await refreshToken('your-refresh-token')
|
|
110
|
+
* // Use result.plainTextToken as new access token
|
|
111
|
+
* // Use result.refreshToken as new refresh token (old one is revoked)
|
|
112
|
+
*/
|
|
113
|
+
export declare function refreshToken(refreshTokenPlain: string, options?: {
|
|
114
|
+
expiresInMinutes?: number
|
|
115
|
+
refreshExpiresInDays?: number
|
|
116
|
+
}): Promise<RefreshTokenResult>;
|
|
117
|
+
/**
|
|
118
|
+
* Validate a refresh token without exchanging it
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* import { validateRefreshToken } from '@stacksjs/auth'
|
|
122
|
+
* const isValid = await validateRefreshToken('your-refresh-token')
|
|
123
|
+
*/
|
|
124
|
+
export declare function validateRefreshToken(refreshTokenPlain: string): Promise<boolean>;
|
|
125
|
+
/**
|
|
126
|
+
* Revoke a specific refresh token
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* import { revokeRefreshToken } from '@stacksjs/auth'
|
|
130
|
+
* await revokeRefreshToken('your-refresh-token')
|
|
131
|
+
*/
|
|
132
|
+
export declare function revokeRefreshToken(refreshTokenPlain: string): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Revoke all refresh tokens for a user
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* import { revokeAllRefreshTokens } from '@stacksjs/auth'
|
|
138
|
+
* await revokeAllRefreshTokens(user.id)
|
|
139
|
+
*/
|
|
140
|
+
export declare function revokeAllRefreshTokens(userId: number): Promise<void>;
|
|
141
|
+
/**
|
|
142
|
+
* Delete expired refresh tokens (cleanup)
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* import { deleteExpiredRefreshTokens } from '@stacksjs/auth'
|
|
146
|
+
* const count = await deleteExpiredRefreshTokens()
|
|
147
|
+
*/
|
|
148
|
+
export declare function deleteExpiredRefreshTokens(): Promise<number>;
|
|
149
|
+
/**
|
|
150
|
+
* Delete revoked refresh tokens older than specified days
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* import { deleteRevokedRefreshTokens } from '@stacksjs/auth'
|
|
154
|
+
* const count = await deleteRevokedRefreshTokens(7)
|
|
155
|
+
*/
|
|
156
|
+
export declare function deleteRevokedRefreshTokens(daysOld?: number): Promise<number>;
|
|
157
|
+
/**
|
|
158
|
+
* Revoke a specific access token
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* import { revokeToken } from '@stacksjs/auth'
|
|
162
|
+
* await revokeToken('abc123...')
|
|
163
|
+
*/
|
|
164
|
+
export declare function revokeToken(plainTextToken: string): Promise<void>;
|
|
165
|
+
/**
|
|
166
|
+
* Revoke a token by its ID
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* import { revokeTokenById } from '@stacksjs/auth'
|
|
170
|
+
* await revokeTokenById(123)
|
|
171
|
+
*/
|
|
172
|
+
export declare function revokeTokenById(tokenId: number): Promise<void>;
|
|
173
|
+
/**
|
|
174
|
+
* Revoke all tokens for a user
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* import { revokeAllTokens } from '@stacksjs/auth'
|
|
178
|
+
* await revokeAllTokens(user.id)
|
|
179
|
+
*/
|
|
180
|
+
export declare function revokeAllTokens(userId: number): Promise<void>;
|
|
181
|
+
/**
|
|
182
|
+
* Revoke all tokens except the current one
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* import { revokeOtherTokens } from '@stacksjs/auth'
|
|
186
|
+
* await revokeOtherTokens(user.id)
|
|
187
|
+
*/
|
|
188
|
+
export declare function revokeOtherTokens(userId: number): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* Delete expired tokens (cleanup)
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* import { deleteExpiredTokens } from '@stacksjs/auth'
|
|
194
|
+
* const count = await deleteExpiredTokens()
|
|
195
|
+
*/
|
|
196
|
+
export declare function deleteExpiredTokens(): Promise<number>;
|
|
197
|
+
/**
|
|
198
|
+
* Delete revoked tokens older than specified days
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* import { deleteRevokedTokens } from '@stacksjs/auth'
|
|
202
|
+
* const count = await deleteRevokedTokens(30) // older than 30 days
|
|
203
|
+
*/
|
|
204
|
+
export declare function deleteRevokedTokens(daysOld?: number): Promise<number>;
|
|
205
|
+
/**
|
|
206
|
+
* Get all OAuth clients for a user
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* import { clients } from '@stacksjs/auth'
|
|
210
|
+
* const userClients = await clients(user.id)
|
|
211
|
+
*/
|
|
212
|
+
export declare function clients(userId: number): Promise<OAuthClient[]>;
|
|
213
|
+
/**
|
|
214
|
+
* Get a specific OAuth client by ID
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* import { findClient } from '@stacksjs/auth'
|
|
218
|
+
* const client = await findClient(1)
|
|
219
|
+
*/
|
|
220
|
+
export declare function findClient(clientId: number): Promise<OAuthClient | null>;
|
|
221
|
+
/**
|
|
222
|
+
* Create a new OAuth client
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* import { createClient } from '@stacksjs/auth'
|
|
226
|
+
* const client = await createClient({
|
|
227
|
+
* name: 'My App',
|
|
228
|
+
* redirect: 'https://myapp.com/callback'
|
|
229
|
+
* })
|
|
230
|
+
*/
|
|
231
|
+
export declare function createClient(options: CreateClientOptions): Promise<CreateClientResult>;
|
|
232
|
+
/**
|
|
233
|
+
* Revoke an OAuth client
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* import { revokeClient } from '@stacksjs/auth'
|
|
237
|
+
* await revokeClient(1)
|
|
238
|
+
*/
|
|
239
|
+
export declare function revokeClient(clientId: number): Promise<void>;
|
|
240
|
+
// ============================================================================
|
|
241
|
+
// HELPER FUNCTIONS
|
|
242
|
+
// ============================================================================
|
|
243
|
+
export declare function parseScopes(scopes: string | string[] | null | undefined): TokenScopes;
|
|
244
|
+
/**
|
|
245
|
+
* Alias for currentAccessToken
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* import { token } from '@stacksjs/auth'
|
|
249
|
+
* const t = await token()
|
|
250
|
+
*/
|
|
251
|
+
export declare const token: unknown;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { UserModel as OrmUserModel } from '@stacksjs/orm';
|
|
2
|
+
/**
|
|
3
|
+
* Get the currently authenticated user
|
|
4
|
+
*
|
|
5
|
+
* This is the primary way to get the authenticated user in your application.
|
|
6
|
+
* It first checks if the user was already set by the auth middleware,
|
|
7
|
+
* then falls back to validating the bearer token.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import { authUser } from '@stacksjs/auth'
|
|
11
|
+
*
|
|
12
|
+
* const user = await authUser()
|
|
13
|
+
* if (user) {
|
|
14
|
+
* console.log('Logged in as:', user.email)
|
|
15
|
+
* }
|
|
16
|
+
*/
|
|
17
|
+
export declare function authUser(): Promise<UserModel | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* Alias for authUser() - for backwards compatibility
|
|
20
|
+
* @deprecated Use authUser() instead
|
|
21
|
+
*/
|
|
22
|
+
export declare function getCurrentUser(): Promise<UserModel | undefined>;
|
|
23
|
+
export declare function check(): Promise<boolean>;
|
|
24
|
+
export declare function id(): Promise<number | undefined>;
|
|
25
|
+
export declare function email(): Promise<string | undefined>;
|
|
26
|
+
export declare function name(): Promise<string | undefined>;
|
|
27
|
+
export declare function isAuthenticated(): Promise<boolean>;
|
|
28
|
+
export declare function logout(): Promise<void>;
|
|
29
|
+
export declare function refresh(): Promise<void>;
|
|
30
|
+
// Local aliases — keep the existing `UserModel` / `UserJsonResponse` symbols
|
|
31
|
+
// in this module while sourcing the underlying type from the ORM.
|
|
32
|
+
declare type UserModel = OrmUserModel;
|
|
33
|
+
declare type UserJsonResponse = OrmUserModel;
|
|
34
|
+
export type AuthUser = UserJsonResponse;
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/auth",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.70.
|
|
4
|
+
"version": "0.70.25",
|
|
5
5
|
"description": "A more simplistic way to authenticate.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
|
-
"contributors": [
|
|
7
|
+
"contributors": [
|
|
8
|
+
"Chris Breuer <chris@stacksjs.com>"
|
|
9
|
+
],
|
|
8
10
|
"license": "MIT",
|
|
9
11
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
10
12
|
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/auth#readme",
|
|
@@ -16,31 +18,39 @@
|
|
|
16
18
|
"bugs": {
|
|
17
19
|
"url": "https://github.com/stacksjs/stacks/issues"
|
|
18
20
|
},
|
|
19
|
-
"keywords": [
|
|
21
|
+
"keywords": [
|
|
22
|
+
"auth",
|
|
23
|
+
"authenticate",
|
|
24
|
+
"stacks"
|
|
25
|
+
],
|
|
20
26
|
"exports": {
|
|
21
27
|
".": {
|
|
28
|
+
"bun": "./src/index.ts",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
22
30
|
"import": "./dist/index.js"
|
|
23
31
|
},
|
|
24
32
|
"./*": {
|
|
33
|
+
"bun": "./src/*",
|
|
25
34
|
"import": "./dist/*"
|
|
26
35
|
}
|
|
27
36
|
},
|
|
28
37
|
"module": "dist/index.js",
|
|
29
38
|
"types": "dist/index.d.ts",
|
|
30
|
-
"files": [
|
|
39
|
+
"files": [
|
|
40
|
+
"README.md",
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
31
43
|
"scripts": {
|
|
32
44
|
"build": "bun build.ts",
|
|
33
45
|
"typecheck": "bun tsc --noEmit",
|
|
34
46
|
"prepublishOnly": "bun run build"
|
|
35
47
|
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@stacksjs/ts-auth": "^0.4.1"
|
|
50
|
+
},
|
|
36
51
|
"devDependencies": {
|
|
37
|
-
"
|
|
38
|
-
"@
|
|
39
|
-
"@stacksjs/
|
|
40
|
-
"@stacksjs/error-handling": "0.70.22",
|
|
41
|
-
"@stacksjs/router": "0.70.22",
|
|
42
|
-
"@types/qrcode": "^1.5.5",
|
|
43
|
-
"otplib": "^12.0.1",
|
|
44
|
-
"qrcode": "^1.5.4"
|
|
52
|
+
"better-dx": "^0.2.12",
|
|
53
|
+
"@stacksjs/error-handling": "0.70.23",
|
|
54
|
+
"@stacksjs/router": "0.70.23"
|
|
45
55
|
}
|
|
46
56
|
}
|