Next.js Discord

Discord Forum

Purposeful Non-Session OAuth2

Unanswered
Catla posted this in #help-forum
Open in Discord
Avatar
CatlaOP
I have an application that links a Discord account to various Roblox accounts. I use Next-Auth (v5) for accessing OAuth for both Discord and Roblox.

What is the best way to make signing in with Roblox not create a session or any form of tokens, rather just write the account information to a db and move on?

Here's my current auth.ts:

2 Replies

Avatar
CatlaOP
export const {
    handlers: { GET, POST },
    auth,
    signIn,
    signOut,
    update
} = NextAuth({
    callbacks: {
        async session({ session, token }) {
            session.user.id = token.sub;

            return session;
        },
        async signIn({ user, account, profile }) {
            if (account?.provider === "discord" && profile) {
                user.image = profile.avatar ? `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png` : `https://cdn.discordapp.com/embed/avatars/${Math.abs(Number(profile.id) >> 22) % 5}.png`
            } else if (account?.provider === "roblox") {
                const session = await auth();

                const existingAccount = await db.accounts.findUnique({
                    where: {
                        id: session?.user.id
                    },
                });

                if (existingAccount) return '/manage/accounts';

                const existingAccounts = await db.accounts.findMany({
                    where: {
                        ownerId: session?.user.id,
                    },
                });

                const isPrimary = existingAccounts.length === 0;

                await db.accounts.create({
                    data: {
                        id: user.id,
                        ownerId: session?.user.id,
                        isPrimary: isPrimary,
                    },
                });

                return '/manage/account';
            }

            return true;
        },
    },
    pages: { signIn: '/' },
    session: { strategy: "jwt" },
    ...authConfig
});


You can see where I'm returning '/manage/accounts' I am currently ignoring the signIn function and passing them onto the manage accounts page. This works, but since v5 I can no longer return a string from the signIn function to ignore & redirect.

Hence I'm asking for a better way.

Thanks!
Avatar
CatlaOP
Bump