[GH-ISSUE #1555] Redirecting to /api/auth on success #8815

Closed
opened 2026-04-13 04:01:46 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @soham2k06 on GitHub (Feb 24, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1555

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. sign in/up using google provider

Current vs. Expected behavior

I am expecting it not to redirect anywhere. Instead I want to redirect using next/navigation hooks using better-auth's onSuccess.
But when it redirects to '/' page that is secured by middleware, it does not have the cookie yet in the moment.

Cookies are being set properly though. No errors on terminal.

What version of Better Auth are you using?

^1.1.18

Provide environment information

- OS: Mac Sonoma
- Browser: Chrome

Which area(s) are affected? (Select all that apply)

Client

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";

import prisma from "./db/prisma";

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "mongodb",
  }),

  emailAndPassword: {
    enabled: true,
  },

  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID!,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    },
  },
});

Additional context

Here is my google sign in handler:

  async function handleGoogleSignup() {
    await authClient.signIn.social({
      provider: "google",

      fetchOptions: {
        onSuccess: () => {
          console.log("success");
          toast.success("Sign in successful");
          router.push("/");
        },
      },
    });
  }

Here is my middleware.ts:

import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth";

export async function middleware(request: NextRequest) {
  const sessionCookie = getSessionCookie(request);

  const isAuthRoute = request.nextUrl.pathname.startsWith("/auth/");

  // If user is signed in and tries to access auth pages, redirect to home
  if (isAuthRoute && sessionCookie)
    return NextResponse.redirect(new URL("/", request.url));

  // If user is NOT signed in and trying to access a protected page, redirect to login
  if (!sessionCookie && !isAuthRoute)
    return NextResponse.redirect(new URL("/auth/login", request.url));

  return NextResponse.next();
}

export const config = {
  matcher: ["/", "/((?!api|_next|.*\\..*).*)"],
};

Originally created by @soham2k06 on GitHub (Feb 24, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1555 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. sign in/up using google provider ### Current vs. Expected behavior I am expecting it not to redirect anywhere. Instead I want to redirect using next/navigation hooks using better-auth's `onSuccess`. But when it redirects to '/' page that is secured by middleware, it does not have the cookie yet in the moment. Cookies are being set properly though. No errors on terminal. ### What version of Better Auth are you using? ^1.1.18 ### Provide environment information ```bash - OS: Mac Sonoma - Browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import prisma from "./db/prisma"; export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "mongodb", }), emailAndPassword: { enabled: true, }, socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID!, clientSecret: process.env.GOOGLE_CLIENT_SECRET!, }, }, }); ``` ### Additional context Here is my google sign in handler: ``` async function handleGoogleSignup() { await authClient.signIn.social({ provider: "google", fetchOptions: { onSuccess: () => { console.log("success"); toast.success("Sign in successful"); router.push("/"); }, }, }); } ``` Here is my `middleware.ts`: ``` import { NextRequest, NextResponse } from "next/server"; import { getSessionCookie } from "better-auth"; export async function middleware(request: NextRequest) { const sessionCookie = getSessionCookie(request); const isAuthRoute = request.nextUrl.pathname.startsWith("/auth/"); // If user is signed in and tries to access auth pages, redirect to home if (isAuthRoute && sessionCookie) return NextResponse.redirect(new URL("/", request.url)); // If user is NOT signed in and trying to access a protected page, redirect to login if (!sessionCookie && !isAuthRoute) return NextResponse.redirect(new URL("/auth/login", request.url)); return NextResponse.next(); } export const config = { matcher: ["/", "/((?!api|_next|.*\\..*).*)"], }; ```
GiteaMirror added the lockedbug labels 2026-04-13 04:01:46 -05:00
Author
Owner

@soham2k06 commented on GitHub (Feb 24, 2025):

I think we can just use callbackUrl. May be no need to use single page routing. At least for me it is not needed.
Closing the issue.

<!-- gh-comment-id:2677452539 --> @soham2k06 commented on GitHub (Feb 24, 2025): I think we can just use `callbackUrl`. May be no need to use single page routing. At least for me it is not needed. Closing the issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8815