Next.js Discord

Discord Forum

Appending information to server session

Answered
Catla posted this in #help-forum
Open in Discord
CatlaOP
Hi.

I am struggling to get the discordId via getServerSession(). discordId (as you can see in the session/jwt function) is appended to session.user, but cannot be accessed via the getServerSession() present in the signIn function.

Observe:
Answered by Catla
Resolved. For some reason my user wasn't printing with discordId visible, but when logged directly (console.log(session?.user.discordId)) it logged the correct value.
View full answer

4 Replies

CatlaOP
These exist within callbacks in my auth options.
        async session({ session, token }) {
            if (token.discordId) {
                session.user.discordId = token.discordId;
            }

            return session;
        },
        async jwt({ token, user, account }) {
            if (account?.provider === "discord" && user) {
                token.discordId = user.id;
            }

            return token;
        },
        async signIn({ user, account }) {
            if (account?.provider === "roblox") {
                const session = await getServerSession();
                console.log(session?.user);

                //get discord id from session
                //identify discord user document in database
                //append roblox account id to accounts array inside document

                return '/manage/accounts';
            } else if (account?.provider === "discord" && user) {
                (async () => {
                    const existingUser = await prisma.user.findUnique({
                        where: {
                            id: user.id,
                        },
                    });

                    if (!existingUser) {
                        await prisma.user.create({
                            data: {
                                id: user.id as string,
                                refreshToken: account?.refresh_token as string,
                                accounts: {
                                    primaryId: '',
                                    accountIds: [],
                                },
                            },
                        });
                    }
                })()
            };

            return true;
        },
CatlaOP
Resolved. For some reason my user wasn't printing with discordId visible, but when logged directly (console.log(session?.user.discordId)) it logged the correct value.
Answer
thats a bit odd though, you should get all values by just printing session.user