[GH-ISSUE #425] Bug: CallbackURL is not redirecting the users to the specified page #8268

Closed
opened 2026-04-13 03:21:11 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @firstaxel on GitHub (Nov 5, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/425

import { AuthFormSchema } from "@/app/components/auth/register-form"
import { authClient } from "@/lib/auth.client"
import { useMutation } from "@tanstack/react-query"
import { useRouter } from "next/navigation"
import { toast } from "sonner"

const useAuth = (action: "LOGIN" | "REGISTER", callbackURL?: string) => {
  const router = useRouter()
  const mutation = useMutation({
    mutationFn: async (data: AuthFormSchema) => {
      if (action === "LOGIN") {
        return await authClient.signIn.email({
          email: data.email,
          password: data.password,
          callbackURL,
        })
      }
      if (action === "REGISTER") {
        const name = data.name?.first + " " + data.name?.last
        return await authClient.signUp.email({
          name,
          email: data.email,
          password: data.password,
          callbackURL,
        })
      }
    },
    onSuccess: (data) => {
      if (action === "LOGIN") {
        toast.success("Successfully logged in")
      }
      if (action === "REGISTER") {
        if (data?.error?.status === 422) toast.error("User already exists")
        else {
          toast.success("Successfully registered")
          router.push(callbackURL as string)
        }
      }
    },
    onError: (err) => {
      if (action === "LOGIN") {
        toast.error("Error logging in")
      }
      if (action === "REGISTER") {
        toast.error("Error registering your account")
      }
    },
  })

  return mutation
}

export { useAuth }

expected behavior: Rediect to the callbackurl

Originally created by @firstaxel on GitHub (Nov 5, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/425 ``` import { AuthFormSchema } from "@/app/components/auth/register-form" import { authClient } from "@/lib/auth.client" import { useMutation } from "@tanstack/react-query" import { useRouter } from "next/navigation" import { toast } from "sonner" const useAuth = (action: "LOGIN" | "REGISTER", callbackURL?: string) => { const router = useRouter() const mutation = useMutation({ mutationFn: async (data: AuthFormSchema) => { if (action === "LOGIN") { return await authClient.signIn.email({ email: data.email, password: data.password, callbackURL, }) } if (action === "REGISTER") { const name = data.name?.first + " " + data.name?.last return await authClient.signUp.email({ name, email: data.email, password: data.password, callbackURL, }) } }, onSuccess: (data) => { if (action === "LOGIN") { toast.success("Successfully logged in") } if (action === "REGISTER") { if (data?.error?.status === 422) toast.error("User already exists") else { toast.success("Successfully registered") router.push(callbackURL as string) } } }, onError: (err) => { if (action === "LOGIN") { toast.error("Error logging in") } if (action === "REGISTER") { toast.error("Error registering your account") } }, }) return mutation } export { useAuth } ``` expected behavior: Rediect to the callbackurl
GiteaMirror added the locked label 2026-04-13 03:21:11 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8268