问题
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