mobilizon/js/src/router/guards/auth-guard.ts

23 lines
677 B
TypeScript
Raw Normal View History

import { NavigationGuard } from "vue-router";
import { UserRouteName } from "@/router/user";
import { AUTH_ACCESS_TOKEN } from "@/constants";
import { LoginErrorCode } from "@/types/enums";
export const authGuardIfNeeded: NavigationGuard = async (to, from, next) => {
if (to.meta?.requiredAuth !== true) return next();
// 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)) {
return next({
name: UserRouteName.LOGIN,
query: {
code: LoginErrorCode.NEED_TO_LOGIN,
redirect: to.fullPath,
},
});
}
return next();
};