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. // 在服务端
  10. if (isServer) {
  11. // 获取服务端cookie
  12. let cookies = getCookie.getcookiesInServer(req)
  13. // 获取当前服务端cookie中是否含有token字段
  14. token = cookies.token ? cookies.token : ''
  15. // open_platform = cookies.open_platform ? cookies.open_platform : ''
  16. }
  17. // 在客户端
  18. if (isClient) {
  19. // 获取客户端(本地)cookie中的token字段
  20. token = getCookie.getcookiesInClient('shop-token')
  21. // open_platform = getCookie.getcookiesInClient('open_platform')
  22. }
  23. // 判断是否获取到token
  24. // 未获取到,重定向到home頁
  25. if (!token) {
  26. Message({
  27. message: 'Please login first!',
  28. type: 'error',
  29. duration: 3 * 1000
  30. });
  31. redirect(redirectURL)
  32. }
  33. }