ctx.user.session.email possibly null | undefined
It's a protected procedure and the only sign-in methods I plan to have is Google and standard Email/Password where in both cases a user will always have an email so how can I convince typescript that this is typesafe? Other than something like
String(ctx.session.user.email)
or ctx.session.user.email ?? ""
7 Replies
just add an exclamation point
ctx.session.user.email!
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I think if you make email the email column non-nullable in your database, then regenerate the prisma client, it should be inferred as non-optional
Well it works but eslint throws a warning for it so I removed the warning in my eslint
That's the weird thing it is non-nullable in my schema but whatever the other solution works
Is the middleware checking the whole session or just the user id?
It depends on where the type is coming from. IIRC you can override the
Session
type provided by NextAuth (which I assume you're using)
So take a look at if anything is attempting to add email
to the Session
object and see if you can make it nonnull.
Alternatively ctx.session.user.email ?? undefined
works too because prisma happily accepts undefined (if you read that error message closely)i do have the exact same problem. especially using
session?.user.id