Skip to content

Commit

Permalink
Setup sign in page to redirect back to the page that linked it
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jun 14, 2024
1 parent e847dc0 commit 2815c79
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/app/setup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -27,6 +27,7 @@ function FullName({
setActiveTab: Dispatch<SetStateAction<number>>;
}) {
const { user, isLoaded } = useUser();
const pathname = usePathname();

if (!isLoaded) {
// Handle loading state however you like
Expand All @@ -39,7 +40,7 @@ function FullName({
}

if (!user) {
return redirect("/sign-in");
return redirect("/sign-in?redirect=" + pathname);
}

const formSchema = z.object({
Expand Down
8 changes: 7 additions & 1 deletion src/app/sign-in/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="text-xl">
<SignedOut>
Signing you in...
<RedirectToSignIn />
<RedirectToSignIn signInForceRedirectUrl={redirect} />
</SignedOut>
<SignedIn>
You are already signed in
Expand Down

0 comments on commit 2815c79

Please sign in to comment.