Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(auth): redirect to `targetPath` after login (#1913)

Rahul Soni52cfba89e894
diff --git a/web/src/__e2e__/auth.spec.ts b/web/src/__e2e__/auth.spec.ts
index e79f39b13f..b30d285388 100644
--- a/web/src/__e2e__/auth.spec.ts
+++ b/web/src/__e2e__/auth.spec.ts
@@ -70,3 +70,38 @@ test("Signup input validation", async ({ page }) => {
// random email address to be used in tests
const randomEmailAddress = () =>
Math.random().toString(36).substring(2, 11) + "@example.com";
+
+test("Unauthenticated user should be redirected to target URL after login", async ({
+ page,
+}) => {
+ await page.goto("/auth/sign-in");
+ await page.fill('input[name="email"]', "[email protected]");
+ await page.fill('input[type="password"]', "password");
+ await page.click('button[data-testid="submit-email-password-sign-in-form"]');
+
+ // wait 2 seconds
+ await page.waitForTimeout(2000);
+
+ // project id and prompt from seed.ts
+ const promptUrl = "/project/7a88fb47-b4e2-43b8-a06c-a5ce950dc53a/prompts/summary-prompt";
+
+ await page.getByRole("button", { name: /Demo User/ }).click();
+
+ await page.getByRole("menuitem", { name: "Sign Out" }).click();
+
+ await expect(page).toHaveURL("/auth/sign-in");
+
+ await page.goto(promptUrl);
+
+ await page.waitForTimeout(2000);
+
+ await expect(page).toHaveURL(/targetPath/);
+
+ await page.fill('input[name="email"]', "[email protected]");
+ await page.fill('input[type="password"]', "password");
+ await page.click('button[data-testid="submit-email-password-sign-in-form"]');
+
+ await page.waitForTimeout(2000);
+
+ await expect(page).toHaveURL(promptUrl);
+});
diff --git a/web/src/components/error-page.tsx b/web/src/components/error-page.tsx
index 7633cfcef1..ff6c5b7fa0 100644
--- a/web/src/components/error-page.tsx
+++ b/web/src/components/error-page.tsx
@@ -14,6 +14,7 @@ export const ErrorPage = ({
}) => {
const session = useSession();
const router = useRouter();
+ const newTargetPath = router.asPath;
return (
<div className="flex h-screen flex-col items-center justify-center">
@@ -21,7 +22,15 @@ export const ErrorPage = ({
<h1 className="mb-4 text-xl font-bold">{title}</h1>
<p className="mb-8 text-center">{message}</p>
{session.status === "unauthenticated" ? (
- <Button onClick={() => void router.push("/")}>Sign In</Button>
+ <Button
+ onClick={() =>
+ void router.push(
+ `/auth/sign-in?targetPath=${encodeURIComponent(newTargetPath)}`,
+ )
+ }
+ >
+ Sign In
+ </Button>
) : null}
</div>
);
diff --git a/web/src/components/layouts/layout.tsx b/web/src/components/layouts/layout.tsx
index e734196e56..5f9adee858 100644
--- a/web/src/components/layouts/layout.tsx
+++ b/web/src/components/layouts/layout.tsx
@@ -147,7 +147,14 @@ export default function Layout(props: PropsWithChildren) {
!publishablePaths.includes(router.pathname) &&
!router.pathname.startsWith("/public/")
) {
- void router.replace("/auth/sign-in");
+ const newTargetPath = router.asPath;
+ if (newTargetPath && newTargetPath !== "/") {
+ void router.replace(
+ `/auth/sign-in?targetPath=${encodeURIComponent(newTargetPath)}`,
+ );
+ } else {
+ void router.replace(`/auth/sign-in`);
+ }
return <Spinner message="Redirecting" />;
}
@@ -155,7 +162,8 @@ export default function Layout(props: PropsWithChildren) {
session.status === "authenticated" &&
unauthenticatedPaths.includes(router.pathname)
) {
- void router.replace("/");
+ const targetPath = router.query.targetPath as string | undefined;
+ void router.replace(targetPath ?? "/");
return <Spinner message="Redirecting" />;
}