mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
fix(login/onboarding): redirect to dashboard if already logged in
This commit is contained in:
@@ -29,7 +29,7 @@ export default function Layout({ loaderData }: Route.ComponentProps) {
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(error);
|
||||
toast.error("Logout failed");
|
||||
toast.error("Logout failed", { description: error.message });
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -2,10 +2,12 @@ import { redirect, type MiddlewareFunction } from "react-router";
|
||||
import { getMe, getStatus } from "~/api-client";
|
||||
import { appContext } from "~/context";
|
||||
|
||||
export const authMiddleware: MiddlewareFunction = async ({ context }) => {
|
||||
export const authMiddleware: MiddlewareFunction = async ({ context, request }) => {
|
||||
const session = await getMe();
|
||||
|
||||
if (!session.data?.user.id) {
|
||||
const isAuthRoute = ["/login", "/onboarding"].includes(new URL(request.url).pathname);
|
||||
|
||||
if (!session.data?.user.id && !isAuthRoute) {
|
||||
const status = await getStatus();
|
||||
if (!status.data?.hasUsers) {
|
||||
throw redirect("/onboarding");
|
||||
@@ -14,5 +16,11 @@ export const authMiddleware: MiddlewareFunction = async ({ context }) => {
|
||||
throw redirect("/login");
|
||||
}
|
||||
|
||||
context.set(appContext, { user: session.data.user, hasUsers: true });
|
||||
if (session.data?.user.id) {
|
||||
context.set(appContext, { user: session.data.user, hasUsers: true });
|
||||
|
||||
if (isAuthRoute) {
|
||||
throw redirect("/");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,6 +9,9 @@ import { AuthLayout } from "~/components/auth-layout";
|
||||
import { Button } from "~/components/ui/button";
|
||||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "~/components/ui/form";
|
||||
import { Input } from "~/components/ui/input";
|
||||
import { authMiddleware } from "~/middleware/auth";
|
||||
|
||||
export const clientMiddleware = [authMiddleware];
|
||||
|
||||
const loginSchema = type({
|
||||
username: "2<=string<=50",
|
||||
@@ -35,7 +38,7 @@ export default function LoginPage() {
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(error);
|
||||
toast.error("Login failed");
|
||||
toast.error("Login failed", { description: error.message });
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@ import { AuthLayout } from "~/components/auth-layout";
|
||||
import { Button } from "~/components/ui/button";
|
||||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "~/components/ui/form";
|
||||
import { Input } from "~/components/ui/input";
|
||||
import { authMiddleware } from "~/middleware/auth";
|
||||
|
||||
export const clientMiddleware = [authMiddleware];
|
||||
|
||||
const onboardingSchema = type({
|
||||
username: "2<=string<=50",
|
||||
@@ -38,7 +41,7 @@ export default function OnboardingPage() {
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(error);
|
||||
toast.error("Failed to create admin user");
|
||||
toast.error("Failed to create admin user", { description: error.message });
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user