import { NavigationGuard } from "vue-router" ; import { RootStore } from "./store" ; const isAuthenticated = async ( ) => { if ( RootStore . state . auth . apiKey ) return true ; if ( RootStore . state . auth . loadedInitialAuth ) return false ; await RootStore . dispatch ( "auth/loadInitialAuth" ) ; if ( RootStore . state . auth . apiKey ) return true ; return false ; } ; export const authGuard = async ( to , from , next ) => { if ( await isAuthenticated ( ) ) return next ( ) ; window . location . href = `${ process . env . API_URL }/auth/login` ; } ; export const loginCallbackGuard = async ( to , from , next ) => { if ( to . query . apiKey ) { await RootStore . dispatch ( "auth/setApiKey" , { key : to . query . apiKey } ) ; window . location . href = "/dashboard" ; } else { window . location . href = `/?error=noAccess` ; } } ; export const authRedirectGuard = async ( to , form , next ) => { if ( await isAuthenticated ( ) ) return next ( "/dashboard" ) ; window . location . href = `${ process . env . API_URL }/auth/login` ; } ;