2020-02-18 07:57:00 +00:00
|
|
|
import { NavigationGuard } from "vue-router";
|
|
|
|
import { UserRouteName } from "@/router/user";
|
|
|
|
import { AUTH_ACCESS_TOKEN } from "@/constants";
|
2020-11-27 18:27:44 +00:00
|
|
|
import { LoginErrorCode } from "@/types/enums";
|
2019-04-01 09:49:54 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
export const authGuardIfNeeded: NavigationGuard = async (to, from, next) => {
|
2019-04-01 09:49:54 +00:00
|
|
|
if (to.meta.requiredAuth !== true) return next();
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
// We can't use "currentUser" from apollo here
|
|
|
|
// because we may not have loaded the user from the local storage yet
|
2019-08-12 14:04:16 +00:00
|
|
|
if (!localStorage.getItem(AUTH_ACCESS_TOKEN)) {
|
2019-04-01 09:49:54 +00:00
|
|
|
return next({
|
|
|
|
name: UserRouteName.LOGIN,
|
|
|
|
query: {
|
|
|
|
code: LoginErrorCode.NEED_TO_LOGIN,
|
|
|
|
redirect: to.fullPath,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
};
|