diff --git a/README.md b/README.md index 67daec2..1144cb4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/jwt.ts b/src/jwt.ts index 7e814e6..e53b2f1 100644 --- a/src/jwt.ts +++ b/src/jwt.ts @@ -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 = { - /** - * 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 { - /** - * 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> - - /** - * Find a user by their id. - */ - findById(identifier: string | number | BigInt): Promise | null> -} - -export type JwtGuardOptions = { - secret: string - expiresIn?: number | string - useCookies?: boolean - content?: (user: JwtGuardUser) => Record -} +import { JwtUserProviderContract, JwtGuardOptions } from './types.js' export class JwtGuard> implements GuardContract diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..e5aa418 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,47 @@ +import { symbols } from '@adonisjs/auth' + +/** + * The bridge between the User provider and the + * Guard + */ +export type JwtGuardUser = { + /** + * 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 { + /** + * 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> + + /** + * Find a user by their id. + */ + findById(identifier: string | number | BigInt): Promise | null> +} + +export type JwtGuardOptions = { + secret: string + expiresIn?: number | string + useCookies?: boolean + content?: (user: JwtGuardUser) => Record +}