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

chore: include user.id on session #180

Merged
merged 14 commits into from
Jul 14, 2022
Merged

chore: include user.id on session #180

merged 14 commits into from
Jul 14, 2022

Conversation

bhatvikrant
Copy link
Contributor

@bhatvikrant bhatvikrant commented Jul 12, 2022

Include user.id on session

  • I reviewed linter warnings + errors, resolved formatting, types and other issues related to my work
  • The PR title follows the convention we established conventional-commit
  • I performed a functional test on my final commit

Summary

fixes #176

  1. added callbacks in api-handler-prisma.ts and api-handler.ts
  2. included next-auth.d.ts only when it is a next-auth scaffolding

@nexxeln nexxeln requested a review from t3dotgg July 12, 2022 11:11
Copy link
Contributor

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added my suggestions.

@nexxeln
Copy link
Member

nexxeln commented Jul 12, 2022

Thank you! @balazsorban44 , great to have a review from you.

@bhatvikrant
Copy link
Contributor Author

Added my suggestions.

Hey @balazsorban44 nice to see you here!
Thanks for the inputs

@bhatvikrant
Copy link
Contributor Author

Have made the changes suggested by @balazsorban44

Copy link
Member

@t3dotgg t3dotgg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, I'm fine with the session binding being "incorrect" if you don't use a DB persistence (can't imagine many if any of our users would NOT use a DB)

@juliusmarminge
Copy link
Member

if we only include this when Prisma is also included, would that make more sense?

I'm fine with the session binding being "incorrect" if you don't use a DB persistence

I think modifying the installer to have two separate api-handlers would be better? Also not include the next-auth.d.ts if no db is included?

@juliusmarminge
Copy link
Member

juliusmarminge commented Jul 12, 2022

if we only include this when Prisma is also included, would that make more sense?

I'm fine with the session binding being "incorrect" if you don't use a DB persistence

I think modifying the installer to have two separate api-handlers would be better? Also not include the next-auth.d.ts if no db is included?

On second thought it doesn't really matter since the user-object's type exists whether there is a db or not. This is from a fresh t3 app with just next-auth:
CleanShot 2022-07-12 at 19 48 41

So having an id on there doesn't really matter I guess...

The above solution would also require two different tsconfigs and now it's really getting annoying...

But making the user (and the id) optional in the next-auth.ts I guess would be better to keep the typesafety going?

  interface Session {
    user?: {
      id?: string;
    } & DefaultSession["user"];
  }

The current setup where they are not optional:

  interface Session {
    user: {
      id: string;
    } & DefaultSession["user"];
  }

would result in a runtime error if trying to access the id:

console.log(session?.user.id); // no type-errors, but the user-object is undefined

@bhatvikrant
Copy link
Contributor Author

if we only include this when Prisma is also included, would that make more sense?

I'm fine with the session binding being "incorrect" if you don't use a DB persistence

I think modifying the installer to have two separate api-handlers would be better? Also not include the next-auth.d.ts if no db is included?

On second thought it doesn't really matter since the user-object's type exists whether there is a db or not. This is from a fresh t3 app with just next-auth: CleanShot 2022-07-12 at 19 48 41

So having an id on there doesn't really matter I guess...

The above solution would also require two different tsconfigs and now it's really getting annoying...

But making the user (and the id) optional in the next-auth.ts I guess would be better to keep the typesafety going?

  interface Session {
    user?: {
      id?: string;
    } & DefaultSession["user"];
  }

The current setup where they are not optional:

  interface Session {
    user: {
      id: string;
    } & DefaultSession["user"];
  }

would result in a runtime error if trying to access the id:

console.log(session?.user.id); // no type-errors, but the user-object is undefined

I agree, making user and id optional. This should be better for type-safety

@bhatvikrant
Copy link
Contributor Author

Hey @juliusmarminge @theobr, I've made the suggested changes and the PR already has 2 approvals.
Whats next to get it merged?

@nexxeln nexxeln merged commit 4c162a7 into t3-oss:main Jul 14, 2022
devvianto605 pushed a commit to devvianto605/create-devviantex-nextjs-app-deprecated that referenced this pull request Jun 9, 2024
* chore: include user.id on session

* chore: include next-auth.d.ts only for next-auth scaffoldings

* Update template/addons/next-auth/api-handler-prisma.ts

Co-authored-by: Balázs Orbán <[email protected]>

* Update template/addons/next-auth/api-handler.ts

Co-authored-by: Balázs Orbán <[email protected]>

* Update template/addons/next-auth/next-auth.d.ts

Co-authored-by: Balázs Orbán <[email protected]>

* fix: include `next-auth.d.ts` in `tsconfig.json`

* fix: run prettier

* chore: make `user` and `id` optional for better type-safety

* fix: add `id` to `session.user` only when it exists

Co-authored-by: Balázs Orbán <[email protected]>
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

Successfully merging this pull request may close these issues.

Include user.id on session
6 participants