fix: A user without profile cannot access all requiredAuth routes
Redirect to CREATE_IDENTITY if a user without a selected profile try to access a requiredAuth route not in SettingsRouteName Solves #1806
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
import { NavigationGuard } from "vue-router";
|
import { NavigationGuard } from "vue-router";
|
||||||
import { UserRouteName } from "@/router/user";
|
import { UserRouteName } from "@/router/user";
|
||||||
import { AUTH_ACCESS_TOKEN } from "@/constants";
|
import RouteName from "@/router/name";
|
||||||
|
import { AUTH_ACCESS_TOKEN, AUTH_USER_ACTOR_ID } from "@/constants";
|
||||||
import { LoginErrorCode } from "@/types/enums";
|
import { LoginErrorCode } from "@/types/enums";
|
||||||
|
import { SettingsRouteName } from "../settings";
|
||||||
|
|
||||||
export const authGuardIfNeeded: NavigationGuard = async (to, from, next) => {
|
export const authGuardIfNeeded: NavigationGuard = async (to, from, next) => {
|
||||||
if (to.meta?.requiredAuth !== true) return next();
|
if (to.meta?.requiredAuth !== true) return next();
|
||||||
|
|
||||||
|
// 1. A route that requiredAuth need a connected user
|
||||||
// We can't use "currentUser" from apollo here
|
// We can't use "currentUser" from apollo here
|
||||||
// because we may not have loaded the user from the local storage yet
|
// because we may not have loaded the user from the local storage yet
|
||||||
if (!localStorage.getItem(AUTH_ACCESS_TOKEN)) {
|
if (!localStorage.getItem(AUTH_ACCESS_TOKEN)) {
|
||||||
@@ -18,5 +21,19 @@ export const authGuardIfNeeded: NavigationGuard = async (to, from, next) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. A route that requiredAuth also need a selected profile
|
||||||
|
// except for all Settings Route
|
||||||
|
const isInSettingsRoute = Object.values(SettingsRouteName).includes(
|
||||||
|
to.name as SettingsRouteName
|
||||||
|
);
|
||||||
|
|
||||||
|
// Redirect to CREATE_IDENTITY if a user without a selected profile
|
||||||
|
// try to access a requiredAuth route not in SettingsRouteName
|
||||||
|
if (!localStorage.getItem(AUTH_USER_ACTOR_ID) && !isInSettingsRoute) {
|
||||||
|
return next({
|
||||||
|
name: RouteName.CREATE_IDENTITY,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user