Skip to content

Commit

Permalink
fix(auth): add callbackUrl parameter for redirect after sign-in
Browse files Browse the repository at this point in the history
  • Loading branch information
skynetigor committed Feb 3, 2025
1 parent b120ddc commit 4d4727f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 5 additions & 2 deletions keep-ui/app/(signin)/signin/SignInForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Text, TextInput, Button } from "@tremor/react";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { authenticate, revalidateAfterAuth } from "@/app/actions/authactions";
import { useRouter } from "next/navigation";
import { useRouter, useSearchParams } from "next/navigation";
import "../../globals.css";

export interface Provider {
Expand Down Expand Up @@ -33,6 +33,7 @@ export default function SignInForm({ params }: { params?: { amt: string } }) {
const [providers, setProviders] = useState<Providers | null>(null);
const [isRedirecting, setIsRedirecting] = useState(false);
const router = useRouter();
const searchParams = useSearchParams();
const {
register,
handleSubmit,
Expand Down Expand Up @@ -71,7 +72,9 @@ export default function SignInForm({ params }: { params?: { amt: string } }) {
providers.credentials &&
providers.credentials.name == "NoAuth"
) {
signIn("credentials", { callbackUrl: "/" });
signIn("credentials", {
callbackUrl: searchParams.get("callbackUrl") || "/",
});
}
}
}, [providers, params]);
Expand Down
5 changes: 4 additions & 1 deletion keep-ui/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ export const middleware = auth(async (request) => {

// If not authenticated and not on signin page, redirect to signin
if (!isAuthenticated && !pathname.startsWith("/signin") && !pathname.startsWith("/health")) {
const redirectTo = request.nextUrl.href || "/incidents";
console.log("Redirecting to signin page because user is not authenticated");
return NextResponse.redirect(new URL("/signin", request.url));
return NextResponse.redirect(
new URL(`/signin?callbackUrl=${redirectTo}`, request.url)
);
}

// If authenticated and on signin page, redirect to incidents
Expand Down

0 comments on commit 4d4727f

Please sign in to comment.