Next.js Discord

Discord Forum

Extending User in Authjs

Unanswered
Bombay posted this in #help-forum
Open in Discord
BombayOP
How do I exactly extend the user properties in Authjs? 🤔
adding a custom profile() to return the full Discord user object does reflect on the database; But the session only returns email
what am i missing?

19 Replies

Large garden bumble bee
You need to edit the callback that returns the session object, more information is on nextauth docs
the provider's config?
Large garden bumble bee
in the auth options, outside of providers
BombayOP
this is what I'm using
SvelteKitAuth({
        providers: [
            Discord({
                clientId: DISCORD_CLIENT_ID,
                clientSecret: DISCORD_CLIENT_SECRET,
                authorization: {
                    params: {
                        scope: "identify guilds guilds.members.read email" // this doesn't work fyi
                    }
                },
                checks: ["pkce"],

            })],
        adapter: MongoDBAdapter(clientPromise)
    })
and fyi, many things don't even work in the Discord provider config
alright, ill try outside of the provider
Large garden bumble bee
SvelteKitAuth({
        providers: [
            Discord({
                clientId: DISCORD_CLIENT_ID,
                clientSecret: DISCORD_CLIENT_SECRET,
                authorization: {
                    params: {
                        scope: "identify guilds guilds.members.read email" // this doesn't work fyi
                    }
                },
                checks: ["pkce"],

            })],
    callbacks: {
        async session({ session, token, user }) {
            // Send properties to the client, like an access_token and user id from a provider.
            session.user.id = user.discordId;
            session.user.name = user.name;
            session.user.displayName = user.global_name;
            session.user.two_factor_enabled = user.two_factor_enabled;
            session.user.email = user.email;
            session.user.image = user.image;

            return session
        }
    },
        adapter: MongoDBAdapter(clientPromise)
    })
i think it would be something like this
BombayOP
yep, it logs when i try that outside of providers
i guess i can modify the session user object from the parent user object
so that's solved
any idea why i can't change the Discord OAuth scope?
authorization: {
                    params: {
                        scope: "identify guilds guilds.members.read email"
                    }
                },
checks: ["pkce"],


.scope is always the default identify email even when i extend it (based on docs)

.checks must always be pkce, doing state or none leads to callback error always
checking Authjs internals, this is the part that should be responsible
even if i do supply authorization?.params, it can't apply since authorization?.url.searchParams is the default https://discord.com/api/oauth2/authorize?scope=identify+email
so it's basically
Object.assign({
        response_type: "code",
scope: "identify guilds guilds.members.read email" //what i want as scope
    }, Object.fromEntries(new URL("https://discord.com/api/oauth2/authorize?scope=identify+email").searchParams ?? []));
// ?scope=identify+email ===> "identify email" taking over always
that's assuming my custom scope is actually making it to ...provider.authorization?.params