Skip to content

Commit

Permalink
fix(jwt content): change id by userId
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeMRF committed Oct 20, 2024
1 parent e804edb commit 5ef74db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ import { defineConfig } from '@adonisjs/auth'
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 { JwtGuardUser, BaseJwtContent } from '@maximemrf/adonisjs-jwt/types'
import User from '#models/user'

interface JwtContent extends BaseJwtContent {
email: string
}

const authConfig = defineConfig({
// define the default authenticator to jwt
default: 'jwt',
Expand All @@ -49,8 +53,8 @@ const authConfig = defineConfig({
model: () => import('#models/user'),
}),
// content is a function that takes the user and returns the content of the token, it can be optional, by default it returns only the user id
content: (user: JwtGuardUser<User>) => ({
id: user.getId(),
content: (user: JwtGuardUser<User>): JwtContent => ({
userId: user.getId(),
email: user.getOriginal().email,
}),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function jwtGuard<UserProvider extends JwtUserProviderContract<unknown>>(
provider: UserProvider
tokenExpiresIn?: number | string
useCookies?: boolean
content: <T>(user: JwtGuardUser<T>) => Record<string, any>
content: <T>(user: JwtGuardUser<T>) => Record<string | number, any>
}): GuardConfigProvider<(ctx: HttpContext) => JwtGuard<UserProvider>> {
return {
async resolver(_, app) {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export interface JwtUserProviderContract<RealUser> {
findById(identifier: string | number | BigInt): Promise<JwtGuardUser<RealUser> | null>
}

export type BaseJwtContent = {
userId: string | number | BigInt
}

export type JwtGuardOptions<RealUser extends any = unknown> = {
secret: string
expiresIn?: number | string
Expand Down
13 changes: 9 additions & 4 deletions tests/guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from '@japa/runner'
import { JwtGuard } from '../src/jwt.js'
import { JwtGuardUser } from '../src/types.js'
import { BaseJwtContent, JwtGuardUser } from '../src/types.js'
import { HttpContextFactory } from '@adonisjs/core/factories/http'
import { errors } from '@adonisjs/auth'
import { JwtAuthFakeUser, JwtFakeUserProvider } from '../factories/main.js'
Expand Down Expand Up @@ -43,8 +43,13 @@ test.group('Jwt guard | authenticate', () => {
test('it should return the content function provided when generating jwt', async ({ assert }) => {
const ctx = new HttpContextFactory().create()
const userProvider = new JwtFakeUserProvider()
const jwtContentFn = (user: JwtGuardUser<JwtAuthFakeUser>) => ({
id: user.getId(),

interface CustomJwtContent extends BaseJwtContent {
otherProperty: string
}

const jwtContentFn = (user: JwtGuardUser<JwtAuthFakeUser>): CustomJwtContent => ({
userId: user.getId(),
otherProperty: 'random',
})
const guard = new JwtGuard(ctx, userProvider, {
Expand All @@ -66,7 +71,7 @@ test.group('Jwt guard | authenticate', () => {
assert.exists(tokenResponse.token)
assert.equal(tokenResponse.expiresIn, '1h')

assert.equal(decoded.id, content.id)
assert.equal(decoded.userId, content.userId)
assert.equal(decoded.otherProperty, content.otherProperty)
})

Expand Down

0 comments on commit 5ef74db

Please sign in to comment.