<template lang="pug">
.wait-message
    p.verify-message Thanks for authenticating your email!
    p.verify-message Please give us a moment to redirect you back to the survey
</template>

<script>
import { currentProfile, authenticator } from '../services'
let hash = null
export default {
    name: 'VerifyView',
    async created() {
        hash = this.$route.params.hashedToken
        try {
            this.isHashInUrl(hash)
            await this.verifyActiveSession(hash)
            const sessionData =
                await authenticator.verifySessionCookie('siimee_session')
            currentProfile.login(
                sessionData.profileId,
                this.$waveui.notify,
                sessionData.accessToken,
            )
        } catch (err) {
            console.error(err)
        }
        this.$router.push('/')
    },
    methods: {
        isHashInUrl(hash) {
            if (!hash) throw new Error('URL contains no hash!')
        },
        async verifyActiveSession(hashedToken) {
            const sessionData = await authenticator.verifySession(hashedToken)
            if (!sessionData.hashesMatch)
                throw new Error('Hash is not in activeSessions!')
        },
    },
}
</script>

<style>
.wait-message {
    margin: 5rem auto;
    text-align: center;
    width: 90%;
    max-width: 35rem;
    font-size: 150%;
    font-weight: bold;
}
</style>
