Skip to content

Commit a15b817

Browse files
committed
doc: update docblocks
1 parent ce6b53b commit a15b817

File tree

15 files changed

+309
-18
lines changed

15 files changed

+309
-18
lines changed

modules/access_tokens_guard/define_config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ import type {
2121

2222
/**
2323
* Configures access tokens guard for authentication
24+
*
25+
* @param config - Configuration object containing the user provider
26+
*
27+
* @example
28+
* const guard = tokensGuard({
29+
* provider: tokensUserProvider({
30+
* model: () => import('#models/user'),
31+
* tokens: 'accessTokens'
32+
* })
33+
* })
2434
*/
2535
export function tokensGuard<
2636
UserProvider extends AccessTokensUserProviderContract<unknown>,
@@ -40,6 +50,14 @@ export function tokensGuard<
4050
/**
4151
* Configures user provider that uses Lucid models to verify
4252
* access tokens and find users during authentication.
53+
*
54+
* @param config - Configuration options for the Lucid user provider
55+
*
56+
* @example
57+
* const userProvider = tokensUserProvider({
58+
* model: () => import('#models/user'),
59+
* tokens: 'accessTokens'
60+
* })
4361
*/
4462
export function tokensUserProvider<
4563
TokenableProperty extends string,

modules/access_tokens_guard/types.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,16 @@ export type AccessTokenDbColumns = {
128128
}
129129

130130
/**
131-
* Access token providers are used verify an access token
132-
* during authentication
131+
* Access token providers are used to verify an access token
132+
* during authentication and manage token lifecycle
133133
*/
134134
export interface AccessTokensProviderContract<Tokenable extends LucidModel> {
135135
/**
136136
* Create a token for a given user
137+
*
138+
* @param user - The user instance to create a token for
139+
* @param abilities - Optional array of abilities the token should have
140+
* @param options - Optional token configuration including name and expiration
137141
*/
138142
create(
139143
user: InstanceType<Tokenable>,
@@ -147,11 +151,15 @@ export interface AccessTokensProviderContract<Tokenable extends LucidModel> {
147151
/**
148152
* Verifies a publicly shared access token and returns an
149153
* access token for it.
154+
*
155+
* @param tokenValue - The token value to verify
150156
*/
151157
verify(tokenValue: Secret<string>): Promise<AccessToken | null>
152158

153159
/**
154160
* Invalidates a token identified by its publicly shared token
161+
*
162+
* @param tokenValue - The token value to invalidate
155163
*/
156164
invalidate(tokenValue: Secret<string>): Promise<boolean>
157165
}
@@ -182,11 +190,18 @@ export type AccessTokensLucidUserProviderOptions<
182190
* and the guard.
183191
*
184192
* The guard is user provider agnostic and therefore it
185-
* needs a adapter to known some basic info about the
193+
* needs an adapter to know some basic info about the
186194
* user.
187195
*/
188196
export type AccessTokensGuardUser<RealUser> = {
197+
/**
198+
* Get the unique identifier for the user
199+
*/
189200
getId(): string | number | BigInt
201+
202+
/**
203+
* Get the original user object from the provider
204+
*/
190205
getOriginal(): RealUser
191206
}
192207

@@ -200,11 +215,17 @@ export interface AccessTokensUserProviderContract<RealUser> {
200215
/**
201216
* Create a user object that acts as an adapter between
202217
* the guard and real user value.
218+
*
219+
* @param user - The real user object from the provider
203220
*/
204221
createUserForGuard(user: RealUser): Promise<AccessTokensGuardUser<RealUser>>
205222

206223
/**
207224
* Create a token for a given user
225+
*
226+
* @param user - The user to create a token for
227+
* @param abilities - Optional array of abilities the token should have
228+
* @param options - Optional token configuration including name and expiration
208229
*/
209230
createToken(
210231
user: RealUser,
@@ -217,16 +238,22 @@ export interface AccessTokensUserProviderContract<RealUser> {
217238

218239
/**
219240
* Invalidates a token identified by its publicly shared token.
241+
*
242+
* @param tokenValue - The token value to invalidate
220243
*/
221244
invalidateToken(tokenValue: Secret<string>): Promise<boolean>
222245

223246
/**
224247
* Find a user by the user id.
248+
*
249+
* @param identifier - The unique identifier of the user
225250
*/
226251
findById(identifier: string | number | BigInt): Promise<AccessTokensGuardUser<RealUser> | null>
227252

228253
/**
229254
* Verify a token by its publicly shared value.
255+
*
256+
* @param tokenValue - The token value to verify
230257
*/
231258
verifyToken(tokenValue: Secret<string>): Promise<AccessToken | null>
232259
}

modules/basic_auth_guard/define_config.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ import type {
2020
} from './types.ts'
2121

2222
/**
23-
* Configures basic auth guard for authentication
23+
* Configures basic auth guard for authentication using HTTP Basic Authentication
24+
*
25+
* @param config - Configuration object containing the user provider
26+
*
27+
* @example
28+
* const guard = basicAuthGuard({
29+
* provider: basicAuthUserProvider({
30+
* model: () => import('#models/user')
31+
* })
32+
* })
2433
*/
2534
export function basicAuthGuard<
2635
UserProvider extends BasicAuthUserProviderContract<unknown>,
@@ -39,7 +48,14 @@ export function basicAuthGuard<
3948

4049
/**
4150
* Configures user provider that uses Lucid models to authenticate
42-
* users using basic auth
51+
* users using basic auth credentials
52+
*
53+
* @param config - Configuration options for the Lucid user provider
54+
*
55+
* @example
56+
* const userProvider = basicAuthUserProvider({
57+
* model: () => import('#models/user')
58+
* })
4359
*/
4460
export function basicAuthUserProvider<Model extends LucidAuthenticatable>(
4561
config: BasicAuthLucidUserProviderOptions<Model>

modules/basic_auth_guard/guard.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ export class BasicAuthGuard<
9191
*/
9292
user?: UserProvider[typeof PROVIDER_REAL_USER]
9393

94+
/**
95+
* Creates a new BasicAuthGuard instance
96+
*
97+
* @param name - Unique name for the guard instance
98+
* @param ctx - HTTP context for the current request
99+
* @param emitter - Event emitter for guard events
100+
* @param userProvider - User provider for credential verification
101+
*
102+
* @example
103+
* const guard = new BasicAuthGuard(
104+
* 'basic',
105+
* ctx,
106+
* emitter,
107+
* new BasicAuthLucidUserProvider()
108+
* )
109+
*/
94110
constructor(
95111
name: string,
96112
ctx: HttpContext,
@@ -143,6 +159,12 @@ export class BasicAuthGuard<
143159
/**
144160
* Returns an instance of the authenticated user. Or throws
145161
* an exception if the request is not authenticated.
162+
*
163+
* @throws {E_UNAUTHORIZED_ACCESS} When user is not authenticated
164+
*
165+
* @example
166+
* const user = guard.getUserOrFail()
167+
* console.log('User:', user.email)
146168
*/
147169
getUserOrFail(): UserProvider[typeof PROVIDER_REAL_USER] {
148170
if (!this.user) {
@@ -158,7 +180,15 @@ export class BasicAuthGuard<
158180
* Authenticates the incoming HTTP request by looking for BasicAuth
159181
* credentials inside the request authorization header.
160182
*
161-
* Returns the authenticated user or throws an exception.
183+
* @throws {E_UNAUTHORIZED_ACCESS} When authentication fails
184+
*
185+
* @example
186+
* try {
187+
* const user = await guard.authenticate()
188+
* console.log('Authenticated as:', user.email)
189+
* } catch (error) {
190+
* console.log('Authentication failed')
191+
* }
162192
*/
163193
async authenticate(): Promise<UserProvider[typeof PROVIDER_REAL_USER]> {
164194
/**
@@ -205,6 +235,12 @@ export class BasicAuthGuard<
205235
*
206236
* The method returns a boolean indicating if the authentication
207237
* succeeded or failed.
238+
*
239+
* @example
240+
* const isAuthenticated = await guard.check()
241+
* if (isAuthenticated) {
242+
* console.log('User is authenticated:', guard.user.email)
243+
* }
208244
*/
209245
async check(): Promise<boolean> {
210246
try {
@@ -220,8 +256,15 @@ export class BasicAuthGuard<
220256
}
221257

222258
/**
223-
* Does not support authenticating as client. Instead use "basicAuth"
224-
* helper on Japa APIClient
259+
* Returns the Authorization header clients can use to authenticate
260+
* the request using basic auth.
261+
*
262+
* @param uid - The username or user identifier
263+
* @param password - The user's password
264+
*
265+
* @example
266+
* const clientAuth = await guard.authenticateAsClient('[email protected]', 'secret')
267+
* // Use clientAuth.headers.authorization in API tests
225268
*/
226269
async authenticateAsClient(uid: string, password: string): Promise<AuthClientResponse> {
227270
return {

modules/basic_auth_guard/types.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export type LucidAuthenticatable = LucidModel & {
2020
/**
2121
* Verify credentials method should return the user instance
2222
* or throw an exception
23+
*
24+
* @param uid - The username or user identifier
25+
* @param password - The password to verify
2326
*/
2427
verifyCredentials(uid: string, password: string): Promise<InstanceType<LucidAuthenticatable>>
2528
}
@@ -41,11 +44,18 @@ export type BasicAuthLucidUserProviderOptions<Model extends LucidAuthenticatable
4144
* and the guard.
4245
*
4346
* The guard is user provider agnostic and therefore it
44-
* needs a adapter to known some basic info about the
47+
* needs an adapter to know some basic info about the
4548
* user.
4649
*/
4750
export type BasicAuthGuardUser<RealUser> = {
51+
/**
52+
* Get the unique identifier for the user
53+
*/
4854
getId(): string | number | BigInt
55+
56+
/**
57+
* Get the original user object from the provider
58+
*/
4959
getOriginal(): RealUser
5060
}
5161

@@ -59,12 +69,17 @@ export interface BasicAuthUserProviderContract<RealUser> {
5969
/**
6070
* Create a user object that acts as an adapter between
6171
* the guard and real user value.
72+
*
73+
* @param user - The real user object from the provider
6274
*/
6375
createUserForGuard(user: RealUser): Promise<BasicAuthGuardUser<RealUser>>
6476

6577
/**
6678
* Verify user credentials and must return an instance of the
6779
* user back or null when the credentials are invalid
80+
*
81+
* @param uid - The username or user identifier
82+
* @param password - The password to verify
6883
*/
6984
verifyCredentials(uid: string, password: string): Promise<BasicAuthGuardUser<RealUser> | null>
7085
}

modules/basic_auth_guard/user_providers/lucid.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export class BasicAuthLucidUserProvider<
5757
/**
5858
* Imports the model from the provider, returns and caches it
5959
* for further operations.
60+
*
61+
* @example
62+
* const UserModel = await provider.getModel()
63+
* const user = await UserModel.find(1)
6064
*/
6165
protected async getModel() {
6266
if (this.model && !('hot' in import.meta)) {
@@ -70,6 +74,12 @@ export class BasicAuthLucidUserProvider<
7074

7175
/**
7276
* Creates an adapter user for the guard
77+
*
78+
* @param user - The user model instance
79+
*
80+
* @example
81+
* const guardUser = await provider.createUserForGuard(user)
82+
* console.log('User ID:', guardUser.getId())
7383
*/
7484
async createUserForGuard(
7585
user: InstanceType<UserModel>
@@ -102,6 +112,15 @@ export class BasicAuthLucidUserProvider<
102112

103113
/**
104114
* Verifies credentials using the underlying model
115+
*
116+
* @param uid - The username or user identifier
117+
* @param password - The password to verify
118+
*
119+
* @example
120+
* const guardUser = await provider.verifyCredentials('[email protected]', 'secret')
121+
* if (guardUser) {
122+
* console.log('Valid credentials')
123+
* }
105124
*/
106125
async verifyCredentials(
107126
uid: string,

modules/session_guard/define_config.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ import type {
2222
} from './types.ts'
2323

2424
/**
25-
* Configures session tokens guard for authentication
25+
* Configures session guard for authentication using HTTP sessions
26+
*
27+
* @param config - Configuration object containing the user provider and session options
28+
*
29+
* @example
30+
* const guard = sessionGuard({
31+
* useRememberMeTokens: false,
32+
* provider: sessionUserProvider({
33+
* model: () => import('#models/user')
34+
* })
35+
* })
2636
*/
2737
export function sessionGuard<
2838
UseRememberTokens extends boolean,
@@ -47,6 +57,13 @@ export function sessionGuard<
4757
/**
4858
* Configures user provider that uses Lucid models to authenticate
4959
* users using sessions
60+
*
61+
* @param config - Configuration options for the Lucid user provider
62+
*
63+
* @example
64+
* const userProvider = sessionUserProvider({
65+
* model: () => import('#models/user')
66+
* })
5067
*/
5168
export function sessionUserProvider<Model extends LucidAuthenticatable>(
5269
config: SessionLucidUserProviderOptions<Model>

0 commit comments

Comments
 (0)