auth.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. export default function (context) {
  2. const {app, route, store, req, res, redirect, next} = context;
  3. let isClient = process.client;
  4. let isServer = process.server;
  5. let token, path;
  6. token = store.state.token;
  7. // console.log('token', token);
  8. // console.log("cookies", cook);
  9. //在服务端
  10. if (isServer) {
  11. path = req.originalUrl;
  12. }
  13. //在客户端判读是否需要登陆
  14. if (isClient) {
  15. path = route.path;
  16. }
  17. // console.log("path:"+path);
  18. let mainPath = path.split("?")[0];
  19. // console.log('mainPath', mainPath);
  20. if(mainPath === "/setToken"){
  21. try{
  22. let origin_path = app.$cookies.get('origin_path');
  23. if(origin_path){
  24. app.$cookies.remove('origin_path');
  25. redirect(origin_path);
  26. }else{
  27. redirect('/');
  28. }
  29. }catch (e) {
  30. console.log("e: ", e);
  31. redirect('/error');
  32. }
  33. }
  34. else if (!token && mainPath !== "/" && mainPath !=="/login" && mainPath !== "/register" && mainPath !=="/error" && mainPath !=="/resetPassword" && mainPath !=="/unauthorized" && mainPath !== "/api/receiveIDToken") {
  35. // console.log("session:", req.session);
  36. app.$cookies.set('origin_path', path);
  37. // req.session.origin_path = path;
  38. redirect('/api/authenticate');
  39. }
  40. else if(store.state.user.role !== 'ADMIN' && mainPath.indexOf('/admin') >= 0){
  41. redirect('/unauthorized');
  42. }
  43. }