bugfix(router): redirect to /workspaces if user is authenticated, otherwise redirect to /auth route + rename /login -> /auth

This commit is contained in:
2026-03-13 02:47:36 +08:00
parent 7e378a92f8
commit 3948505a30
5 changed files with 13 additions and 8 deletions

View File

@@ -23,6 +23,11 @@ const MainLayout = () => {
) )
} }
const RootRedirect = () => {
const isAuthenticated = !!localStorage.getItem("accessToken")
return isAuthenticated ? <Navigate to="/workspaces" replace /> : <Navigate to="/auth" replace />
}
function App() { function App() {
return ( return (
<ThemeProvider> <ThemeProvider>
@@ -30,8 +35,8 @@ function App() {
<Router> <Router>
<WorkspaceProvider> <WorkspaceProvider>
<Routes> <Routes>
<Route path="/" element={<Navigate to="/login" replace />} /> <Route path="/" element={<RootRedirect />} />
<Route path="/login" element={<Auth />} /> <Route path="/auth" element={<Auth />} />
<Route path="/terms" element={<Terms />} /> <Route path="/terms" element={<Terms />} />
<Route element={<MainLayout />}> <Route element={<MainLayout />}>

View File

@@ -23,7 +23,7 @@ export const authFetch = async (endpoint: string, options: RequestInit = {}) =>
if (response.status === 401) { if (response.status === 401) {
localStorage.removeItem("accessToken"); localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken"); localStorage.removeItem("refreshToken");
window.location.href = "/login"; window.location.href = "/auth";
} }
return response; return response;

View File

@@ -3,7 +3,7 @@ import { authFetch } from './client';
// --- Auth Endpoints --- // --- Auth Endpoints ---
export const loginWithPassword = async (mobile: string, password: string) => { export const loginWithPassword = async (mobile: string, password: string) => {
const response = await authFetch('/api/users/login/', { const response = await authFetch('/api/users/auth/', {
method: 'POST', method: 'POST',
body: JSON.stringify({ mobile, password }) body: JSON.stringify({ mobile, password })
}); });
@@ -21,7 +21,7 @@ export const sendOtp = async (mobile: string, mode: string) => {
}; };
export const loginWithOtp = async (mobile: string, otp: string) => { export const loginWithOtp = async (mobile: string, otp: string) => {
const response = await authFetch('/api/users/otp/login/', { const response = await authFetch('/api/users/otp/auth/', {
method: 'POST', method: 'POST',
body: JSON.stringify({ mobile, otp }) body: JSON.stringify({ mobile, otp })
}); });

View File

@@ -71,7 +71,7 @@ export function Navbar() {
setUser(null) setUser(null)
setShowLogoutModal(false) setShowLogoutModal(false)
toast.success(t.logoutToast || "Successfully logged out!") toast.success(t.logoutToast || "Successfully logged out!")
navigate("/login") navigate("/auth")
} }
} }
@@ -167,7 +167,7 @@ export function Navbar() {
<> <>
<SettingsMenu /> <SettingsMenu />
<Button <Button
onClick={() => navigate("/login")} onClick={() => navigate("/auth")}
className="bg-blue-600 text-white hover:bg-blue-700 dark:bg-blue-600 dark:hover:bg-blue-700" className="bg-blue-600 text-white hover:bg-blue-700 dark:bg-blue-600 dark:hover:bg-blue-700"
> >
{t.login?.signIn || "Login"} {t.login?.signIn || "Login"}

View File

@@ -70,7 +70,7 @@ export default function Profile() {
const data = await getUserProfile() const data = await getUserProfile()
setUser(data) setUser(data)
} catch (error) { } catch (error) {
navigate("/login") navigate("/auth")
} finally { } finally {
setIsLoading(false) setIsLoading(false)
} }