Replies: 1 comment 9 replies
-
You should do all the logic in the same if block, otherwise things will be overridden. Check the docs: https://next-auth.js.org/configuration/callbacks#jwt-callback
export default NextAuth({
callbacks: {
async jwt(token, user, account, profile, isNewUser) {
// Sign-in
if (user) {
token.profile = user;
token.accessToken = account.accessToken;
const res = await fetch("https://discord.com/api/v8/users/@me/guilds", {
headers: { Authorization: `Bearer ${account.accessToken}` },
})
const data = await res.json()
token.profile.guilds = data
}
return token
},
session(session, token) {
session.profile = token.profile
return session;
},
}
providers: [
Providers.Discord({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
profile(profile) {
// What you return here will be the same as the second argument in the jwt() callback at signin.
return
}
})
]
}) The fourth parameter is actually ALL the information the provider will return, you probably don't need everything. You can filter it down with the provider's profile callback. Regarding TS, better typings are coming built-in. Follow #1649 |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to authenticate users with Discord using the "identify" and "guilds" scopes. It seems to be possible to add the user info provided by Discord into the token into
token.profile
(note I am also including the guilds array there as well) using this code:But, as soon as I add a
session
function that adds theprofile
object into the session, the session becomesnull
all the time even when signed in, and the login screen from Discord prompts me to enter username and password instead of just automatically letting me in.As a side-note, the reason I have to add so many
@ts-ignore
s in thesession
function is that it expects aPromise<WithAdditionalParams<Session>>
while I only return aPromise<Session>
, not sure how to fix that.Can anyone help me add the information I receive from Discord into the
session
so that I can use it in my application?Beta Was this translation helpful? Give feedback.
All reactions