How do i redirect to a specific provider login within Next.js Middleware

I'm trying:

import { type NextRequest, NextResponse } from "next/server";
import { headers } from "next/headers";

import { auth } from "@/lib/auth";

export async function middleware(request: NextRequest) {
  const pathname = request.nextUrl.pathname;

  if (!pathname.startsWith("/dashboard")) {
    return NextResponse.next();
  }

  const session = await auth.api.getSession({
    headers: request.headers,
  });

  if (session?.user?.scopes.includes("guilds")) {
    return NextResponse.next();
  }

  const response = await auth.api.signInSocial({
    body: { provider: "discord", callbackURL: pathname },
    headers: await headers(),
  });

  console.log("Redirecting to: ", response.url);
  return NextResponse.redirect(response.url);
}

export const config = {
  matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};


But this creates the discord oauth url with a relative redirect_uri: /callback/discord
Which returns this from discord: {"redirect_uri": ["Not a well formed URL."]}
Was this page helpful?