ToothyT
Convex Community6mo ago
2 replies
Toothy

Discord oauth with Convex Auth and invalid redirect uri.

Hi!

I've been trying to setup discord oauth with convex and it's giving me so much issues!

Here's my auth.ts
import Discord from "@auth/core/providers/discord";
import { convexAuth } from "@convex-dev/auth/server";

export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
  providers: [
    Discord({
      clientId: process.env.DISCORD_AUTH_CLIENT_ID,
      clientSecret: process.env.DISCORD_AUTH_CLIENT_SECRET,
      authorization: { params: { scope: "identify" } },
    }),
  ],
});


Here's the sign in button:
import { useAuthActions } from "@convex-dev/auth/react";
import { Button } from "../ui/button";

export const DiscordSignIn = () => {
  const { signIn } = useAuthActions();
  const handleSignIn = async () => {
    try {
      await signIn("discord");
    } catch (error) {
      console.error("Sign-in error:", error);
    }
  };
  return <Button onClick={handleSignIn}>Sign in with Discord</Button>;
};


And here's my middleware (with next-intl):
import { convexAuthNextjsMiddleware } from "@convex-dev/auth/nextjs/server";
import type { NextFetchEvent, NextRequest } from "next/server";
import createMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";

const intlMiddleware = createMiddleware(routing);
const convexAuthMiddleware = convexAuthNextjsMiddleware();

export async function middleware(request: NextRequest, event: NextFetchEvent) {
  const { pathname } = request.nextUrl;
  const isApiRoute =
    pathname.startsWith("/api") || pathname.startsWith("/trpc");
  const isNextInternal = pathname.startsWith("/_next");
  const isStaticFile = pathname.includes(".");

  if (isApiRoute || isNextInternal || isStaticFile) {
    if (isApiRoute) {
      return convexAuthMiddleware(request, event);
    }
    return;
  }

  const intlResponse = await intlMiddleware(request);

  if (intlResponse) {
    return intlResponse;
  }

  return convexAuthMiddleware(request, event);
}

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


NEXT_PUBLIC_CONVEX_URL=http://127.0.0.1:3210 so this is default.

I've been trying to set up callback url within discord dev portal to variations of http://localhost:3210/auth/callback/discord, I tried localhost, tried 127.0.0.1, tried 3210 and 3000 ports and it always gives me wrong oauth2 redirect_uri error.

I am out of ideas and I was hoping I can get some help here.
Was this page helpful?