Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error not solved #3

Open
BakauDesign opened this issue Jan 24, 2025 · 4 comments
Open

Error not solved #3

BakauDesign opened this issue Jan 24, 2025 · 4 comments

Comments

@BakauDesign
Copy link

I got one error from content:
Type '(user: JwtGuardUser) => JwtContent' is not assignable to type '(user: JwtGuardUser) => Record<string | number, any>'.
Types of parameters 'user' and 'user' are incompatible.
Type 'JwtGuardUser' is not assignable to type 'JwtGuardUser'.
Type 'T' is not assignable to type 'User'.ts(2322)
define_config.d.ts(9, 15): This type parameter might need an extends User constraint.
define_config.d.ts(9, 5): The expected type comes from property 'content' which is declared here on type '{ provider: SessionLucidUserProvider; tokenExpiresIn?: string | number | undefined; useCookies?: boolean | undefined; content: (user: JwtGuardUser) => Record<...>; }'
(property) content: (user: JwtGuardUser) => Record<string | number, any>

Code:
import { defineConfig } from '@adonisjs/auth'
import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session'
import { jwtGuard } from '@maximemrf/adonisjs-jwt/jwt_config'
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',
guards: {
web: sessionGuard({
useRememberMeTokens: false,
provider: sessionUserProvider({
model: () => import('#models/user'),
}),
}),
// add the jwt guard
jwt: jwtGuard({
// tokenExpiresIn can be a string or a number, it can be optional
tokenExpiresIn: '1h',
// if you want to use cookies for the authentication instead of the bearer token (optional)
useCookies: true,
provider: sessionUserProvider({
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): JwtContent => ({
userId: user.getId(),
email: user.getOriginal().email,
}),
}),
},
})

export { authConfig };

THanks dude

@MaximeMRF
Copy link
Owner

Hello @BakauDesign,

I will take a look this weekend, thanks for the issue

@BakauDesign
Copy link
Author

Image

@BakauDesign
Copy link
Author

@MaximeMRF
Copy link
Owner

MaximeMRF commented Jan 26, 2025

Hey @BakauDesign,

You can fix the error with this config:

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, 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',
  guards: {
    web: sessionGuard({
      useRememberMeTokens: false,
      provider: sessionUserProvider({
        model: () => import('#models/user'),
      }),
    }),
    // add the jwt guard
    jwt: jwtGuard({
      // tokenExpiresIn can be a string or a number, it can be optional
      tokenExpiresIn: '1h',
      // if you want to use cookies for the authentication instead of the bearer token (optional)
      useCookies: true,
      provider: sessionUserProvider({
        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: <T>(user: JwtGuardUser<T>): JwtContent => {
        return {
          userId: user.getId(),
          email: (user.getOriginal() as User).email,
        }
      },
    }),
  },
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants