Skip to content

How to get userId on api Endpoints while using next-auth #536

Answered by ruohola
youngnishant asked this question in Help
Discussion options

You must be logged in to vote

I'm using next-auth 4.0.5. Here's how I solved it (mostly combining advice from this thread):

[EDIT] See my updated, simplified solution: #536 (reply in thread)

src/pages/api/auth/[...nextAuth].ts:

export default NextAuth({
  ...
  callbacks: {
    session: async ({ session, token }) => {
      if (session?.user) {
        session.user.id = token.uid;
      }
      return session;
    },
    jwt: async ({ user, token }) => {
      if (user) {
        token.uid = user.id;
      }
      return token;
    },
  },
  session: {
    strategy: 'jwt',
  },
  ...
});

types.d.ts (file in project root):

import type { DefaultUser } from 'next-auth';

declare module 'next-auth' {
  interface Session …

Replies: 27 comments 35 replies

Comment options

You must be logged in to vote
1 reply
@ZendeAditya
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@halp1
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@asibilia
Comment options

@abdullahtariq1171
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@noynek
Comment options

@ruohola
Comment options

@goleary
Comment options

@sayinmehmet47
Comment options

@ruohola
Comment options

Comment options

You must be logged in to vote
9 replies
@KirschX
Comment options

@reevebarreto
Comment options

@dawkaka
Comment options

@mengxi-ream
Comment options

@mengxi-ream
Comment options

Answer selected by balazsorban44
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@shadoath
Comment options

Comment options

You must be logged in to vote
3 replies
@youngnishant
Comment options

@youngnishant
Comment options

@youngnishant
Comment options

Comment options

You must be logged in to vote
5 replies
@youngnishant
Comment options

@beaumontyun
Comment options

@mcgrealife
Comment options

@mcgrealife
Comment options

@felipeotarola
Comment options

Comment options

You must be logged in to vote
1 reply
@arvl130
Comment options

Comment options

You must be logged in to vote
5 replies
@beaumontyun
Comment options

@cobbvanth
Comment options

@cobbvanth
Comment options

@AndrewRayCode
Comment options

@eyadaldaoud
Comment options

Comment options

You must be logged in to vote
1 reply
@simonasbuj
Comment options

Comment options

You must be logged in to vote
1 reply
@mengxi-ream
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
Converted from issue

This discussion was converted from issue #536 on December 07, 2020 17:22.