Skip to content

Commit

Permalink
refactor: small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 26, 2023
1 parent ab0735d commit f5809fd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/auth/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { HttpContext } from '@adonisjs/core/http'
* made to authenticate an HTTP request
*/
export class AuthenticationException extends Exception {
static status?: number | undefined = 401
static code?: string | undefined = 'E_UNAUTHORIZED_ACCESS'

/**
* Raises authentication exception when session guard
* is unable to authenticate the request
Expand Down Expand Up @@ -126,6 +129,7 @@ export class AuthenticationException extends Exception {
export class InvalidCredentialsException extends Exception {
static message: string = 'Invalid credentials'
static code: string = 'E_INVALID_CREDENTIALS'
static status?: number | undefined = 400

static E_INVALID_CREDENTIALS(guardDriverName: string) {
return new InvalidCredentialsException(InvalidCredentialsException.message, {
Expand Down
18 changes: 15 additions & 3 deletions src/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,24 @@ export type InferAuthenticators<
}>,
> = Awaited<ReturnType<Config['resolver']>>['guards']

/**
* Helper to convert union to intersection
*/
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
? I
: never

/**
* Infer events based upon the configure authenticators
*/
export type InferAuthEvents<KnownAuthenticators extends Record<string, GuardFactory>> = {
[K in keyof KnownAuthenticators]: ReturnType<KnownAuthenticators[K]>[typeof GUARD_KNOWN_EVENTS]
}[keyof KnownAuthenticators]
export type InferAuthEvents<KnownAuthenticators extends Record<string, GuardFactory>> =
UnionToIntersection<
{
[K in keyof KnownAuthenticators]: ReturnType<
KnownAuthenticators[K]
>[typeof GUARD_KNOWN_EVENTS]
}[keyof KnownAuthenticators]
>

/**
* Auth service is a singleton instance of the AuthManager
Expand Down
2 changes: 1 addition & 1 deletion src/core/token_providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type DatabaseTokenRow = {
created_at: Date
updated_at: Date
expires_at: Date | null
}
} & Record<string, any>

/**
* A generic implementation to read tokens from the database
Expand Down
2 changes: 1 addition & 1 deletion src/guards/session/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import { Exception } from '@poppinss/utils'
import type { Exception } from '@poppinss/utils'

import type { RememberMeToken } from './token.js'
import type {
Expand Down

0 comments on commit f5809fd

Please sign in to comment.