Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): add callbackUrl parameter for redirect after sign-in #3290

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions keep-ui/app/(signin)/signin/SignInForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ interface SignInFormInputs {
password: string;
}

export default function SignInForm({ params }: { params?: { amt: string } }) {
export default function SignInForm({
params,
searchParams,
}: {
params?: { amt: string };
searchParams: { [key: string]: string | string[] | undefined };
}) {
console.log("Init SignInForm");
const [providers, setProviders] = useState<Providers | null>(null);
const [isRedirecting, setIsRedirecting] = useState(false);
const router = useRouter();

const {
register,
handleSubmit,
Expand Down Expand Up @@ -71,7 +78,9 @@ export default function SignInForm({ params }: { params?: { amt: string } }) {
providers.credentials &&
providers.credentials.name == "NoAuth"
) {
signIn("credentials", { callbackUrl: "/" });
signIn("credentials", {
callbackUrl: (searchParams["callbackUrl"] as string) || "/",
});
}
}
}, [providers, params]);
Expand Down
10 changes: 8 additions & 2 deletions keep-ui/app/(signin)/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"use client";
import SignInForm from "./SignInForm";

export default function SignInPage({ params }: { params: { amt: string } }) {
return <SignInForm params={params} />;
export default function SignInPage({
params,
searchParams,
}: {
params: { amt: string };
searchParams: { [key: string]: string | string[] | undefined };
}) {
return <SignInForm params={params} searchParams={searchParams} />;
}
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
Loading