| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import { currentProfile, authenticator } from '../services'
- import WaveUI from '../../node_modules/wave-ui/src/wave-ui/core'
- 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)
- }
- }
-
- // TODO: move to utils/index.js and import to this file, OnboardingView.vue and VerifyView.vue
- const grabStoredCookie = cookieKey => {
- const cookies = document.cookie.split('; ').reduce((prev, current) => {
- const [name, ...value] = current.split('=')
- prev[name] = value.join('=')
- return prev
- }, {})
- const cookieVal = cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
- return cookieVal
- }
-
- const verifySession = async () => {
- const hashedAccessToken = grabStoredCookie('siimee_access')
- if (!hashedAccessToken)
- return console.warn('WARNING :=> accessToken is not defined')
- const validatedToken = await authenticator.validateSession(
- hashedAccessToken,
- )
- if (validatedToken.error) {
- console.error('ERROR :=>', validatedToken.error)
- } else {
- return validatedToken
- }
- }
-
- const loginIfToken = async () => {
- const sessionData = await verifySession()
- if (
- sessionData?.profileId &&
- sessionData?.accessToken &&
- !currentProfile.isLoggedIn
- ) {
- await currentProfile.login(
- sessionData.profileId,
- // NOTE: probably not correct...
- WaveUI.notify,
- sessionData.accessToken,
- )
- }
- }
-
- const checkLoginStatus = async (destination, nextCb) => {
- await loginIfToken()
- log(destination)
- if (DEV_MODE) {
- nextCb()
- } else if (
- destination.meta.requiresCompleteProfile &&
- !currentProfile.isLoggedIn &&
- !currentProfile.isComplete
- ) {
- nextCb('onboarding')
- } else if (
- destination.meta.requiresCompleteProfile &&
- destination.meta.requiresAuth &&
- !currentProfile.isLoggedIn
- ) {
- nextCb('login')
- } else {
- nextCb()
- }
- }
-
- export { checkLoginStatus }
|