auth.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import getCookie from '~/utils/getCookie'
  2. import { Message } from "element-ui";
  3. export default function ({store,route, req, res, redirect}) {
  4. let isClient = process.client;
  5. let isServer = process.server;
  6. let redirectURL = '/';
  7. var token;
  8. // var open_platform;
  9. console.log(req, 'req')
  10. // 在服务端
  11. if (isServer) {
  12. // 获取服务端cookie
  13. let cookies = getCookie.getcookiesInServer(req)
  14. // 获取当前服务端cookie中是否含有token字段
  15. token = cookies.token ? cookies.token : ''
  16. // open_platform = cookies.open_platform ? cookies.open_platform : ''
  17. }
  18. // 在客户端
  19. if (isClient) {
  20. // 获取客户端(本地)cookie中的token字段
  21. token = getCookie.getcookiesInClient('token')
  22. // open_platform = getCookie.getcookiesInClient('open_platform')
  23. }
  24. // 判断是否获取到token
  25. // 未获取到,重定向到home頁
  26. if (!token) {
  27. Message({
  28. message: 'Please login first!',
  29. type: 'error',
  30. duration: 3 * 1000
  31. });
  32. redirect(redirectURL)
  33. }
  34. }