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 (!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.isValidSession() if ( sessionData?.profileId && sessionData?.sessionToken && !currentProfile.isLoggedIn ) { await currentProfile.login( sessionData.profileId, WaveUI.instance.notify, sessionData.sessionToken, ) } } const checkLoginStatus = async (destination, nextCb) => { await loginIfToken() log(destination) if (DEV_MODE) { nextCb() } else if ( destination.meta.requiresAuth && !currentProfile.isLoggedIn && !currentProfile.isComplete ) { nextCb('/onboarding') } else if (destination.meta.requiresAuth && !currentProfile.isLoggedIn) { nextCb('/login') } else { nextCb() } } export { checkLoginStatus }