feat: redirect home to dashboard untuk user yang sudah login
- Update authMiddleware: dashboard untuk semua user, admin hanya untuk admin - Tambah redirect otomatis dari / ke /dashboard jika sudah login - User biasa sekarang bisa akses halaman dashboard - Admin panel tetap terbatas untuk role admin saja Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -68,8 +68,18 @@ const routeRules: RouteRule[] = [
|
|||||||
{
|
{
|
||||||
match: (p) => p === "/dashboard" || p.startsWith("/dashboard/"),
|
match: (p) => p === "/dashboard" || p.startsWith("/dashboard/"),
|
||||||
requireAuth: true,
|
requireAuth: true,
|
||||||
|
redirectTo: "/signin",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
match: (p) => p === "/admin" || p.startsWith("/admin/"),
|
||||||
|
requireAuth: true,
|
||||||
requiredRole: "admin",
|
requiredRole: "admin",
|
||||||
redirectTo: "/profile",
|
redirectTo: "/dashboard",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
match: (p) => p === "/" || p === "",
|
||||||
|
requireAuth: false,
|
||||||
|
redirectTo: "/dashboard",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -32,11 +32,29 @@ import {
|
|||||||
IconStack2,
|
IconStack2,
|
||||||
IconSun,
|
IconSun,
|
||||||
} from "@tabler/icons-react";
|
} from "@tabler/icons-react";
|
||||||
import { createFileRoute, Link } from "@tanstack/react-router";
|
import { createFileRoute, Link, useNavigate } from "@tanstack/react-router";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { useSnapshot } from "valtio";
|
||||||
|
import { authStore } from "@/store/auth";
|
||||||
|
|
||||||
export const Route = createFileRoute("/")({
|
export const Route = createFileRoute("/")({
|
||||||
component: HomePage,
|
component: HomePage,
|
||||||
|
beforeLoad: async () => {
|
||||||
|
// Check if user is already logged in
|
||||||
|
const session = await fetch("/api/session", {
|
||||||
|
method: "GET",
|
||||||
|
credentials: "include",
|
||||||
|
}).then((res) => (res.ok ? res.json() : null));
|
||||||
|
|
||||||
|
// If user is logged in, redirect to dashboard
|
||||||
|
if (session?.data?.user) {
|
||||||
|
throw {
|
||||||
|
redirect: {
|
||||||
|
to: "/dashboard",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Navigation items
|
// Navigation items
|
||||||
|
|||||||
Reference in New Issue
Block a user