@adonisjs/auth 10.0.0-next.4 → 10.0.0-next.5

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.
@@ -6,6 +6,16 @@ import { AccessTokensLucidUserProvider } from './user_providers/lucid.ts';
6
6
  import type { LucidTokenable, AccessTokensUserProviderContract, AccessTokensLucidUserProviderOptions } from './types.ts';
7
7
  /**
8
8
  * Configures access tokens guard for authentication
9
+ *
10
+ * @param config - Configuration object containing the user provider
11
+ *
12
+ * @example
13
+ * const guard = tokensGuard({
14
+ * provider: tokensUserProvider({
15
+ * model: () => import('#models/user'),
16
+ * tokens: 'accessTokens'
17
+ * })
18
+ * })
9
19
  */
10
20
  export declare function tokensGuard<UserProvider extends AccessTokensUserProviderContract<unknown>>(config: {
11
21
  provider: UserProvider | ConfigProvider<UserProvider>;
@@ -13,5 +23,13 @@ export declare function tokensGuard<UserProvider extends AccessTokensUserProvide
13
23
  /**
14
24
  * Configures user provider that uses Lucid models to verify
15
25
  * access tokens and find users during authentication.
26
+ *
27
+ * @param config - Configuration options for the Lucid user provider
28
+ *
29
+ * @example
30
+ * const userProvider = tokensUserProvider({
31
+ * model: () => import('#models/user'),
32
+ * tokens: 'accessTokens'
33
+ * })
16
34
  */
17
35
  export declare function tokensUserProvider<TokenableProperty extends string, Model extends LucidTokenable<TokenableProperty>>(config: AccessTokensLucidUserProviderOptions<TokenableProperty, Model>): AccessTokensLucidUserProvider<TokenableProperty, Model>;
@@ -102,12 +102,16 @@ export type AccessTokenDbColumns = {
102
102
  last_used_at: null | Date;
103
103
  };
104
104
  /**
105
- * Access token providers are used verify an access token
106
- * during authentication
105
+ * Access token providers are used to verify an access token
106
+ * during authentication and manage token lifecycle
107
107
  */
108
108
  export interface AccessTokensProviderContract<Tokenable extends LucidModel> {
109
109
  /**
110
110
  * Create a token for a given user
111
+ *
112
+ * @param user - The user instance to create a token for
113
+ * @param abilities - Optional array of abilities the token should have
114
+ * @param options - Optional token configuration including name and expiration
111
115
  */
112
116
  create(user: InstanceType<Tokenable>, abilities?: string[], options?: {
113
117
  name?: string;
@@ -116,10 +120,14 @@ export interface AccessTokensProviderContract<Tokenable extends LucidModel> {
116
120
  /**
117
121
  * Verifies a publicly shared access token and returns an
118
122
  * access token for it.
123
+ *
124
+ * @param tokenValue - The token value to verify
119
125
  */
120
126
  verify(tokenValue: Secret<string>): Promise<AccessToken | null>;
121
127
  /**
122
128
  * Invalidates a token identified by its publicly shared token
129
+ *
130
+ * @param tokenValue - The token value to invalidate
123
131
  */
124
132
  invalidate(tokenValue: Secret<string>): Promise<boolean>;
125
133
  }
@@ -146,11 +154,17 @@ export type AccessTokensLucidUserProviderOptions<TokenableProperty extends strin
146
154
  * and the guard.
147
155
  *
148
156
  * The guard is user provider agnostic and therefore it
149
- * needs a adapter to known some basic info about the
157
+ * needs an adapter to know some basic info about the
150
158
  * user.
151
159
  */
152
160
  export type AccessTokensGuardUser<RealUser> = {
161
+ /**
162
+ * Get the unique identifier for the user
163
+ */
153
164
  getId(): string | number | BigInt;
165
+ /**
166
+ * Get the original user object from the provider
167
+ */
154
168
  getOriginal(): RealUser;
155
169
  };
156
170
  /**
@@ -162,10 +176,16 @@ export interface AccessTokensUserProviderContract<RealUser> {
162
176
  /**
163
177
  * Create a user object that acts as an adapter between
164
178
  * the guard and real user value.
179
+ *
180
+ * @param user - The real user object from the provider
165
181
  */
166
182
  createUserForGuard(user: RealUser): Promise<AccessTokensGuardUser<RealUser>>;
167
183
  /**
168
184
  * Create a token for a given user
185
+ *
186
+ * @param user - The user to create a token for
187
+ * @param abilities - Optional array of abilities the token should have
188
+ * @param options - Optional token configuration including name and expiration
169
189
  */
170
190
  createToken(user: RealUser, abilities?: string[], options?: {
171
191
  name?: string;
@@ -173,14 +193,20 @@ export interface AccessTokensUserProviderContract<RealUser> {
173
193
  }): Promise<AccessToken>;
174
194
  /**
175
195
  * Invalidates a token identified by its publicly shared token.
196
+ *
197
+ * @param tokenValue - The token value to invalidate
176
198
  */
177
199
  invalidateToken(tokenValue: Secret<string>): Promise<boolean>;
178
200
  /**
179
201
  * Find a user by the user id.
202
+ *
203
+ * @param identifier - The unique identifier of the user
180
204
  */
181
205
  findById(identifier: string | number | BigInt): Promise<AccessTokensGuardUser<RealUser> | null>;
182
206
  /**
183
207
  * Verify a token by its publicly shared value.
208
+ *
209
+ * @param tokenValue - The token value to verify
184
210
  */
185
211
  verifyToken(tokenValue: Secret<string>): Promise<AccessToken | null>;
186
212
  }
@@ -5,13 +5,29 @@ import type { GuardConfigProvider } from '../../src/types.ts';
5
5
  import { BasicAuthLucidUserProvider } from './user_providers/lucid.ts';
6
6
  import type { LucidAuthenticatable, BasicAuthUserProviderContract, BasicAuthLucidUserProviderOptions } from './types.ts';
7
7
  /**
8
- * Configures basic auth guard for authentication
8
+ * Configures basic auth guard for authentication using HTTP Basic Authentication
9
+ *
10
+ * @param config - Configuration object containing the user provider
11
+ *
12
+ * @example
13
+ * const guard = basicAuthGuard({
14
+ * provider: basicAuthUserProvider({
15
+ * model: () => import('#models/user')
16
+ * })
17
+ * })
9
18
  */
10
19
  export declare function basicAuthGuard<UserProvider extends BasicAuthUserProviderContract<unknown>>(config: {
11
20
  provider: UserProvider | ConfigProvider<UserProvider>;
12
21
  }): GuardConfigProvider<(ctx: HttpContext) => BasicAuthGuard<UserProvider>>;
13
22
  /**
14
23
  * Configures user provider that uses Lucid models to authenticate
15
- * users using basic auth
24
+ * users using basic auth credentials
25
+ *
26
+ * @param config - Configuration options for the Lucid user provider
27
+ *
28
+ * @example
29
+ * const userProvider = basicAuthUserProvider({
30
+ * model: () => import('#models/user')
31
+ * })
16
32
  */
17
33
  export declare function basicAuthUserProvider<Model extends LucidAuthenticatable>(config: BasicAuthLucidUserProviderOptions<Model>): BasicAuthLucidUserProvider<Model>;
@@ -51,17 +51,47 @@ export declare class BasicAuthGuard<UserProvider extends BasicAuthUserProviderCo
51
51
  * the request is not authenticated.
52
52
  */
53
53
  user?: UserProvider[typeof PROVIDER_REAL_USER];
54
+ /**
55
+ * Creates a new BasicAuthGuard instance
56
+ *
57
+ * @param name - Unique name for the guard instance
58
+ * @param ctx - HTTP context for the current request
59
+ * @param emitter - Event emitter for guard events
60
+ * @param userProvider - User provider for credential verification
61
+ *
62
+ * @example
63
+ * const guard = new BasicAuthGuard(
64
+ * 'basic',
65
+ * ctx,
66
+ * emitter,
67
+ * new BasicAuthLucidUserProvider()
68
+ * )
69
+ */
54
70
  constructor(name: string, ctx: HttpContext, emitter: EmitterLike<BasicAuthGuardEvents<UserProvider[typeof PROVIDER_REAL_USER]>>, userProvider: UserProvider);
55
71
  /**
56
72
  * Returns an instance of the authenticated user. Or throws
57
73
  * an exception if the request is not authenticated.
74
+ *
75
+ * @throws {E_UNAUTHORIZED_ACCESS} When user is not authenticated
76
+ *
77
+ * @example
78
+ * const user = guard.getUserOrFail()
79
+ * console.log('User:', user.email)
58
80
  */
59
81
  getUserOrFail(): UserProvider[typeof PROVIDER_REAL_USER];
60
82
  /**
61
83
  * Authenticates the incoming HTTP request by looking for BasicAuth
62
84
  * credentials inside the request authorization header.
63
85
  *
64
- * Returns the authenticated user or throws an exception.
86
+ * @throws {E_UNAUTHORIZED_ACCESS} When authentication fails
87
+ *
88
+ * @example
89
+ * try {
90
+ * const user = await guard.authenticate()
91
+ * console.log('Authenticated as:', user.email)
92
+ * } catch (error) {
93
+ * console.log('Authentication failed')
94
+ * }
65
95
  */
66
96
  authenticate(): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
67
97
  /**
@@ -69,11 +99,24 @@ export declare class BasicAuthGuard<UserProvider extends BasicAuthUserProviderCo
69
99
  *
70
100
  * The method returns a boolean indicating if the authentication
71
101
  * succeeded or failed.
102
+ *
103
+ * @example
104
+ * const isAuthenticated = await guard.check()
105
+ * if (isAuthenticated) {
106
+ * console.log('User is authenticated:', guard.user.email)
107
+ * }
72
108
  */
73
109
  check(): Promise<boolean>;
74
110
  /**
75
- * Does not support authenticating as client. Instead use "basicAuth"
76
- * helper on Japa APIClient
111
+ * Returns the Authorization header clients can use to authenticate
112
+ * the request using basic auth.
113
+ *
114
+ * @param uid - The username or user identifier
115
+ * @param password - The user's password
116
+ *
117
+ * @example
118
+ * const clientAuth = await guard.authenticateAsClient('user@example.com', 'secret')
119
+ * // Use clientAuth.headers.authorization in API tests
77
120
  */
78
121
  authenticateAsClient(uid: string, password: string): Promise<AuthClientResponse>;
79
122
  }
@@ -10,6 +10,9 @@ export type LucidAuthenticatable = LucidModel & {
10
10
  /**
11
11
  * Verify credentials method should return the user instance
12
12
  * or throw an exception
13
+ *
14
+ * @param uid - The username or user identifier
15
+ * @param password - The password to verify
13
16
  */
14
17
  verifyCredentials(uid: string, password: string): Promise<InstanceType<LucidAuthenticatable>>;
15
18
  };
@@ -31,11 +34,17 @@ export type BasicAuthLucidUserProviderOptions<Model extends LucidAuthenticatable
31
34
  * and the guard.
32
35
  *
33
36
  * The guard is user provider agnostic and therefore it
34
- * needs a adapter to known some basic info about the
37
+ * needs an adapter to know some basic info about the
35
38
  * user.
36
39
  */
37
40
  export type BasicAuthGuardUser<RealUser> = {
41
+ /**
42
+ * Get the unique identifier for the user
43
+ */
38
44
  getId(): string | number | BigInt;
45
+ /**
46
+ * Get the original user object from the provider
47
+ */
39
48
  getOriginal(): RealUser;
40
49
  };
41
50
  /**
@@ -47,11 +56,16 @@ export interface BasicAuthUserProviderContract<RealUser> {
47
56
  /**
48
57
  * Create a user object that acts as an adapter between
49
58
  * the guard and real user value.
59
+ *
60
+ * @param user - The real user object from the provider
50
61
  */
51
62
  createUserForGuard(user: RealUser): Promise<BasicAuthGuardUser<RealUser>>;
52
63
  /**
53
64
  * Verify user credentials and must return an instance of the
54
65
  * user back or null when the credentials are invalid
66
+ *
67
+ * @param uid - The username or user identifier
68
+ * @param password - The password to verify
55
69
  */
56
70
  verifyCredentials(uid: string, password: string): Promise<BasicAuthGuardUser<RealUser> | null>;
57
71
  }
@@ -39,14 +39,33 @@ export declare class BasicAuthLucidUserProvider<UserModel extends LucidAuthentic
39
39
  /**
40
40
  * Imports the model from the provider, returns and caches it
41
41
  * for further operations.
42
+ *
43
+ * @example
44
+ * const UserModel = await provider.getModel()
45
+ * const user = await UserModel.find(1)
42
46
  */
43
47
  protected getModel(): Promise<UserModel>;
44
48
  /**
45
49
  * Creates an adapter user for the guard
50
+ *
51
+ * @param user - The user model instance
52
+ *
53
+ * @example
54
+ * const guardUser = await provider.createUserForGuard(user)
55
+ * console.log('User ID:', guardUser.getId())
46
56
  */
47
57
  createUserForGuard(user: InstanceType<UserModel>): Promise<BasicAuthGuardUser<InstanceType<UserModel>>>;
48
58
  /**
49
59
  * Verifies credentials using the underlying model
60
+ *
61
+ * @param uid - The username or user identifier
62
+ * @param password - The password to verify
63
+ *
64
+ * @example
65
+ * const guardUser = await provider.verifyCredentials('user@example.com', 'secret')
66
+ * if (guardUser) {
67
+ * console.log('Valid credentials')
68
+ * }
50
69
  */
51
70
  verifyCredentials(uid: string, password: string): Promise<BasicAuthGuardUser<InstanceType<UserModel>> | null>;
52
71
  }
@@ -5,7 +5,17 @@ import type { GuardConfigProvider } from '../../src/types.ts';
5
5
  import { SessionLucidUserProvider } from './user_providers/lucid.ts';
6
6
  import type { SessionGuardOptions, LucidAuthenticatable, SessionUserProviderContract, SessionLucidUserProviderOptions, SessionWithTokensUserProviderContract } from './types.ts';
7
7
  /**
8
- * Configures session tokens guard for authentication
8
+ * Configures session guard for authentication using HTTP sessions
9
+ *
10
+ * @param config - Configuration object containing the user provider and session options
11
+ *
12
+ * @example
13
+ * const guard = sessionGuard({
14
+ * useRememberMeTokens: false,
15
+ * provider: sessionUserProvider({
16
+ * model: () => import('#models/user')
17
+ * })
18
+ * })
9
19
  */
10
20
  export declare function sessionGuard<UseRememberTokens extends boolean, UserProvider extends UseRememberTokens extends true ? SessionWithTokensUserProviderContract<unknown> : SessionUserProviderContract<unknown>>(config: {
11
21
  provider: UserProvider | ConfigProvider<UserProvider>;
@@ -13,5 +23,12 @@ export declare function sessionGuard<UseRememberTokens extends boolean, UserProv
13
23
  /**
14
24
  * Configures user provider that uses Lucid models to authenticate
15
25
  * users using sessions
26
+ *
27
+ * @param config - Configuration options for the Lucid user provider
28
+ *
29
+ * @example
30
+ * const userProvider = sessionUserProvider({
31
+ * model: () => import('#models/user')
32
+ * })
16
33
  */
17
34
  export declare function sessionUserProvider<Model extends LucidAuthenticatable>(config: SessionLucidUserProviderOptions<Model>): SessionLucidUserProvider<Model>;
@@ -106,32 +106,71 @@ export declare class SessionGuard<UseRememberTokens extends boolean, UserProvide
106
106
  /**
107
107
  * Returns an instance of the authenticated user. Or throws
108
108
  * an exception if the request is not authenticated.
109
+ *
110
+ * @throws {E_UNAUTHORIZED_ACCESS} When user is not authenticated
111
+ *
112
+ * @example
113
+ * const user = guard.getUserOrFail()
114
+ * console.log('User:', user.email)
109
115
  */
110
116
  getUserOrFail(): UserProvider[typeof PROVIDER_REAL_USER];
111
117
  /**
112
118
  * Login user using sessions. Optionally, you can also create
113
119
  * a remember me token to automatically login user when their
114
120
  * session expires.
121
+ *
122
+ * @param user - The user to login
123
+ * @param remember - Whether to create a remember me token
124
+ *
125
+ * @example
126
+ * await guard.login(user, true)
127
+ * console.log('User logged in with remember me token')
115
128
  */
116
129
  login(user: UserProvider[typeof PROVIDER_REAL_USER], remember?: boolean): Promise<void>;
117
130
  /**
118
131
  * Logout a user by removing its state from the session
119
132
  * store and delete the remember me cookie (if any).
133
+ *
134
+ * @example
135
+ * await guard.logout()
136
+ * console.log('User logged out successfully')
120
137
  */
121
138
  logout(): Promise<void>;
122
139
  /**
123
- * Authenticate the current HTTP request by verifying the bearer
124
- * token or fails with an exception
140
+ * Authenticate the current HTTP request by verifying the session
141
+ * or remember me token and fails with an exception if authentication fails
142
+ *
143
+ * @throws {E_UNAUTHORIZED_ACCESS} When authentication fails
144
+ *
145
+ * @example
146
+ * try {
147
+ * const user = await guard.authenticate()
148
+ * console.log('Authenticated as:', user.email)
149
+ * } catch (error) {
150
+ * console.log('Authentication failed')
151
+ * }
125
152
  */
126
153
  authenticate(): Promise<UserProvider[typeof PROVIDER_REAL_USER]>;
127
154
  /**
128
155
  * Silently check if the user is authenticated or not, without
129
156
  * throwing any exceptions
157
+ *
158
+ * @example
159
+ * const isAuthenticated = await guard.check()
160
+ * if (isAuthenticated) {
161
+ * console.log('User is authenticated:', guard.user.email)
162
+ * }
130
163
  */
131
164
  check(): Promise<boolean>;
132
165
  /**
133
166
  * Returns the session info for the clients to send during
134
167
  * an HTTP request to mark the user as logged-in.
168
+ *
169
+ * @param user - The user to authenticate as
170
+ *
171
+ * @example
172
+ * const clientAuth = await guard.authenticateAsClient(user)
173
+ * // Use clientAuth.session in API tests
135
174
  */
136
175
  authenticateAsClient(user: UserProvider[typeof PROVIDER_REAL_USER]): Promise<AuthClientResponse>;
137
176
  }
@@ -30,27 +30,39 @@ export type DbRememberMeTokensProviderOptions<TokenableModel extends LucidModel>
30
30
  tokenSecretLength?: number;
31
31
  };
32
32
  /**
33
- * Remember me token providers are used verify a remember me
33
+ * Remember me token providers are used to verify a remember me
34
34
  * token during authentication
35
35
  */
36
36
  export interface RememberMeTokensProviderContract<Tokenable extends LucidModel> {
37
37
  /**
38
38
  * Create a token for a given user
39
+ *
40
+ * @param user - The user instance to create a token for
41
+ * @param expiresIn - Token expiration time
39
42
  */
40
43
  create(user: InstanceType<Tokenable>, expiresIn: string | number): Promise<RememberMeToken>;
41
44
  /**
42
45
  * Verifies the remember me token shared as cookie and returns an
43
46
  * instance of remember me token
47
+ *
48
+ * @param tokenValue - The token value to verify
44
49
  */
45
50
  verify(tokenValue: Secret<string>): Promise<RememberMeToken | null>;
46
51
  /**
47
52
  * Delete token for a user by the token identifier.
53
+ *
54
+ * @param user - The user that owns the token
55
+ * @param identifier - The token identifier to delete
48
56
  */
49
57
  delete(user: InstanceType<Tokenable>, identifier: string | number | BigInt): Promise<number>;
50
58
  /**
51
59
  * Recycle an existing token by its id. Recycling tokens helps
52
60
  * detect compromised tokens.
53
61
  * https://web.archive.org/web/20130214051957/http://jaspan.com/improved_persistent_login_cookie_best_practice
62
+ *
63
+ * @param user - The user that owns the token
64
+ * @param identifier - The token identifier to recycle
65
+ * @param expiresIn - New expiration time
54
66
  */
55
67
  recycle(user: InstanceType<Tokenable>, identifier: string | number | BigInt, expiresIn: string | number): Promise<RememberMeToken>;
56
68
  }
@@ -111,11 +123,17 @@ export type RememberMeTokenDbColumns = {
111
123
  * and the guard.
112
124
  *
113
125
  * The guard is user provider agnostic and therefore it
114
- * needs a adapter to known some basic info about the
126
+ * needs an adapter to know some basic info about the
115
127
  * user.
116
128
  */
117
129
  export type SessionGuardUser<RealUser> = {
130
+ /**
131
+ * Get the unique identifier for the user
132
+ */
118
133
  getId(): string | number | BigInt;
134
+ /**
135
+ * Get the original user object from the provider
136
+ */
119
137
  getOriginal(): RealUser;
120
138
  };
121
139
  /**
@@ -127,10 +145,14 @@ export interface SessionUserProviderContract<RealUser> {
127
145
  /**
128
146
  * Create a user object that acts as an adapter between
129
147
  * the guard and real user value.
148
+ *
149
+ * @param user - The real user object from the provider
130
150
  */
131
151
  createUserForGuard(user: RealUser): Promise<SessionGuardUser<RealUser>>;
132
152
  /**
133
153
  * Find a user by their id.
154
+ *
155
+ * @param identifier - The unique identifier of the user
134
156
  */
135
157
  findById(identifier: string | number | BigInt): Promise<SessionGuardUser<RealUser> | null>;
136
158
  }
@@ -141,21 +163,33 @@ export interface SessionWithTokensUserProviderContract<RealUser> extends Session
141
163
  /**
142
164
  * Create a token for a given user. Must be implemented when
143
165
  * "supportsRememberMeTokens" flag is true
166
+ *
167
+ * @param user - The user to create a token for
168
+ * @param expiresIn - Token expiration time
144
169
  */
145
170
  createRememberToken(user: RealUser, expiresIn: string | number): Promise<RememberMeToken>;
146
171
  /**
147
172
  * Verify a token by its publicly shared value. Must be implemented when
148
173
  * "supportsRememberMeTokens" flag is true
174
+ *
175
+ * @param tokenValue - The token value to verify
149
176
  */
150
177
  verifyRememberToken(tokenValue: Secret<string>): Promise<RememberMeToken | null>;
151
178
  /**
152
179
  * Recycle a token for a user by the token identifier. Must be
153
180
  * implemented when "supportsRememberMeTokens" flag is true
181
+ *
182
+ * @param user - The user that owns the token
183
+ * @param tokenIdentifier - The token identifier to recycle
184
+ * @param expiresIn - New expiration time
154
185
  */
155
186
  recycleRememberToken(user: RealUser, tokenIdentifier: string | number | BigInt, expiresIn: string | number): Promise<RememberMeToken>;
156
187
  /**
157
188
  * Delete a token for a user by the token identifier. Must be
158
189
  * implemented when "supportsRememberMeTokens" flag is true
190
+ *
191
+ * @param user - The user that owns the token
192
+ * @param tokenIdentifier - The token identifier to delete
159
193
  */
160
194
  deleteRemeberToken(user: RealUser, tokenIdentifier: string | number | BigInt): Promise<number>;
161
195
  }
@@ -12,7 +12,11 @@ export declare class AuthManager<KnownGuards extends Record<string, GuardFactory
12
12
  guards: KnownGuards;
13
13
  };
