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

Include user.id on session #176

Closed
t3dotgg opened this issue Jul 11, 2022 · 8 comments · Fixed by #180
Closed

Include user.id on session #176

t3dotgg opened this issue Jul 11, 2022 · 8 comments · Fixed by #180
Assignees
Labels
🔰 good first issue Good for newcomers 🌟 enhancement New feature or request

Comments

@t3dotgg
Copy link
Member

t3dotgg commented Jul 11, 2022

I feel like I see this particular issue come up in the discord at least a few times a week, and it's always bothered me.

Should be as simple as:

// in [...nextAuth].ts
callbacks: {
  async session({ session, user }) {
    session.user.id = user.id;
    return Promise.resolve(session);
  },
}

// in a random next-auth.d.ts
declare module "next-auth" {
  /**
   * Returned by `useSession`, `getSession` and received as a prop on the `Provider` React Context
   */
  interface Session {
    userId: string;
  }
}

Ideally NextAuthJS would include this 😅 hoping us including it can work as a forcing function for them to get on it

@t3dotgg
Copy link
Member Author

t3dotgg commented Jul 11, 2022

Cut an issue on NextAuthJS because this should be fixed there IMO

nextauthjs/next-auth#4903

@t3dotgg
Copy link
Member Author

t3dotgg commented Jul 12, 2022

Unhappy w/ response on NextAuth side, let's do this here instead

@t3dotgg t3dotgg added 🌟 enhancement New feature or request 🔰 good first issue Good for newcomers labels Jul 12, 2022
@bhatvikrant
Copy link
Contributor

bhatvikrant commented Jul 12, 2022

Hey I can pick this up @theobr
Raising a PR asap

@nexxeln nexxeln added the 🙋‍♂️ help wanted Extra attention is needed label Jul 12, 2022
@nexxeln
Copy link
Member

nexxeln commented Jul 12, 2022

Hey I can pick this up @theobr

Feel free to make a PR!

@nexxeln
Copy link
Member

nexxeln commented Jul 12, 2022

@bhatvikrant No need to make a separate issue

@bhatvikrant
Copy link
Contributor

Yeah, that happened by mistake LoL @nexxeln

@bhatvikrant
Copy link
Contributor

I think the next-auth.d.ts file should look like the following:

import { DefaultSession } from "next-auth";

declare module "next-auth" {
  /**
   * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
   */
  interface Session {
    user: {
      id?: string | null; // NOTE: made "id" optional (?) and nullable (null) so that it matches types with ones already existing in DefaultSession
    } & DefaultSession["user"];
  }
}

This way it extends the DefaultSession types and the type of id is string | null | undefined just like the already existing name, email and image.
Screenshot 2022-07-12 at 2 41 04 PM
Screenshot 2022-07-12 at 2 41 20 PM

@theobr @nexxeln

@t3dotgg
Copy link
Member Author

t3dotgg commented Jul 12, 2022

@bhatvikrant looks correct to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔰 good first issue Good for newcomers 🌟 enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants