If you follow theNextAuth instructions for the TwitterProvider you won't immediately get the UserId on the Session Provider, when you use theuseSession()
hook. The below code worked for me.
importNextAuthfrom'next-auth';importTwitterProviderfrom'next-auth/providers/twitter';import{addSignedInUserToDb}from'@/app/auth-client';exportconst{handlers,signIn,signOut,auth}=NextAuth({providers:[TwitterProvider({clientId:process.env.TWITTER_CLIENT_ID,clientSecret:process.env.TWITTER_CLIENT_SECRET,}),],session:{},secret:process.env.SECRETasstring,callbacks:{asyncsignIn({user,account,profile,email,credentials}){user.x_id=profile.data.id;// this is where i add the userId to dbawaitaddSignedInUserToDb(user);returntrue;},asyncredirect({url,baseUrl}){returnbaseUrl;},asyncsession({session,user,token}){session.user.x_id=token.data.id;returnsession;},asyncjwt({token,user,account,profile}){return{...token,...user,...account,...profile};},},});
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse