question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to custom session field and user field? (CredentialsProvider) [ts language]

See original GitHub issue

Describe the feature

Hi, i’m read doc & try set custom session field and user field

i want set some field in user

{
  accessToken: 'dddddddLZ_pCi1bLqdac3sdfdsfdsfdsfdsf6Jxlw0VyVCzHDopjXuYAviolnA',
  accessTokenExpiresIn: 604800,
  refreshToken: '7c43400b-xxxx-xxxx-xxxx-99b0f91bd570',
  user: {
    id: '7c43400b-xxxx-xxxx-xxxx-99b0f91bd570',
    createdAt: '2021-05-19T17:24:50.579279+07:00',
    displayName: 'test',
    avatarUrl: 'https://s.gravatar.com/avatar/ac44930a62044d038e16f027629b9206?r=g&default=blank',
    locale: 'en',
    email: 'example@gmail.com',
    isAnonymous: false,
    defaultRole: 'admin',
    metadata: { permissions: ['xx'] },
    emailVerified: true,
    phoneNumber: null,
    phoneNumberVerified: false,
    activeMfaType: null,
    roles: [ 'me', 'user' ]
  }
}

Additional information

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
BracketJohncommented, Dec 19, 2022

Oh damn - yeah, that’s unfortunate. We’ll look into this and improve it! I’ve opened #112 to work on this.

And I will close this issue now, as the original author does not respond anymore. Let’s continue discussion on the typing in #112

1reaction
JoaoPedroAS51commented, Nov 29, 2022

Oh I see, my bad. I read ā€œtsā€ and misunderstood the question šŸ˜… So, to extend the session with accessToken for example, you must add jwt and session callbacks. And to define the user object, add profile callback to you provider. Something like this:

// file: ~/server/api/auth/[...].ts
export default NuxtAuthHandler({
  ...
  providers: [
    CredentialsProvider({
      ...
      // Here you define user object
      profile(profile, tokens) {
        return {
           ...
        }
      },
    })
  ],
  callbacks: {
   // Props defined here can be accessed in session callback and will be returned by getToken
    jwt: ({ token, account, user }) => {
      if (account && user) {
        token.accessToken = account.access_token
        // The `user` is the data defined in `profile` callback
        token.user = user
      }

      return token
    },
    
    // Session retuned by useSession and getSession
    session: ({ token, session }) => {
      if (token) {
        session.user = token.user
        session.accessToken = token.accessToken
      }

      return session
    },
  },
})
// file: ~/types/next-auth.d.ts
declare module 'next-auth' {
  /*
   * Returned by useSession and getSession
   */
  interface Session {
    accessToken: string
    user: {
      ...
    }
  }

  /*
   * Usually contains information about the provider being used
   * and also extends TokenSet, which is different tokens returned by OAuth Providers.
   */
  interface Account {
    ...
    access_token: string
  }
}

declare module 'next-auth/jwt' {
  /** Returned by the jwt callback and getToken, when using JWT sessions */
  interface JWT {
    ...
    accessToken: string
    user: {
      ...
    }
  }
}

Note: This is not a complete example. To implement refresh token you shoud read https://next-auth.js.org/tutorials/refresh-token-rotation, and to improve type interfaces follow the link of my comment above.

I’m new to next-auth and this is what I tested with KeycloakProvider, but I believe it will work for CredentialsProvider too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Custom Session Fields - Cvent Community
From the left-hand navigation, under General, click Custom Fields. Select Custom Session Fields from the dropdown. 2 Add the custom session field. Click...
Read more >
Issues Ā· sidebase/nuxt-auth - GitHub
how to custom session field and user field ? (CredentialsProvider) [ts language] question Further information is requested.
Read more >
How to update custom fields for completed training in user's ...
We encountered a problem regarding updating a custom field for a completed session at users' transcripts level.
Read more >
custom session next js next-auth - Stack Overflow
... field to session.user. I created /types/next-auth.d.ts import NextAuth from "next-auth"; declare module "next-auth" { interface Session ...
Read more >
Creating user custom fields - Security Center 5.10
Click the Custom fields page, and click Add an item . From the Entity type drop-down list in the Add custom field dialog...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found