관리자 기본 진입 경로 수정
This commit is contained in:
parent
778e8526d4
commit
bd046c4501
6 changed files with 62 additions and 25 deletions
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
import type { ReactNode } from "react";
|
||||
import { BrowserRouter, Navigate, Route, Routes, useLocation } from "react-router-dom";
|
||||
import { AuthProvider, canAccessRole, useAuth, roleHomePath, type AuthUser, type Role } from "./lib/auth";
|
||||
import { AuthProvider, canAccessRole, initialPathForUser, useAuth, roleHomePath, type AuthUser, type Role } from "./lib/auth";
|
||||
|
||||
import Login from "./pages/Login";
|
||||
import Onboarding from "./pages/Onboarding";
|
||||
|
|
@ -79,10 +79,7 @@ function isAdminWorkspacePath(path: string) {
|
|||
}
|
||||
|
||||
function approvedHomePath(user: AuthUser) {
|
||||
if (user.onboardingCompletedAt == null) {
|
||||
return canAccessRole(user, "admin") ? "/admin" : "/onboarding";
|
||||
}
|
||||
return roleHomePath(user.role);
|
||||
return initialPathForUser(user);
|
||||
}
|
||||
|
||||
function OnboardingGate({ children }: { children: ReactNode }) {
|
||||
|
|
@ -99,7 +96,7 @@ function OnboardingGate({ children }: { children: ReactNode }) {
|
|||
return <Navigate to="/onboarding" replace state={{ from: path }} />;
|
||||
}
|
||||
if (user && user.onboardingCompletedAt != null && (path === "/login" || path === "/onboarding")) {
|
||||
return <Navigate to={roleHomePath(user.role)} replace />;
|
||||
return <Navigate to={initialPathForUser(user)} replace />;
|
||||
}
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
|
@ -129,7 +126,7 @@ function RootRedirect() {
|
|||
if (user && user.onboardingCompletedAt == null) {
|
||||
return <Navigate to={approvedHomePath(user)} replace />;
|
||||
}
|
||||
return <Navigate to={user ? roleHomePath(user.role) : "/login"} replace />;
|
||||
return <Navigate to={user ? initialPathForUser(user) : "/login"} replace />;
|
||||
}
|
||||
|
||||
function AppRoutes() {
|
||||
|
|
|
|||
|
|
@ -74,6 +74,14 @@ export function canAccessRole(user: AuthUser, role: Role): boolean {
|
|||
return role === "admin" && user.adminAccess;
|
||||
}
|
||||
|
||||
export function initialPathForUser(user: AuthUser): string {
|
||||
if (user.accountStatus !== "approved") return "/pending";
|
||||
if (user.onboardingCompletedAt == null) {
|
||||
return canAccessRole(user, "admin") ? "/admin" : "/onboarding";
|
||||
}
|
||||
return canAccessRole(user, "admin") ? "/admin" : roleHomePath(user.role);
|
||||
}
|
||||
|
||||
export function accessibleRolesFor(user: AuthUser): Role[] {
|
||||
if (user.superAdmin || user.role === "admin") return ["learner", "teacher", "admin"];
|
||||
if (user.adminAccess) return ["admin"];
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { useLocation, useNavigate } from "react-router-dom";
|
||||
import { roleHomePath, useAuth, type Role } from "../lib/auth";
|
||||
import { initialPathForUser, useAuth, type Role } from "../lib/auth";
|
||||
import { apiUrl, authApi, type AuthConfigResponse } from "../lib/api";
|
||||
import { LoginBrand } from "./login/LoginBrand";
|
||||
import { LoginPanel, type LoginRoleOption } from "./login/LoginPanel";
|
||||
|
|
@ -172,14 +172,7 @@ export default function Login() {
|
|||
setLoginErrorReason(null);
|
||||
try {
|
||||
const signedIn = await login(role);
|
||||
navigate(
|
||||
signedIn.accountStatus !== "approved"
|
||||
? "/pending"
|
||||
: signedIn.onboardingCompletedAt == null
|
||||
? "/onboarding"
|
||||
: roleHomePath(signedIn.role),
|
||||
{ replace: true },
|
||||
);
|
||||
navigate(initialPathForUser(signedIn), { replace: true });
|
||||
} catch (err) {
|
||||
setLoginError(err instanceof Error ? err.message : "로그인에 실패했습니다.");
|
||||
} finally {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue