You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement social account linking by combining logins from multiple accounts into one JWT
Non-Goals
No response
Background
I'm working on an app which uses the credentials provider as the main way to sign in, but I also want to allow the user linking multiple social accounts.
When signing with a different provider, the JWT data is replaced, which makes linking data from multiple accounts into one JWT difficult.
Proposal
I can currently make this work by implementing the jwt callback and parsing the current session cookies to generate the combined JWT
asyncjwt({ token, user, trigger, account, profile }){if(trigger!=='signIn'||!user){returntoken}if(account?.provider==='credentials'){// logging in with credentialsreturntoken}constprovider=account?.providerif(!provider){returntoken}constsessionCookie=cookies().get("authjs.session-token")?.valueif(!sessionCookie){console.warn('No session cookie found')returntoken}constsecret=process.env.AUTH_SECRET// the "verify" function will decrypt/verify the session cookieconstcurrentJwt=awaitverify(secret,sessionCookie)constlinkedAccounts=(currentJwt.linkedAccounts??{})asRecord<string,string>switch(provider){case'github':
linkedAccounts[provider]=profile?.loginasstringbreak;case'twitter':
linkedAccounts[provider]=(profile?.dataasany)?.usernameasstringbreak;default:
console.warn('Unknown provider',provider,profile)break;}return{
...currentJwt,
linkedAccounts
}}
While the above works fine for me, it seems a bit redundant that I have to re-parse the JWT from the current session. What I would like is for the currentJwt to be available as a parameter to the jwt callback (I assume the data is already available in the context that calls the jwt function).
I'm rather new to next-auth, so if there's a simpler way to do social account linking, please let me know. My prior search led to #1702
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Goals
Non-Goals
No response
Background
I'm working on an app which uses the credentials provider as the main way to sign in, but I also want to allow the user linking multiple social accounts.
When signing with a different provider, the JWT data is replaced, which makes linking data from multiple accounts into one JWT difficult.
Proposal
I can currently make this work by implementing the jwt callback and parsing the current session cookies to generate the combined JWT
While the above works fine for me, it seems a bit redundant that I have to re-parse the JWT from the current session. What I would like is for the currentJwt to be available as a parameter to the jwt callback (I assume the data is already available in the context that calls the jwt function).
I'm rather new to next-auth, so if there's a simpler way to do social account linking, please let me know. My prior search led to #1702
Beta Was this translation helpful? Give feedback.
All reactions