Vue router - beforeEach block causing 401?

生来就可爱ヽ(ⅴ<●) 提交于 2020-06-09 05:23:21

问题


I'm working on a Vue app and have this beforeEach block that remembers a user's entry URL if they're not logged and redirects them to it after they log in. It works but...

Because I'm a hack, and because I've also been getting some JWT post 401 errors recently, I'm wondering if this block might be poorly constructed and causing me problems(?)

router.beforeEach((to, from, next) => {
  let token = JSON.parse(localStorage.getItem("userData"));
  token = token ? token.token : "";
  if (to.fullPath !== "/login") {
    if (!token) {
      entryUrl = to.fullPath;
      next("/login");
    } else {
      if (entryUrl) {
        const url = entryUrl;
        entryUrl = null;
        next(url);
      }
      next(false);
    }
  } else {
    if (token) {
      next("/");
    }
  }
  next();
});

And the interceptor request success before THE response 401:

interceptor request success= 
{url: "https://panel.site.art/wp-json/jwt-auth/v1/site/transfer", method: "post", data: {…}, headers: {…}, baseURL: "https://panel.site.art/wp-json", …}
adapter: ƒ (t)
baseURL: "https://panel.site.art/wp-json"
data: "{"location_id":"rec140ttKVWJCDr8v","items":["recg1W9lQuLLRm8VS"]}"
headers:
Accept: "application/json, text/plain, */*"
Content-Type: "application/json;charset=utf-8"
__proto__: Object
maxContentLength: -1
method: "post"
timeout: 0
transformRequest: [ƒ]
transformResponse: [ƒ]
url: "https://panel.site.art/wp-json/jwt-auth/v1/site/transfer"
validateStatus: ƒ (t)
xsrfCookieName: "XSRF-TOKEN"
xsrfHeaderName: "X-XSRF-TOKEN"
__proto__: Object

来源:https://stackoverflow.com/questions/62112146/vue-router-beforeeach-block-causing-401

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!