Skip to content

Commit

Permalink
refacto: moves types to a specific file
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeMRF committed Oct 16, 2024
1 parent 8dd10ed commit d07cd7d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { InferAuthEvents, Authenticators } from '@adonisjs/auth/types'
import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session'
import { jwtGuard } from '@maximemrf/adonisjs-jwt/jwt_config'
import { JwtGuardUser } from '@maximemrf/adonisjs-jwt/types'
import User from '#models/user'

const authConfig = defineConfig({
// define the default authenticator to jwt
Expand Down
47 changes: 1 addition & 46 deletions src/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,7 @@ import { symbols, errors } from '@adonisjs/auth'
import { AuthClientResponse, GuardContract } from '@adonisjs/auth/types'
import type { HttpContext } from '@adonisjs/core/http'
import jwt from 'jsonwebtoken'

/**
* The bridge between the User provider and the
* Guard
*/
export type JwtGuardUser<RealUser> = {
/**
* Returns the unique ID of the user
*/
getId(): string | number | BigInt

/**
* Returns the original user object
*/
getOriginal(): RealUser
}

/**
* The interface for the UserProvider accepted by the
* JWT guard.
*/
export interface JwtUserProviderContract<RealUser> {
/**
* A property the guard implementation can use to infer
* the data type of the actual user (aka RealUser)
*/
[symbols.PROVIDER_REAL_USER]: RealUser

/**
* Create a user object that acts as an adapter between
* the guard and real user value.
*/
createUserForGuard(user: RealUser): Promise<JwtGuardUser<RealUser>>

/**
* Find a user by their id.
*/
findById(identifier: string | number | BigInt): Promise<JwtGuardUser<RealUser> | null>
}

export type JwtGuardOptions<RealUser extends any = unknown> = {
secret: string
expiresIn?: number | string
useCookies?: boolean
content?: (user: JwtGuardUser<RealUser>) => Record<string, any>
}
import { JwtUserProviderContract, JwtGuardOptions } from './types.js'

export class JwtGuard<UserProvider extends JwtUserProviderContract<unknown>>
implements GuardContract<UserProvider[typeof symbols.PROVIDER_REAL_USER]>
Expand Down
47 changes: 47 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { symbols } from '@adonisjs/auth'

/**
* The bridge between the User provider and the
* Guard
*/
export type JwtGuardUser<RealUser> = {
/**
* Returns the unique ID of the user
*/
getId(): string | number | BigInt

/**
* Returns the original user object
*/
getOriginal(): RealUser
}

/**
* The interface for the UserProvider accepted by the
* JWT guard.
*/
export interface JwtUserProviderContract<RealUser> {
/**
* A property the guard implementation can use to infer
* the data type of the actual user (aka RealUser)
*/
[symbols.PROVIDER_REAL_USER]: RealUser

/**
* Create a user object that acts as an adapter between
* the guard and real user value.
*/
createUserForGuard(user: RealUser): Promise<JwtGuardUser<RealUser>>

/**
* Find a user by their id.
*/
findById(identifier: string | number | BigInt): Promise<JwtGuardUser<RealUser> | null>
}

export type JwtGuardOptions<RealUser extends any = unknown> = {
secret: string
expiresIn?: number | string
useCookies?: boolean
content?: (user: JwtGuardUser<RealUser>) => Record<string, any>
}

0 comments on commit d07cd7d

Please sign in to comment.