Skip to content

Commit

Permalink
added a route page not found
Browse files Browse the repository at this point in the history
  • Loading branch information
varsha766 committed Jun 15, 2022
1 parent 0b3e0b7 commit af26472
Showing 1 changed file with 64 additions and 55 deletions.
119 changes: 64 additions & 55 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,71 +144,80 @@ const router = new Router({
});

router.beforeEach((to, from, next) => {
if (to.meta.title) {
document.title = to.meta.title;
if(to.matched.length < 1){
next(false);
router.push('/404');
}

if (to.matched.some((record) => record.meta.requiresAuth)) {
const authToken = localStorage.getItem("authToken");

if (authToken) {
// console.log("Yes auth token");
const url = `${config.studioServer.BASE_URL}hs/api/v2/auth/protected`;
fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "POST",
})
.then((res) => res.json())
.then((json) => {
if (json.status == 403) {
next({
path: to.meta.admin ? "/admin/login" : "/login",
params: { nextUrl: to.fullPath },
});
} else {
localStorage.setItem("user", JSON.stringify(json.message));
Vue.prototype.$accounts = json.accounts;
if (json.accounts.length==0) {

localStorage.removeItem("accessToken")
localStorage.removeItem("accessuser")
}
if (
to.meta.admin &&
!json.message.isSubscribed &&
(to.path != "/admin/subscription" && to.path != "/admin/dashboard")
) {
eventBus.$emit("UpdateAdminNav", false);
else{
if (to.meta.title) {
document.title = to.meta.title;
}

if (to.matched.some((record) => record.meta.requiresAuth)) {
const authToken = localStorage.getItem("authToken");

if (authToken) {
// console.log("Yes auth token");
const url = `${config.studioServer.BASE_URL}hs/api/v2/auth/protected`;
fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
},
method: "POST",
})
.then((res) => res.json())
.then((json) => {
if (json.status == 403) {
next({
path: "/admin/subscription",
path: to.meta.admin ? "/admin/login" : "/login",
params: { nextUrl: to.fullPath },
});

} else {
next();
localStorage.setItem("user", JSON.stringify(json.message));
Vue.prototype.$accounts = json.accounts;
if (json.accounts.length==0) {

localStorage.removeItem("accessToken")
localStorage.removeItem("accessuser")
}
if (
to.meta.admin &&
!json.message.isSubscribed &&
(to.path != "/admin/subscription" && to.path != "/admin/dashboard")
) {
eventBus.$emit("UpdateAdminNav", false);
next({
path: "/admin/subscription",
params: { nextUrl: to.fullPath },
});
} else {
next();
}
}
}
})
.catch((e) => {
console.log(e);
next({
path: to.meta.admin ? "/admin/login" : "/login",
params: { nextUrl: to.fullPath },
})
.catch((e) => {
console.log(e);
next({
path: to.meta.admin ? "/admin/login" : "/login",
params: { nextUrl: to.fullPath },
});
});
} else {
next({
path: to.meta.admin
? "/admin/login"
: !to.query["referrer"]
? `/login/${to.params["slug"]}`
: `/login/${to.params["slug"]}?referrer=${to.query["referrer"]}`,
params: { nextUrl: to.fullPath },
});
}
} else {
next({
path: to.meta.admin
? "/admin/login"
: !to.query["referrer"]
? `/login/${to.params["slug"]}`
: `/login/${to.params["slug"]}?referrer=${to.query["referrer"]}`,
params: { nextUrl: to.fullPath },
});
next();
}
} else {
next();

}

});
export default router;

0 comments on commit af26472

Please sign in to comment.