Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

bespy
bespy

Posted on

NextAuth TwitterProvider UserId on Session

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};},},});
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp