From 2815c7912d8e9c2c51d43a9780b568fe9deda523 Mon Sep 17 00:00:00 2001 From: Aamir Azad Date: Fri, 14 Jun 2024 17:07:11 +0530 Subject: [PATCH] Setup sign in page to redirect back to the page that linked it --- src/app/setup/page.tsx | 5 +++-- src/app/sign-in/page.tsx | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/app/setup/page.tsx b/src/app/setup/page.tsx index c4b61be..36c908c 100644 --- a/src/app/setup/page.tsx +++ b/src/app/setup/page.tsx @@ -18,7 +18,7 @@ import { z } from "zod"; import { useForm } from "react-hook-form"; import { Dispatch, SetStateAction } from "react"; import { useUser } from "@clerk/nextjs"; -import { redirect } from "next/navigation"; +import { redirect, usePathname } from "next/navigation"; import { LoaderCircle } from "lucide-react"; function FullName({ @@ -27,6 +27,7 @@ function FullName({ setActiveTab: Dispatch>; }) { const { user, isLoaded } = useUser(); + const pathname = usePathname(); if (!isLoaded) { // Handle loading state however you like @@ -39,7 +40,7 @@ function FullName({ } if (!user) { - return redirect("/sign-in"); + return redirect("/sign-in?redirect=" + pathname); } const formSchema = z.object({ diff --git a/src/app/sign-in/page.tsx b/src/app/sign-in/page.tsx index 295e833..f85ab1d 100644 --- a/src/app/sign-in/page.tsx +++ b/src/app/sign-in/page.tsx @@ -1,12 +1,18 @@ +"use client"; + import { RedirectToSignIn, SignedIn, SignedOut } from "@clerk/nextjs"; import Link from "next/link"; +import { useSearchParams } from "next/navigation"; export default function SignIn() { + const searchParams = useSearchParams(); + const redirect = searchParams.get("redirect"); + return (
Signing you in... - + You are already signed in