保誠-保戶業務員媒合平台
Tomas
2022-08-24 3b7c219a10fe0d3d4c9182cbcbc5cf05eb426f97
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Middleware } from '@nuxt/types';
 
const isLogin: Middleware = (context) => {
    const isUserLogin = context.store.getters['localStorage/isUserLogin'];
    const isAdminLogin = context.store.getters['localStorage/isAdminLogin'];
    const currentRouteName = context.route.name;
    const noNeedLoginFunctionList = [
        'index', 'login',
        'myConsultantList-consultantList', 'myConsultantList-contactedList',
        'faq', 'consultantLogin', 'agentInfo-agentNo',
        'recommendConsultant', 'recommendConsultant-result',
        'quickFilter',
        'questionnaire-agentNo' // NOTE: 這裡會加入不需 authGuard 的原因是,此 route 有自己的機制來導頁到 login。
    ];
 
    if (!isUserLogin && !isAdminLogin) {
        const routeNeedLogin = !(noNeedLoginFunctionList.some((routeName) => routeName === currentRouteName));
        if (routeNeedLogin) {
            context.redirect('/');
        } 
    } else if(isUserLogin) {
        const userFunctions = ['notification', 'satisfactionList', 'accountSetting', 'userReviews', 'userReviewsRecord', ...noNeedLoginFunctionList];
        const routeNeedUserLogin = !(userFunctions.some((routeName) => routeName === currentRouteName));
        if (routeNeedUserLogin) {
            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) {
            context.redirect('/');
        }
    }
 
}
 
export default isLogin