update: [isLogin] authGuard
| | |
| | | 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', 'faq', 'consultantLogin', 'agentInfo-agentNo']; |
| | | 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) { |
| | | if (!(noNeedLoginFunctionList.some((routeName) => routeName === currentRouteName))) { |
| | | const routeNeedLogin = !(noNeedLoginFunctionList.some((routeName) => routeName === currentRouteName)); |
| | | if (routeNeedLogin) { |
| | | context.redirect('/'); |
| | | } |
| | | } else if(isUserLogin) { |
| | | const userFunctions = ['accountSetting', 'userReviewsRecord', 'myConsultantList-consultantList', 'questionnaire-agentNo', ...noNeedLoginFunctionList]; |
| | | if (!(userFunctions.some((routeName) => routeName === currentRouteName))) { |
| | | const userFunctions = ['notification', 'satisfactionList', 'accountSetting', 'userReviews', 'userReviewsRecord', ...noNeedLoginFunctionList]; |
| | | const routeNeedUserLogin = !(userFunctions.some((routeName) => routeName === currentRouteName)); |
| | | if (routeNeedUserLogin) { |
| | | context.redirect('/'); |
| | | } |
| | | } else if(isAdminLogin) { |
| | | const agentFunctions = ['agentInfo-agentNo', 'agentInfo-edit-agentNo', 'record', 'myAppointmentList-appointmentList', 'myAppointmentList-contactedList', 'myAppointmentList-closedList', 'appointment-appointmentId', 'appointment-appointmentId-close',...noNeedLoginFunctionList ]; |
| | | if (!(agentFunctions.some((routeName) => routeName === currentRouteName))) { |
| | | const agentFunctions = ['notification', 'agentInfo-agentNo', 'agentInfo-edit-agentNo', 'record', 'myAppointmentList-appointmentList', 'myAppointmentList-contactedList', 'myAppointmentList-closedList', 'appointment-appointmentId', 'appointment-appointmentId-close', ...noNeedLoginFunctionList]; |
| | | const routeNeedAgentLogin = !(agentFunctions.some((routeName) => routeName === currentRouteName)); |
| | | if (routeNeedAgentLogin) { |
| | | context.redirect('/'); |
| | | } |
| | | } |
| | |
| | | router: { |
| | | base: process.env.ENV === 'dev' ? '' : '/pam/', |
| | | mode: 'hash', |
| | | middleware: [ 'getUrlQuery', 'errorRoute'] |
| | | middleware: [ 'isLogin', 'getUrlQuery', 'errorRoute'] |
| | | } |
| | | } |