保誠-保戶業務員媒合平台
Tomas
2022-08-24 c188a62c55b4eb2f8b6c5178be7bff58b7ba75a1
refactor: isLoginAuthGrad
修改1個檔案
26 ■■■■■ 已變更過的檔案
PAMapp/middleware/isLogin.ts 26 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
PAMapp/middleware/isLogin.ts
@@ -3,6 +3,7 @@
const isLogin: Middleware = (context) => {
    const isUserLogin = context.store.getters['localStorage/isUserLogin'];
    const isAdminLogin = context.store.getters['localStorage/isAdminLogin'];
    const isGuest = !isUserLogin && !isAdminLogin;
    const currentRouteName = context.route.name;
    const noNeedLoginFunctionList = [
        'index', 'login',
@@ -13,21 +14,28 @@
        'questionnaire-agentNo' // NOTE: 這裡會加入不需 authGuard 的原因是,此 route 有自己的機制來導頁到 login。
    ];
    if (!isUserLogin && !isAdminLogin) {
        const routeNeedLogin = !(noNeedLoginFunctionList.some((routeName) => routeName === currentRouteName));
        if (routeNeedLogin) {
    if (isGuest) {
        const guestCannotAccess = !(noNeedLoginFunctionList.includes(currentRouteName!));
        if (guestCannotAccess) {
            context.redirect('/');
        } 
    } else if(isUserLogin) {
        const userFunctions = ['notification', 'satisfactionList', 'accountSetting', 'userReviews', 'userReviewsRecord', ...noNeedLoginFunctionList];
        const routeNeedUserLogin = !(userFunctions.some((routeName) => routeName === currentRouteName));
        if (routeNeedUserLogin) {
        const userFunctions = [
            'notification', 'satisfactionList', 'accountSetting',
            'userReviews', 'userReviewsRecord',
            ...noNeedLoginFunctionList];
        const userCannotAccess = !(userFunctions.includes(currentRouteName!));
        if (userCannotAccess) {
            context.redirect('/');
        }
    } else if(isAdminLogin) {
        const agentFunctions = ['notification', 'agentInfo-agentNo', 'agentInfo-edit-agentNo', 'record', 'myAppointmentList-appointmentList', 'myAppointmentList-contactedList', 'myAppointmentList-closedList', 'appointment-appointmentId', 'appointment-appointmentId-close', 'appointment-appointmentId-interview-new', ...noNeedLoginFunctionList];
        const routeNeedAgentLogin = !(agentFunctions.some((routeName) => routeName === currentRouteName));
        if (routeNeedAgentLogin) {
        const agentFunctions = [
            'notification', 'agentInfo-agentNo', 'agentInfo-edit-agentNo', 'record',
            'myAppointmentList-appointmentList', 'myAppointmentList-contactedList', 'myAppointmentList-closedList',
            'appointment-appointmentId', 'appointment-appointmentId-close', 'appointment-appointmentId-interview-new',
            ...noNeedLoginFunctionList];
        const agentCannotAccess = !(agentFunctions.includes(currentRouteName!));
        if (agentCannotAccess) {
            context.redirect('/');
        }
    }