@adonisjs/auth 9.5.0 → 10.0.0-next.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.
Files changed (43) hide show
  1. package/build/{chunk-SAW2RPCA.js → chunk-MSPAYMZE.js} +94 -4
  2. package/build/{chunk-MUPAP5IP.js → chunk-S5G5RTJX.js} +40 -0
  3. package/build/index.d.ts +8 -8
  4. package/build/index.js +2 -2
  5. package/build/modules/access_tokens_guard/access_token.d.ts +103 -0
  6. package/build/modules/access_tokens_guard/crc32.d.ts +39 -0
  7. package/build/modules/access_tokens_guard/define_config.d.ts +4 -4
  8. package/build/modules/access_tokens_guard/guard.d.ts +82 -5
  9. package/build/modules/access_tokens_guard/main.d.ts +5 -5
  10. package/build/modules/access_tokens_guard/main.js +339 -4
  11. package/build/modules/access_tokens_guard/token_providers/db.d.ts +102 -4
  12. package/build/modules/access_tokens_guard/types.d.ts +2 -2
  13. package/build/modules/access_tokens_guard/user_providers/lucid.d.ts +77 -4
  14. package/build/modules/basic_auth_guard/define_config.d.ts +4 -4
  15. package/build/modules/basic_auth_guard/guard.d.ts +16 -3
  16. package/build/modules/basic_auth_guard/main.d.ts +3 -3
  17. package/build/modules/basic_auth_guard/main.js +11 -1
  18. package/build/modules/basic_auth_guard/types.d.ts +1 -1
  19. package/build/modules/basic_auth_guard/user_providers/lucid.d.ts +19 -2
  20. package/build/modules/session_guard/define_config.d.ts +4 -4
  21. package/build/modules/session_guard/guard.d.ts +42 -3
  22. package/build/modules/session_guard/main.d.ts +5 -5
  23. package/build/modules/session_guard/main.js +349 -124
  24. package/build/modules/session_guard/remember_me_token.d.ts +64 -0
  25. package/build/modules/session_guard/token_providers/db.d.ts +92 -2
  26. package/build/modules/session_guard/types.d.ts +2 -2
  27. package/build/modules/session_guard/user_providers/lucid.d.ts +78 -5
  28. package/build/providers/auth_provider.d.ts +22 -1
  29. package/build/providers/auth_provider.js +16 -3
  30. package/build/services/auth.d.ts +1 -1
  31. package/build/src/auth_manager.d.ts +25 -4
  32. package/build/src/authenticator.d.ts +55 -3
  33. package/build/src/authenticator_client.d.ts +18 -1
  34. package/build/src/define_config.d.ts +19 -1
  35. package/build/src/errors.d.ts +31 -0
  36. package/build/src/middleware/initialize_auth_middleware.d.ts +15 -2
  37. package/build/src/middleware/initialize_auth_middleware.js +10 -0
  38. package/build/src/mixins/lucid.d.ts +13 -0
  39. package/build/src/mixins/lucid.js +27 -2
  40. package/build/src/plugins/japa/api_client.d.ts +10 -1
  41. package/build/src/plugins/japa/browser_client.d.ts +10 -1
  42. package/build/src/types.d.ts +141 -29
  43. package/package.json +23 -22
@@ -3,6 +3,16 @@ import { Secret } from '@adonisjs/core/helpers';
3
3
  * Remember me token represents an opaque token that can be
4
4
  * used to automatically login a user without asking them
5
5
  * to re-login
6
+ *
7
+ * @example
8
+ * const token = new RememberMeToken({
9
+ * identifier: 1,
10
+ * tokenableId: 123,
11
+ * hash: 'sha256hash',
12
+ * createdAt: new Date(),
13
+ * updatedAt: new Date(),
14
+ * expiresAt: new Date(Date.now() + 86400000)
15
+ * })
6
16
  */
7
17
  export declare class RememberMeToken {
8
18
  /**
@@ -11,6 +21,15 @@ export declare class RememberMeToken {
11
21
  *
12
22
  * Returns null when unable to decode the token because of
13
23
  * invalid format or encoding.
24
+ *
25
+ * @param value - The token value to decode
26
+ *
27
+ * @example
28
+ * const decoded = RememberMeToken.decode('abc123.def456')
29
+ * if (decoded) {
30
+ * console.log('Token ID:', decoded.identifier)
31
+ * console.log('Secret:', decoded.secret.release())
32
+ * }
14
33
  */
15
34
  static decode(value: string): null | {
16
35
  identifier: string;
@@ -19,6 +38,14 @@ export declare class RememberMeToken {
19
38
  /**
20
39
  * Creates a transient token that can be shared with the persistence
21
40
  * layer.
41
+ *
42
+ * @param userId - The ID of the user for whom the token is created
43
+ * @param size - The size of the random secret to generate
44
+ * @param expiresIn - Expiration time (seconds or duration string)
45
+ *
46
+ * @example
47
+ * const transientToken = RememberMeToken.createTransientToken(123, 32, '30d')
48
+ * // Store transientToken in database
22
49
  */
23
50
  static createTransientToken(userId: string | number | BigInt, size: number, expiresIn: string | number): {
24
51
  secret: Secret<string>;
@@ -28,6 +55,13 @@ export declare class RememberMeToken {
28
55
  };
29
56
  /**
30
57
  * Creates a secret opaque token and its hash.
58
+ *
59
+ * @param size - The size of the random string to generate
60
+ *
61
+ * @example
62
+ * const { secret, hash } = RememberMeToken.seed(32)
63
+ * console.log('Secret:', secret.release())
64
+ * console.log('Hash:', hash)
31
65
  */
32
66
  static seed(size: number): {
33
67
  secret: Secret<string>;
@@ -66,6 +100,21 @@ export declare class RememberMeToken {
66
100
  * Timestamp at which the token will expire
67
101
  */
68
102
  expiresAt: Date;
103
+ /**
104
+ * Creates a new RememberMeToken instance
105
+ *
106
+ * @param attributes - Token attributes including identifier, user ID, hash, etc.
107
+ *
108
+ * @example
109
+ * const token = new RememberMeToken({
110
+ * identifier: 1,
111
+ * tokenableId: 123,
112
+ * hash: 'sha256hash',
113
+ * createdAt: new Date(),
114
+ * updatedAt: new Date(),
115
+ * expiresAt: new Date(Date.now() + 86400000)
116
+ * })
117
+ */
69
118
  constructor(attributes: {
70
119
  identifier: string | number | BigInt;
71
120
  tokenableId: string | number | BigInt;
@@ -79,10 +128,25 @@ export declare class RememberMeToken {
79
128
  * Check if the token has been expired. Verifies
80
129
  * the "expiresAt" timestamp with the current
81
130
  * date.
131
+ *
132
+ * @example
133
+ * if (token.isExpired()) {
134
+ * console.log('Remember me token has expired')
135
+ * } else {
136
+ * console.log('Token is still valid')
137
+ * }
82
138
  */
83
139
  isExpired(): boolean;
84
140
  /**
85
141
  * Verifies the value of a token against the pre-defined hash
142
+ *
143
+ * @param secret - The secret to verify against the stored hash
144
+ *
145
+ * @example
146
+ * const isValid = token.verify(new Secret('user-provided-secret'))
147
+ * if (isValid) {
148
+ * console.log('Remember me token is valid')
149
+ * }
86
150
  */
87
151
  verify(secret: Secret<string>): boolean;
88
152
  }
@@ -1,18 +1,36 @@
1
1
  import type { Secret } from '@adonisjs/core/helpers';
2
2
  import type { LucidModel } from '@adonisjs/lucid/types/model';
3
- import { RememberMeToken } from '../remember_me_token.js';
4
- import type { RememberMeTokenDbColumns, RememberMeTokensProviderContract, DbRememberMeTokensProviderOptions } from '../types.js';
3
+ import { RememberMeToken } from '../remember_me_token.ts';
4
+ import type { RememberMeTokenDbColumns, RememberMeTokensProviderContract, DbRememberMeTokensProviderOptions } from '../types.ts';
5
5
  /**
6
6
  * DbRememberMeTokensProvider uses lucid database service to fetch and
7
7
  * persist tokens for a given user.
8
8
  *
9
9
  * The user must be an instance of the associated user model.
10
+ *
11
+ * @template TokenableModel - The Lucid model that can have remember me tokens
12
+ *
13
+ * @example
14
+ * const provider = new DbRememberMeTokensProvider({
15
+ * tokenableModel: () => import('#models/user'),
16
+ * table: 'remember_me_tokens',
17
+ * tokenSecretLength: 32
18
+ * })
10
19
  */
11
20
  export declare class DbRememberMeTokensProvider<TokenableModel extends LucidModel> implements RememberMeTokensProviderContract<TokenableModel> {
12
21
  #private;
13
22
  protected options: DbRememberMeTokensProviderOptions<TokenableModel>;
14
23
  /**
15
24
  * Create tokens provider instance for a given Lucid model
25
+ *
26
+ * @param model - The tokenable model factory function
27
+ * @param options - Optional configuration options
28
+ *
29
+ * @example
30
+ * const provider = DbRememberMeTokensProvider.forModel(
31
+ * () => import('#models/user'),
32
+ * { table: 'user_remember_tokens' }
33
+ * )
16
34
  */
17
35
  static forModel<TokenableModel extends LucidModel>(model: DbRememberMeTokensProviderOptions<TokenableModel>['tokenableModel'], options?: Omit<DbRememberMeTokensProviderOptions<TokenableModel>, 'tokenableModel'>): DbRememberMeTokensProvider<TokenableModel>;
18
36
  /**
@@ -24,29 +42,85 @@ export declare class DbRememberMeTokensProvider<TokenableModel extends LucidMode
24
42
  * secure random string.
25
43
  */
26
44
  protected tokenSecretLength: number;
45
+ /**
46
+ * Creates a new DbRememberMeTokensProvider instance
47
+ *
48
+ * @param options - Configuration options for the provider
49
+ *
50
+ * @example
51
+ * const provider = new DbRememberMeTokensProvider({
52
+ * tokenableModel: () => import('#models/user'),
53
+ * table: 'remember_me_tokens',
54
+ * tokenSecretLength: 40
55
+ * })
56
+ */
27
57
  constructor(options: DbRememberMeTokensProviderOptions<TokenableModel>);
28
58
  /**
29
59
  * Maps a database row to an instance token instance
60
+ *
61
+ * @param dbRow - The database row containing token data
62
+ *
63
+ * @example
64
+ * const token = provider.dbRowToRememberMeToken({
65
+ * id: 1,
66
+ * tokenable_id: 123,
67
+ * hash: 'sha256hash',
68
+ * // ... other columns
69
+ * })
30
70
  */
31
71
  protected dbRowToRememberMeToken(dbRow: RememberMeTokenDbColumns): RememberMeToken;
32
72
  /**
33
73
  * Returns a query client instance from the parent model
74
+ *
75
+ * @example
76
+ * const db = await provider.getDb()
77
+ * const tokens = await db.from('remember_me_tokens').select('*')
34
78
  */
35
79
  protected getDb(): Promise<import("@adonisjs/lucid/types/database").QueryClientContract>;
36
80
  /**
37
81
  * Create a token for a user
82
+ *
83
+ * @param user - The user instance to create a token for
84
+ * @param expiresIn - Token expiration time
85
+ *
86
+ * @example
87
+ * const token = await provider.create(user, '30d')
88
+ * console.log('Remember token:', token.value.release())
38
89
  */
39
90
  create(user: InstanceType<TokenableModel>, expiresIn: string | number): Promise<RememberMeToken>;
40
91
  /**
41
92
  * Find a token for a user by the token id
93
+ *
94
+ * @param user - The user instance that owns the token
95
+ * @param identifier - The token identifier to search for
96
+ *
97
+ * @example
98
+ * const token = await provider.find(user, 123)
99
+ * if (token) {
100
+ * console.log('Found token with id:', token.identifier)
101
+ * }
42
102
  */
43
103
  find(user: InstanceType<TokenableModel>, identifier: string | number | BigInt): Promise<RememberMeToken | null>;
44
104
  /**
45
105
  * Delete a token by its id
106
+ *
107
+ * @param user - The user instance that owns the token
108
+ * @param identifier - The token identifier to delete
109
+ *
110
+ * @example
111
+ * const deletedCount = await provider.delete(user, 123)
112
+ * console.log('Deleted tokens:', deletedCount)
46
113
  */
47
114
  delete(user: InstanceType<TokenableModel>, identifier: string | number | BigInt): Promise<number>;
48
115
  /**
49
116
  * Returns all the tokens a given user
117
+ *
118
+ * @param user - The user instance to get tokens for
119
+ *
120
+ * @example
121
+ * const tokens = await provider.all(user)
122
+ * console.log('User has', tokens.length, 'remember tokens')
123
+ * tokens.forEach(token => console.log(token.identifier))
50
124
  */
51
125
  all(user: InstanceType<TokenableModel>): Promise<RememberMeToken[]>;
52
126
  /**
@@ -55,6 +129,14 @@ export declare class DbRememberMeTokensProvider<TokenableModel extends LucidMode
55
129
  *
56
130
  * Returns null when unable to verify the token or find it
57
131
  * inside the storage
132
+ *
133
+ * @param tokenValue - The token value to verify
134
+ *
135
+ * @example
136
+ * const token = await provider.verify(new Secret('rmt_abc123.def456'))
137
+ * if (token && !token.isExpired()) {
138
+ * console.log('Valid remember token for user:', token.tokenableId)
139
+ * }
58
140
  */
59
141
  verify(tokenValue: Secret<string>): Promise<RememberMeToken | null>;
60
142
  /**
@@ -64,6 +146,14 @@ export declare class DbRememberMeTokensProvider<TokenableModel extends LucidMode
64
146
  * Ideally, the recycle should update the existing token, but we
65
147
  * skip that for now and come back to it later and handle race
66
148
  * conditions as well.
149
+ *
150
+ * @param user - The user that owns the token
151
+ * @param identifier - The token identifier to recycle
152
+ * @param expiresIn - New expiration time
153
+ *
154
+ * @example
155
+ * const newToken = await provider.recycle(user, 123, '30d')
156
+ * console.log('Recycled token:', newToken.value.release())
67
157
  */
68
158
  recycle(user: InstanceType<TokenableModel>, identifier: string | number | BigInt, expiresIn: string | number): Promise<RememberMeToken>;
69
159
  }
@@ -2,8 +2,8 @@ import type { Secret } from '@adonisjs/core/helpers';
2
2
  import type { HttpContext } from '@adonisjs/core/http';
3
3
  import type { Exception } from '@adonisjs/core/exceptions';
4
4
  import type { LucidModel } from '@adonisjs/lucid/types/model';
5
- import { PROVIDER_REAL_USER } from '../../src/symbols.js';
6
- import type { RememberMeToken } from './remember_me_token.js';
5
+ import { type PROVIDER_REAL_USER } from '../../src/symbols.ts';
6
+ import type { RememberMeToken } from './remember_me_token.ts';
7
7
  /**
8
8
  * Options accepted by the tokens provider that uses lucid
9
9
  * database service to fetch and persist tokens.
@@ -1,10 +1,17 @@
1
- import { Secret } from '@adonisjs/core/helpers';
2
- import { RememberMeToken } from '../remember_me_token.js';
3
- import { PROVIDER_REAL_USER } from '../../../src/symbols.js';
4
- import type { SessionGuardUser, LucidAuthenticatable, SessionLucidUserProviderOptions, SessionUserProviderContract } from '../types.js';
1
+ import { type Secret } from '@adonisjs/core/helpers';
2
+ import { type RememberMeToken } from '../remember_me_token.ts';
3
+ import { PROVIDER_REAL_USER } from '../../../src/symbols.ts';
4
+ import type { SessionGuardUser, LucidAuthenticatable, SessionLucidUserProviderOptions, SessionUserProviderContract } from '../types.ts';
5
5
  /**
6
6
  * Uses a lucid model to verify access tokens and find a user during
7
7
  * authentication
8
+ *
9
+ * @template UserModel - The Lucid model representing the user
10
+ *
11
+ * @example
12
+ * const userProvider = new SessionLucidUserProvider({
13
+ * model: () => import('#models/user')
14
+ * })
8
15
  */
9
16
  export declare class SessionLucidUserProvider<UserModel extends LucidAuthenticatable> implements SessionUserProviderContract<InstanceType<UserModel>> {
10
17
  /**
@@ -16,6 +23,16 @@ export declare class SessionLucidUserProvider<UserModel extends LucidAuthenticat
16
23
  * Reference to the lazily imported model
17
24
  */
18
25
  protected model?: UserModel;
26
+ /**
27
+ * Creates a new SessionLucidUserProvider instance
28
+ *
29
+ * @param options - Configuration options for the user provider
30
+ *
31
+ * @example
32
+ * const provider = new SessionLucidUserProvider({
33
+ * model: () => import('#models/user')
34
+ * })
35
+ */
19
36
  constructor(
20
37
  /**
21
38
  * Lucid provider options
@@ -24,34 +41,90 @@ export declare class SessionLucidUserProvider<UserModel extends LucidAuthenticat
24
41
  /**
25
42
  * Imports the model from the provider, returns and caches it
26
43
  * for further operations.
44
+ *
45
+ * @example
46
+ * const UserModel = await provider.getModel()
47
+ * const user = await UserModel.find(1)
27
48
  */
28
49
  protected getModel(): Promise<UserModel>;
29
50
  /**
30
51
  * Returns the tokens provider associated with the user model
52
+ *
53
+ * @example
54
+ * const tokensProvider = await provider.getTokensProvider()
55
+ * const token = await tokensProvider.create(user, '7d')
31
56
  */
32
- protected getTokensProvider(): Promise<import("../types.js").RememberMeTokensProviderContract<import("@adonisjs/lucid/types/model").LucidModel>>;
57
+ protected getTokensProvider(): Promise<import("../types.ts").RememberMeTokensProviderContract<import("@adonisjs/lucid/types/model").LucidModel>>;
33
58
  /**
34
59
  * Creates an adapter user for the guard
60
+ *
61
+ * @param user - The user model instance
62
+ *
63
+ * @example
64
+ * const guardUser = await provider.createUserForGuard(user)
65
+ * console.log('User ID:', guardUser.getId())
66
+ * console.log('Original user:', guardUser.getOriginal())
35
67
  */
36
68
  createUserForGuard(user: InstanceType<UserModel>): Promise<SessionGuardUser<InstanceType<UserModel>>>;
37
69
  /**
38
70
  * Finds a user by their primary key value
71
+ *
72
+ * @param identifier - The user identifier to search for
73
+ *
74
+ * @example
75
+ * const guardUser = await provider.findById(123)
76
+ * if (guardUser) {
77
+ * const originalUser = guardUser.getOriginal()
78
+ * console.log('Found user:', originalUser.email)
79
+ * }
39
80
  */
40
81
  findById(identifier: string | number | BigInt): Promise<SessionGuardUser<InstanceType<UserModel>> | null>;
41
82
  /**
42
83
  * Creates a remember token for a given user
84
+ *
85
+ * @param user - The user to create a token for
86
+ * @param expiresIn - Token expiration time
87
+ *
88
+ * @example
89
+ * const token = await provider.createRememberToken(user, '30d')
90
+ * console.log('Remember token:', token.value.release())
43
91
  */
44
92
  createRememberToken(user: InstanceType<UserModel>, expiresIn: string | number): Promise<RememberMeToken>;
45
93
  /**
46
94
  * Verify a token by its publicly shared value
95
+ *
96
+ * @param tokenValue - The token value to verify
97
+ *
98
+ * @example
99
+ * const token = await provider.verifyRememberToken(
100
+ * new Secret('rmt_abc123.def456')
101
+ * )
102
+ * if (token && !token.isExpired()) {
103
+ * console.log('Valid remember token for user:', token.tokenableId)
104
+ * }
47
105
  */
48
106
  verifyRememberToken(tokenValue: Secret<string>): Promise<RememberMeToken | null>;
49
107
  /**
50
108
  * Delete a token for a user by the token identifier
109
+ *
110
+ * @param user - The user that owns the token
111
+ * @param identifier - The token identifier to delete
112
+ *
113
+ * @example
114
+ * const deletedCount = await provider.deleteRemeberToken(user, 123)
115
+ * console.log('Deleted tokens:', deletedCount)
51
116
  */
52
117
  deleteRemeberToken(user: InstanceType<UserModel>, identifier: string | number | BigInt): Promise<number>;
53
118
  /**
54
119
  * Recycle a token for a user by the token identifier
120
+ *
121
+ * @param user - The user that owns the token
122
+ * @param identifier - The token identifier to recycle
123
+ * @param expiresIn - New expiration time
124
+ *
125
+ * @example
126
+ * const newToken = await provider.recycleRememberToken(user, 123, '30d')
127
+ * console.log('Recycled token:', newToken.value.release())
55
128
  */
56
129
  recycleRememberToken(user: InstanceType<UserModel>, identifier: string | number | BigInt, expiresIn: string | number): Promise<RememberMeToken>;
57
130
  }
@@ -1,12 +1,33 @@
1
1
  import type { ApplicationService } from '@adonisjs/core/types';
2
- import type { AuthService } from '../src/types.js';
2
+ import type { AuthService } from '../src/types.ts';
3
3
  declare module '@adonisjs/core/types' {
4
4
  interface ContainerBindings {
5
5
  'auth.manager': AuthService;
6
6
  }
7
7
  }
8
+ /**
9
+ * The AuthProvider service provider registers the auth manager
10
+ * with the IoC container as a singleton
11
+ *
12
+ * @example
13
+ * // The auth manager is automatically registered and can be injected
14
+ * container.use('auth.manager')
15
+ */
8
16
  export default class AuthProvider {
9
17
  protected app: ApplicationService;
18
+ /**
19
+ * Creates a new AuthProvider instance
20
+ *
21
+ * @param app - The application service instance
22
+ */
10
23
  constructor(app: ApplicationService);
24
+ /**
25
+ * Registers the auth manager as a singleton service
26
+ * in the IoC container
27
+ *
28
+ * @example
29
+ * // This method is called automatically by AdonisJS
30
+ * // The auth manager becomes available as 'auth.manager'
31
+ */
11
32
  register(): void;
12
33
  }
@@ -1,17 +1,30 @@
1
1
  import {
2
2
  AuthManager
3
- } from "../chunk-SAW2RPCA.js";
3
+ } from "../chunk-MSPAYMZE.js";
4
4
  import "../chunk-2VRS2VHB.js";
5
- import "../chunk-MUPAP5IP.js";
5
+ import "../chunk-S5G5RTJX.js";
6
6
  import "../chunk-UXA4FHST.js";
7
7
 
8
8
  // providers/auth_provider.ts
9
9
  import { configProvider } from "@adonisjs/core";
10
- import { RuntimeException } from "@poppinss/utils";
10
+ import { RuntimeException } from "@adonisjs/core/exceptions";
11
11
  var AuthProvider = class {
12
+ /**
13
+ * Creates a new AuthProvider instance
14
+ *
15
+ * @param app - The application service instance
16
+ */
12
17
  constructor(app) {
13
18
  this.app = app;
14
19
  }
20
+ /**
21
+ * Registers the auth manager as a singleton service
22
+ * in the IoC container
23
+ *
24
+ * @example
25
+ * // This method is called automatically by AdonisJS
26
+ * // The auth manager becomes available as 'auth.manager'
27
+ */
15
28
  register() {
16
29
  this.app.container.singleton("auth.manager", async () => {
17
30
  const authConfigProvider = this.app.config.get("auth");
@@ -1,3 +1,3 @@
1
- import { AuthService } from '../src/types.js';
1
+ import { type AuthService } from '../src/types.ts';
2
2
  declare let auth: AuthService;
3
3
  export { auth as default };
@@ -1,7 +1,7 @@
1
1
  import type { HttpContext } from '@adonisjs/core/http';
2
- import type { GuardFactory } from './types.js';
3
- import { Authenticator } from './authenticator.js';
4
- import { AuthenticatorClient } from './authenticator_client.js';
2
+ import type { GuardFactory } from './types.ts';
3
+ import { Authenticator } from './authenticator.ts';
4
+ import { AuthenticatorClient } from './authenticator_client.ts';
5
5
  /**
6
6
  * Auth manager exposes the API to register and manage authentication
7
7
  * guards from the config
@@ -15,18 +15,39 @@ export declare class AuthManager<KnownGuards extends Record<string, GuardFactory
15
15
  * Name of the default guard
16
16
  */
17
17
  get defaultGuard(): keyof KnownGuards;
18
+ /**
19
+ * Creates a new AuthManager instance
20
+ *
21
+ * @param config - Configuration object containing default guard and available guards
22
+ *
23
+ * @example
24
+ * const manager = new AuthManager({
25
+ * default: 'web',
26
+ * guards: { web: sessionGuard, api: tokenGuard }
27
+ * })
28
+ */
18
29
  constructor(config: {
19
30
  default: keyof KnownGuards;
20
31
  guards: KnownGuards;
21
32
  });
22
33
  /**
23
34
  * Create an authenticator for a given HTTP request. The authenticator
24
- * is used to authenticated in incoming HTTP request
35
+ * is used to authenticate incoming HTTP requests
36
+ *
37
+ * @param ctx - The HTTP context for the current request
38
+ *
39
+ * @example
40
+ * const authenticator = manager.createAuthenticator(ctx)
41
+ * const user = await authenticator.authenticate()
25
42
  */
26
43
  createAuthenticator(ctx: HttpContext): Authenticator<KnownGuards>;
27
44
  /**
28
45
  * Creates an instance of the authenticator client. The client is
29
46
  * used to setup authentication state during testing.
47
+ *
48
+ * @example
49
+ * const client = manager.createAuthenticatorClient()
50
+ * const guard = client.use('session')
30
51
  */
31
52
  createAuthenticatorClient(): AuthenticatorClient<KnownGuards>;
32
53
  }
@@ -1,5 +1,5 @@
1
1
  import type { HttpContext } from '@adonisjs/core/http';
2
- import type { GuardFactory } from './types.js';
2
+ import type { GuardFactory } from './types.ts';
3
3
  /**
4
4
  * Authenticator is used to authenticate incoming HTTP requests
5
5
  * using one or more known guards.
@@ -36,13 +36,30 @@ export declare class Authenticator<KnownGuards extends Record<string, GuardFacto
36
36
  * used.
37
37
  */
38
38
  get authenticationAttempted(): boolean;
39
+ /**
40
+ * Creates a new Authenticator instance
41
+ *
42
+ * @param ctx - The HTTP context for the current request
43
+ * @param config - Configuration object containing default guard and available guards
44
+ *
45
+ * @example
46
+ * const authenticator = new Authenticator(ctx, {
47
+ * default: 'web',
48
+ * guards: { web: sessionGuard }
49
+ * })
50
+ */
39
51
  constructor(ctx: HttpContext, config: {
40
52
  default: keyof KnownGuards;
41
53
  guards: KnownGuards;
42
54
  });
43
55
  /**
44
- * Returns an instance of the logged-in user or throws an
45
- * exception
56
+ * Returns an instance of the logged-in user or throws an exception
57
+ *
58
+ * @throws {RuntimeException} When authentication has not been attempted
59
+ *
60
+ * @example
61
+ * const user = auth.getUserOrFail()
62
+ * console.log(user.id)
46
63
  */
47
64
  getUserOrFail(): {
48
65
  [K in keyof KnownGuards]: ReturnType<ReturnType<KnownGuards[K]>['getUserOrFail']>;
@@ -50,18 +67,36 @@ export declare class Authenticator<KnownGuards extends Record<string, GuardFacto
50
67
  /**
51
68
  * Returns an instance of a known guard. Guards instances are
52
69
  * cached during the lifecycle of an HTTP request.
70
+ *
71
+ * @param guard - Optional guard name. Uses default guard if not provided
72
+ *
73
+ * @example
74
+ * const sessionGuard = auth.use('session')
75
+ * const defaultGuard = auth.use()
53
76
  */
54
77
  use<Guard extends keyof KnownGuards>(guard?: Guard): ReturnType<KnownGuards[Guard]>;
55
78
  /**
56
79
  * Authenticate current request using the default guard. Calling this
57
80
  * method multiple times triggers multiple authentication with the
58
81
  * guard.
82
+ *
83
+ * @throws {E_UNAUTHORIZED_ACCESS} When authentication fails
84
+ *
85
+ * @example
86
+ * const user = await auth.authenticate()
87
+ * console.log('Authenticated user:', user.email)
59
88
  */
60
89
  authenticate(): Promise<{ [K in keyof KnownGuards]: ReturnType<ReturnType<KnownGuards[K]>["getUserOrFail"]>; }[keyof KnownGuards]>;
61
90
  /**
62
91
  * Silently attempt to authenticate the request using the default
63
92
  * guard. Calling this method multiple times triggers multiple
64
93
  * authentication with the guard.
94
+ *
95
+ * @example
96
+ * const isAuthenticated = await auth.check()
97
+ * if (isAuthenticated) {
98
+ * console.log('User is authenticated')
99
+ * }
65
100
  */
66
101
  check(): Promise<boolean>;
67
102
  /**
@@ -72,6 +107,15 @@ export declare class Authenticator<KnownGuards extends Record<string, GuardFacto
72
107
  * guards is able to authenticate the request successfully.
73
108
  *
74
109
  * Otherwise, "E_UNAUTHORIZED_ACCESS" will be raised.
110
+ *
111
+ * @param guards - Array of guard names to try for authentication
112
+ * @param options - Options object with optional loginRoute for redirects
113
+ *
114
+ * @throws {E_UNAUTHORIZED_ACCESS} When none of the guards can authenticate
115
+ *
116
+ * @example
117
+ * const user = await auth.authenticateUsing(['session', 'api'])
118
+ * const userWithRedirect = await auth.authenticateUsing(['web'], { loginRoute: '/login' })
75
119
  */
76
120
  authenticateUsing(guards?: (keyof KnownGuards)[], options?: {
77
121
  loginRoute?: string;
@@ -82,6 +126,14 @@ export declare class Authenticator<KnownGuards extends Record<string, GuardFacto
82
126
  * Silently attempt to authenticate the request using all of the mentioned guards
83
127
  * or the default guard. Calling this method multiple times triggers multiple
84
128
  * authentication with the guard.
129
+ *
130
+ * @param guards - Array of guard names to check. Defaults to default guard
131
+ *
132
+ * @example
133
+ * const isAuthenticated = await auth.checkUsing(['session', 'api'])
134
+ * if (isAuthenticated) {
135
+ * const user = auth.user
136
+ * }
85
137
  */
86
138
  checkUsing(guards?: (keyof KnownGuards)[]): Promise<boolean>;
87
139
  }
@@ -1,4 +1,4 @@
1
- import type { GuardFactory } from './types.js';
1
+ import type { GuardFactory } from './types.ts';
2
2
  /**
3
3
  * Authenticator client is used to create guard instances for testing.
4
4
  * It passes a fake HTTPContext to the guards, so make sure to not
@@ -11,6 +11,17 @@ export declare class AuthenticatorClient<KnownGuards extends Record<string, Guar
11
11
  * Name of the default guard
12
12
  */
13
13
  get defaultGuard(): keyof KnownGuards;
14
+ /**
15
+ * Creates a new AuthenticatorClient instance for testing
16
+ *
17
+ * @param config - Configuration object containing default guard and available guards
18
+ *
19
+ * @example
20
+ * const client = new AuthenticatorClient({
21
+ * default: 'web',
22
+ * guards: { web: sessionGuard }
23
+ * })
24
+ */
14
25
  constructor(config: {
15
26
  default: keyof KnownGuards;
16
27
  guards: KnownGuards;
@@ -18,6 +29,12 @@ export declare class AuthenticatorClient<KnownGuards extends Record<string, Guar
18
29
  /**
19
30
  * Returns an instance of a known guard. Guards instances are
20
31
  * cached during the lifecycle of an HTTP request.
32
+ *
33
+ * @param guard - Optional guard name. Uses default guard if not provided
34
+ *
35
+ * @example
36
+ * const sessionGuard = client.use('session')
37
+ * const defaultGuard = client.use()
21
38
  */
22
39
  use<Guard extends keyof KnownGuards>(guard?: Guard): ReturnType<KnownGuards[Guard]>;
23
40
  }