14
14
  /**
15
- * Name of the default guard
15
+ * Name of the default guard configured in the auth configuration
16
+ *
17
+ * @example
18
+ * const manager = new AuthManager({ default: 'web', guards: {} })
19
+ * console.log(manager.defaultGuard) // 'web'
16
20
  */
17
21
  get defaultGuard(): keyof KnownGuards;
18
22
  /**
@@ -7,24 +7,41 @@ import type { GuardFactory } from './types.ts';
7
7
  export declare class Authenticator<KnownGuards extends Record<string, GuardFactory>> {
8
8
  #private;
9
9
  /**
10
- * Name of the default guard
10
+ * Name of the default guard configured in the auth configuration
11
+ *
12
+ * @example
13
+ * const defaultGuard = auth.defaultGuard
14
+ * console.log(defaultGuard) // 'web'
11
15
  */
12
16
  get defaultGuard(): keyof KnownGuards;
13
17
  /**
14
18
  * Reference to the guard using which the current
15
- * request has been authenticated.
19
+ * request has been authenticated. Returns undefined if
20
+ * authentication has not been attempted or failed.
21
+ *
22
+ * @example
23
+ * await auth.authenticate()
24
+ * console.log(auth.authenticatedViaGuard) // 'web'
16
25
  */
17
26
  get authenticatedViaGuard(): keyof KnownGuards | undefined;
