Skip to content

Commit 1ab66ea

Browse files
authored
fix(frontend): login/signup pages away redirects (#11430)
## Changes ๐Ÿ—๏ธ When the user is logged in and tries to navigate to `/login` or `/signup` manually, redirect them away to `/marketplace`. ### Checklist ๐Ÿ“‹ #### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: - [x] Login - [x] Go to `/login` or `/signup` - [x] You are redirected back into `/marketplace`
1 parent 126d583 commit 1ab66ea

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

โ€Žautogpt_platform/frontend/src/app/(platform)/login/useLoginPage.tsโ€Ž

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@ import { environment } from "@/services/environment";
44
import { loginFormSchema, LoginProvider } from "@/types/auth";
55
import { zodResolver } from "@hookform/resolvers/zod";
66
import { useRouter } from "next/navigation";
7-
import { useState } from "react";
7+
import { useEffect, useState } from "react";
88
import { useForm } from "react-hook-form";
99
import z from "zod";
1010
import { login as loginAction } from "./actions";
1111

1212
export function useLoginPage() {
13-
const { supabase, user, isUserLoading } = useSupabase();
13+
const { supabase, user, isUserLoading, isLoggedIn } = useSupabase();
1414
const [feedback, setFeedback] = useState<string | null>(null);
1515
const router = useRouter();
1616
const { toast } = useToast();
1717
const [isLoading, setIsLoading] = useState(false);
18+
const [isLoggingIn, setIsLoggingIn] = useState(false);
1819
const [isGoogleLoading, setIsGoogleLoading] = useState(false);
1920
const [showNotAllowedModal, setShowNotAllowedModal] = useState(false);
2021
const isCloudEnv = environment.isCloud();
2122

23+
useEffect(() => {
24+
if (isLoggedIn && !isLoggingIn) {
25+
router.push("/marketplace");
26+
}
27+
}, [isLoggedIn, isLoggingIn]);
28+
2229
const form = useForm<z.infer<typeof loginFormSchema>>({
2330
resolver: zodResolver(loginFormSchema),
2431
defaultValues: {
@@ -29,6 +36,7 @@ export function useLoginPage() {
2936

3037
async function handleProviderLogin(provider: LoginProvider) {
3138
setIsGoogleLoading(true);
39+
setIsLoggingIn(true);
3240

3341
try {
3442
const response = await fetch("/api/auth/provider", {
@@ -46,25 +54,27 @@ export function useLoginPage() {
4654
if (url) window.location.href = url as string;
4755
} catch (error) {
4856
setIsGoogleLoading(false);
57+
setIsLoggingIn(false);
4958
setFeedback(
5059
error instanceof Error ? error.message : "Failed to start OAuth flow",
5160
);
5261
}
5362
}
5463

5564
async function handleLogin(data: z.infer<typeof loginFormSchema>) {
56-
setIsLoading(true);
57-
5865
if (data.email.includes("@agpt.co")) {
5966
toast({
6067
title: "Please use Google SSO to login using an AutoGPT email.",
6168
variant: "default",
6269
});
6370

6471
setIsLoading(false);
72+
setIsLoggingIn(false);
6573
return;
6674
}
6775

76+
setIsLoggingIn(true);
77+
6878
try {
6979
const result = await loginAction(data.email, data.password);
7080

@@ -86,6 +96,7 @@ export function useLoginPage() {
8696
variant: "destructive",
8797
});
8898
setIsLoading(false);
99+
setIsLoggingIn(false);
89100
}
90101
}
91102

โ€Žautogpt_platform/frontend/src/app/(platform)/signup/useSignupPage.tsโ€Ž

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@ import { environment } from "@/services/environment";
44
import { LoginProvider, signupFormSchema } from "@/types/auth";
55
import { zodResolver } from "@hookform/resolvers/zod";
66
import { useRouter } from "next/navigation";
7-
import { useState } from "react";
7+
import { useEffect, useState } from "react";
88
import { useForm } from "react-hook-form";
99
import z from "zod";
1010
import { signup as signupAction } from "./actions";
1111

1212
export function useSignupPage() {
13-
const { supabase, user, isUserLoading } = useSupabase();
13+
const { supabase, user, isUserLoading, isLoggedIn } = useSupabase();
1414
const [feedback, setFeedback] = useState<string | null>(null);
1515
const { toast } = useToast();
1616
const router = useRouter();
1717
const [isLoading, setIsLoading] = useState(false);
18+
const [isSigningUp, setIsSigningUp] = useState(false);
1819
const [isGoogleLoading, setIsGoogleLoading] = useState(false);
1920
const [showNotAllowedModal, setShowNotAllowedModal] = useState(false);
2021
const isCloudEnv = environment.isCloud();
2122

23+
useEffect(() => {
24+
if (isLoggedIn && !isSigningUp) {
25+
router.push("/marketplace");
26+
}
27+
}, [isLoggedIn, isSigningUp]);
28+
2229
const form = useForm<z.infer<typeof signupFormSchema>>({
2330
resolver: zodResolver(signupFormSchema),
2431
defaultValues: {
@@ -31,6 +38,7 @@ export function useSignupPage() {
3138

3239
async function handleProviderSignup(provider: LoginProvider) {
3340
setIsGoogleLoading(true);
41+
setIsSigningUp(true);
3442

3543
try {
3644
const response = await fetch("/api/auth/provider", {
@@ -44,6 +52,7 @@ export function useSignupPage() {
4452

4553
if (error === "not_allowed") {
4654
setShowNotAllowedModal(true);
55+
setIsSigningUp(false);
4756
return;
4857
}
4958

@@ -54,6 +63,7 @@ export function useSignupPage() {
5463
if (url) window.location.href = url as string;
5564
} catch (error) {
5665
setIsGoogleLoading(false);
66+
setIsSigningUp(false);
5767
toast({
5868
title:
5969
error instanceof Error ? error.message : "Failed to start OAuth flow",
@@ -76,6 +86,8 @@ export function useSignupPage() {
7686
return;
7787
}
7888

89+
setIsSigningUp(true);
90+
7991
try {
8092
const result = await signupAction(
8193
data.email,
@@ -89,24 +101,28 @@ export function useSignupPage() {
89101
if (!result.success) {
90102
if (result.error === "user_already_exists") {
91103
setFeedback("User with this email already exists");
104+
setIsSigningUp(false);
92105
return;
93106
}
94107
if (result.error === "not_allowed") {
95108
setShowNotAllowedModal(true);
109+
setIsSigningUp(false);
96110
return;
97111
}
98112

99113
toast({
100114
title: result.error || "Signup failed",
101115
variant: "destructive",
102116
});
117+
setIsSigningUp(false);
103118
return;
104119
}
105120

106121
const next = result.next || "/";
107122
if (next) router.replace(next);
108123
} catch (error) {
109124
setIsLoading(false);
125+
setIsSigningUp(false);
110126
toast({
111127
title:
112128
error instanceof Error

โ€Žautogpt_platform/frontend/src/tests/signin.spec.tsโ€Ž

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// auth.spec.ts
22

33
import test from "@playwright/test";
4-
import { getTestUser } from "./utils/auth";
4+
import { BuildPage } from "./pages/build.page";
55
import { LoginPage } from "./pages/login.page";
66
import { hasUrl, isHidden, isVisible } from "./utils/assertion";
7+
import { getTestUser } from "./utils/auth";
78
import { getSelectors } from "./utils/selectors";
8-
import { BuildPage } from "./pages/build.page";
99

1010
test.beforeEach(async ({ page }) => {
1111
await page.goto("/login");
@@ -171,3 +171,29 @@ test("multi-tab logout with WebSocket cleanup", async ({ context }) => {
171171
await page1.close();
172172
await page2.close();
173173
});
174+
175+
test("logged in user is redirected from /login to /marketplace", async ({
176+
page,
177+
}) => {
178+
const testUser = await getTestUser();
179+
const loginPage = new LoginPage(page);
180+
181+
await loginPage.login(testUser.email, testUser.password);
182+
await hasUrl(page, "/marketplace");
183+
184+
await page.goto("/login");
185+
await hasUrl(page, "/marketplace");
186+
});
187+
188+
test("logged in user is redirected from /signup to /marketplace", async ({
189+
page,
190+
}) => {
191+
const testUser = await getTestUser();
192+
const loginPage = new LoginPage(page);
193+
194+
await loginPage.login(testUser.email, testUser.password);
195+
await hasUrl(page, "/marketplace");
196+
197+
await page.goto("/signup");
198+
await hasUrl(page, "/marketplace");
199+
});

0 commit comments

Comments
ย (0)