find logged user's row in the DB by SOME id?
Unanswered
piscopancer posted this in #help-forum
I logged into the github account and used
👇
Firstly, there is no id provided by the OAuth service like github. Why is that?
Secondly, I cannot straight up use
So, assuming these are the only 3 fields I have. How should I find the row? Also, if you have a better solution, please speak, I just started and I realize I miss a lot of points
account.providerAccountId (which is permanent) as id for the DB row. Now I need read the user in the server component:import { auth } from '@/auth'
export default async function Home() {
const authUser = await auth().then((s) => s?.user) // User | undefined
}👇
{
name: 'piscopancer',
email: '...@gmail.com',
image: 'https://avatars.githubusercontent.com/u/109352196?v=4'
}Firstly, there is no id provided by the OAuth service like github. Why is that?
Secondly, I cannot straight up use
email because I may have also logged in with my Google account, which you know, is linked to the same email address. So I will have to restrict my DB to allow only 1 email address per user bcs now it's a unique identifier which is weird.So, assuming these are the only 3 fields I have. How should I find the row? Also, if you have a better solution, please speak, I just started and I realize I miss a lot of points
40 Replies
all providers does provide id but next auth chooses to only give name, email and image in the default session object. You can change that behaviour by adding the type for it
check https://next-auth.js.org/getting-started/typescript#module-augmentation
check https://next-auth.js.org/getting-started/typescript#module-augmentation
I tried to alter the async callbacks the following way but it does not affect the
auth() returned object jwt: async ({ account, token }) => {
console.log(account)
token.accountId = account?.providerAccountId
return token
},
session: async ({ session, token }) => {
session.user.accountId = token.accountId
return session
},the problem is that in
token.accountId = account?.providerAccountId the account is undefined because account is only defined in the signIn async callbackso I am struggling with what approach to use and what to type where
u have to alter the session interface
I did so with
types.d.tsimport 'next-auth'
import 'next-auth/jwt'
declare global {
namespace NodeJS {
interface ProcessEnv {
TURSO_URL: string
TURSO_TOKEN: string
AUTH_GITHUB_ID: string
AUTH_GITHUB_SECRET: string
}
}
}
declare module 'next-auth' {
interface User {}
interface Session {
user: User & {
accountId: string | undefined
}
}
}
declare module 'next-auth/jwt' {
interface JWT {
accountId: string | undefined
}
}
export {}if that's what you are talkin about
and I get suggestions, but it's just types
they do not even matter
I do not know how to carry the
providerAccountId into the sessionor user
import { DefaultSession } from "next-auth";
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
accountId: string | undefined;
} & DefaultSession["user"];
}
}try this
what does ur auth function do?
in the jwt and session callback, are u sure account id is being returned from them?
@<Milind ツ /> in the jwt and session callback, are u sure account id is being returned from them?
yes, I can hardcode it to any word and then it's returned
@<Milind ツ /> what does ur auth function do?
next-auth function
import GitHub from '@auth/core/providers/github'
import NextAuth from 'next-auth'
import { db } from './db'
import { users } from './db/schema'
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
providers: [GitHub],
...oh its v5
yes
what does await auth() return without doing .then?
@<Milind ツ /> what does await auth() return without doing .then?
{
user: {
name: 'piscopancer',
email: 'igor.bistr01092003@gmail.com',
image: 'https://avatrs.githubusercontent.com/u/109352196?v=4'
},
expires: '2024-04-07T10:19:14.147Z'
}lemme try it on my end. i have no idea about v5
oh I may be wrong, but
sub is the github account permanent id session: async ({ session, token }) => {
session.user.accountId = token.sub
return session
},bcs now I can finally see it and it's the same number which was used to create a new row in the DB
i tried logging out and in and it persists
can it be the solution?
yea why not.
try logging the whole token before returning and see what all details it is returning
more weird, the value of sub is different for me
its like jti
wait no ðŸ˜
I just accepted the fact that this works fine
@<Milind ツ /> more weird, the value of sub is different for me
are you sure about that?
yea, not sure if it should
I am not going to mark this question as resolved bcs your
sub changes and who knows if this is the right solution, at the end of the day it could also turn out to be a silly workaround and I should not have ever used it