18
27
  /**
19
28
  * A boolean to know if the current request has been authenticated. The
20
29
  * property returns false when "authenticate" or "authenticateUsing"
21
30
  * methods are not used.
31
+ *
32
+ * @example
33
+ * await auth.authenticate()
34
+ * console.log(auth.isAuthenticated) // true
22
35
  */
23
36
  get isAuthenticated(): boolean;
24
37
  /**
25
38
  * Reference to the currently authenticated user. The property returns
26
39
  * undefined when "authenticate" or "authenticateUsing" methods are
27
40
  * not used.
41
+ *
42
+ * @example
43
+ * await auth.authenticate()
44
+ * console.log(auth.user?.email)
28
45
  */
29
46
  get user(): {
30
47
  [K in keyof KnownGuards]: ReturnType<KnownGuards[K]>['user'];
@@ -34,6 +51,10 @@ export declare class Authenticator<KnownGuards extends Record<string, GuardFacto
34
51
  * the current request. The property returns false when the
35
52
  * "authenticate" or "authenticateUsing" methods are not
36
53
  * used.
54
+ *
55
+ * @example
56
+ * await auth.check()
57
+ * console.log(auth.authenticationAttempted) // true
37
58
  */
38
59
  get authenticationAttempted(): boolean;
39
60
  /**
@@ -8,7 +8,11 @@ import type { GuardFactory } from './types.ts';
8
8
  export declare class AuthenticatorClient<KnownGuards extends Record<string, GuardFactory>> {
9
9
  #private;
10
10
  /**
11
- * Name of the default guard
11
+ * Name of the default guard configured in the auth configuration
12
+ *
13
+ * @example
14
+ * const client = new AuthenticatorClient({ default: 'web', guards: {} })
15
+ * console.log(client.defaultGuard) // 'web'
12
16
  */
13
17
  get defaultGuard(): keyof KnownGuards;
14
18
  /**
@@ -1,2 +1,9 @@
1
+ /**
2
+ * Debug logger instance for the AdonisJS auth package.
3
+ * Set NODE_DEBUG=adonisjs:auth environment variable to enable debug logging.
4
+ *
5
+ * @example
6
+ * debug('authenticating user %s', user.email)
7
+ */
1
8
  declare const _default: import("node:util").DebugLogger;
2
9
  export default _default;
@@ -68,7 +68,13 @@ export declare const E_UNAUTHORIZED_ACCESS: {
68
68
  stack?: string;
69
69
  cause?: unknown;
70
70
  };
71
+ /**
72
+ * HTTP status code for unauthorized access
73
+ */
71
74
  status: number;
75
+ /**
76
+ * Error code identifier
77
+ */
72
78
  code: string;
73
79
  help?: string;
74
80
  message?: string;
@@ -126,7 +132,13 @@ export declare const E_INVALID_CREDENTIALS: {
126
132
  stack?: string;
127
133
  cause?: unknown;
128
134
  };
135
+ /**
136
+ * HTTP status code for invalid credentials
137
+ */
129
138
  status: number;
139
+ /**
140
+ * Error code identifier
141
+ */
130
142
  code: string;
131
143
  help?: string;
132
144
  message?: string;
@@ -3,6 +3,7 @@ import { type BaseModel } from '@adonisjs/lucid/orm';
3
3
  import type { NormalizeConstructor } from '@adonisjs/core/types/helpers';
4
4
  type UserWithUserFinderRow = {
5
5
  verifyPassword(plainPassword: string): Promise<boolean>;
6
+ validatePassword(plainPassword: string, passwordFieldName?: string): Promise<void>;
6
7
  };
7
8
  type UserWithUserFinderClass<Model extends NormalizeConstructor<typeof BaseModel> = NormalizeConstructor<typeof BaseModel>> = Model & {
8
9
  hashPassword<T extends UserWithUserFinderClass>(this: T, user: InstanceType<T>): Promise<void>;
@@ -39,6 +39,19 @@ function withAuthFinder(hash, options) {
39
39
  if (!passwordHash) throw new RuntimeException(`Cannot verify password. The value for "${normalizedOptions.passwordColumnName}" column is undefined or null`);
40
40
  return hashFactory().verify(passwordHash, plainPassword);
41
41
  }
42
+ async validatePassword(plainPassword, passwordFieldName) {
43
+ if (!await this.verifyPassword(plainPassword)) {
44
+ const error = /* @__PURE__ */ new Error("Validation Error");
45
+ Object.defineProperty(error, "code", { value: "E_VALIDATION_ERROR" });
46
+ Object.defineProperty(error, "status", { value: 422 });
47
+ Object.defineProperty(error, "messages", { value: [{
48
+ field: passwordFieldName ?? "currentPassword",
49
+ message: "The current password is incorrect",
50
+ rule: "current_password"
51
+ }] });
52
+ throw error;
53
+ }
54
+ }
42
55
  }
43
56
  __decorate([beforeSave()], UserWithUserFinder, "hashPassword", null);
44
57
  return UserWithUserFinder;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/auth",
3
3
  "description": "Official authentication provider for Adonis framework",
4
- "version": "10.0.0-next.4",
4
+ "version": "10.0.0-next.5",
5
5
  "engines": {
6
6
  "node": ">=24.0.0"
7
7
  },
@@ -67,11 +67,11 @@
67
67
  "@japa/plugin-adonisjs": "^5.1.0-next.0",
68
68
  "@japa/runner": "^5.3.0",
69
69
  "@japa/snapshot": "^2.0.10",
70
- "@poppinss/ts-exec": "^1.4.1",
70
+ "@poppinss/ts-exec": "^1.4.2",
71
71
  "@release-it/conventional-changelog": "^10.0.4",
72
72
  "@types/basic-auth": "^1.1.8",
73
73
  "@types/luxon": "^3.7.1",
74
- "@types/node": "^25.0.9",
74
+ "@types/node": "^25.0.10",
75
75
  "@types/set-cookie-parser": "^2.4.10",
76
76
  "@types/sinon": "^21.0.0",
77
77
  "better-sqlite3": "^12.6.2",
@@ -87,7 +87,7 @@
87
87
  "nock": "^14.0.10",
88
88
  "pg": "^8.17.2",
89
89
  "playwright": "^1.57.0",
90
- "prettier": "^3.8.0",
90
+ "prettier": "^3.8.1",
91
91
  "release-it": "^19.2.4",
92
92
  "set-cookie-parser": "^3.0.1",
93
93
  "sinon": "^21.0.1",