| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- export default function (context) {
- const {app, route, store, req, res, redirect, next} = context;
- let isClient = process.client;
- let isServer = process.server;
- let token, path;
- token = store.state.token;
- // console.log('token', token);
- // console.log("cookies", cook);
- //在服务端
- if (isServer) {
- path = req.originalUrl;
- }
- //在客户端判读是否需要登陆
- if (isClient) {
- path = route.path;
- }
- // console.log("path:"+path);
- let mainPath = path.split("?")[0];
- // console.log('mainPath', mainPath);
- if(mainPath === "/setToken"){
- try{
- let origin_path = app.$cookies.get('origin_path');
- if(origin_path){
- app.$cookies.remove('origin_path');
- redirect(origin_path);
- }else{
- redirect('/');
- }
- }catch (e) {
- console.log("e: ", e);
- redirect('/error');
- }
- }
- else if (!token && mainPath !== "/" && mainPath !=="/login" && mainPath !== "/register" && mainPath !=="/error" && mainPath !=="/resetPassword" && mainPath !=="/unauthorized" && mainPath !== "/api/receiveIDToken") {
- // console.log("session:", req.session);
- app.$cookies.set('origin_path', path);
- // req.session.origin_path = path;
- redirect('/api/authenticate');
- }
- else if(store.state.user.role !== 'ADMIN' && mainPath.indexOf('/admin') >= 0){
- redirect('/unauthorized');
- }
- }
|