diff --git a/src/router/guards/auth-guard.ts b/src/router/guards/auth-guard.ts index eb758118e..3331459e7 100644 --- a/src/router/guards/auth-guard.ts +++ b/src/router/guards/auth-guard.ts @@ -1,11 +1,14 @@ import { NavigationGuard } from "vue-router"; 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 { SettingsRouteName } from "../settings"; export const authGuardIfNeeded: NavigationGuard = async (to, from, 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 // because we may not have loaded the user from the local storage yet 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(); };