Next.js Discord

Discord Forum

How to save user data from oauth provider after authorization completes?

Unanswered
Tonkinese posted this in #help-forum
Open in Discord
TonkineseOP
Hey guys, I'm setting up authjs using the prisma adapter for mongo and the discord provider, I've got the authorization redirect working successfuly (to app/api/auth/callback/discord), not unsure what do next in order for that info to be saved to the database.

This is what my auth.ts file looks like:
const config = {
  ...authConfig, // discord provider
  adapter: PrismaAdapter(prisma),
  callbacks: {
    authorized({ request, auth }) {
      const { pathname } = request.nextUrl;

      if (pathname === "/middleware-example") return !!auth;

      return true;
    },
    jwt({ token, trigger, session, account }) {
      if (trigger === "update") token.name = session.user.name;

      if (account?.provider === "discord") {
        return { ...token, accessToken: account.access_token };
      }

      return token;
    },
    async session({ session, token, user }) {
      if (token?.accessToken) {
        session.accessToken = token.accessToken;
      }

      return session;
    },
  },
  experimental: {
    enableWebAuthn: true,
  },
  debug: process.env.NODE_ENV !== "production" ? true : false,
} satisfies NextAuthConfig;

export const { handlers, auth, signIn, signOut } = NextAuth(config);

declare module "next-auth" {
  interface Session {
    accessToken?: string;
  }
}

declare module "next-auth/jwt" {
  interface JWT {
    accessToken?: string;
  }
}

3 Replies

TonkineseOP
session is also undefined in my Page:
export default async function Page() {
  const session = await auth();
  if (!session) console.log("NO SESSION");
}
TonkineseOP
and app/api/auth/callback/discord/route.ts:
import { NextResponse } from "next/server";

export async function GET(req: Request) {
  const urlParams = new URL(req.url).searchParams;

  const code = urlParams.get("code");

  console.log(`CODE: ${code}`);

  return NextResponse.redirect("http://localhost:3001");
}
TonkineseOP
UPDATE: I've been able to get the Credentials provider working, maybe it's an issue with the discord provider?