import WaveUI from '../../node_modules/wave-ui/src/wave-ui/core' import { authenticator, currentProfile } from '../services' const DEV_MODE = import.meta.env.VITE_DEV == 'true' async function log(to) { // if (DEV_MODE) { if (!currentProfile.isLoggedIn || !currentProfile.isComplete) { console.info( `[Guard Status debug]: Profile: ${currentProfile.id.value} | Login: ${currentProfile.isLoggedIn} | Complete: ${currentProfile.isComplete}`, ) } console.info('[Guard Status debug]: being routed to:', to.fullPath) // } } const loginIfToken = async () => { const sessionData = await authenticator.checkSessionValid() if ( sessionData?.profileId && sessionData?.sessionToken ) { await currentProfile.login( sessionData.profileId, WaveUI.instance.notify, sessionData.sessionToken, ) } } const checkLoginStatus = async (destination, nextCb) => { if(!currentProfile.isLoggedIn) { await loginIfToken() } log(destination) if (DEV_MODE) { nextCb() } else if ( !currentProfile.isLoggedIn ) { nextCb('/login') } else if ( destination.meta.requiresCompleteProfile && destination.meta.requiresAuth ) { nextCb('/onboarding') } else { nextCb() } } export { checkLoginStatus }