<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
        let sessionData = null
        /** Confirm session with backend */
        try {
            if (!hash) throw new Error('URL contains no hash!')
            sessionData = await authenticator.verifySession(hash)
            if (!sessionData.hashesMatch)
                throw new Error('Hash is not in activeSessions!')
        } catch (err) {
            console.error(err)
        }
        /** Check if session was confirmed and is now valid */
        await authenticator.checkSessionValid()
        currentProfile.login(
            sessionData.profileId,
            this.$waveui.notify,
            sessionData.accessToken,
        )
        this.$router.push('/')
    }
}
</script>

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