Strugling with Persona verification token in a Adonisjs+Vue Router

Hi.
I’m implementing Persona in an Adonisjs (API) + Vuetify+Vue Router SPA and I’m having problems with the token data.

When the user is created I send an email with a link to a page where I take the token and send it to the LoginController where I check it.

Event Listener User.js:

User.registered = async (data) => {

    const emailToken = querystring.encode({
        token:data.token
    })

    const tokenUrl = `//localhost:8080/check_email?${emailToken}` 

    const payload = {
        token:tokenUrl,
        user:data.user
    }

    await Mail.send('emails.welcome', payload, (message) => {
        message
          .to(data.user.email)
          .from('welcome@redempresarial.cl')
          .subject('Bienvenido a Red Empresarial !')
      })

}

With this I send an email with the link
welcome.edge

<a href="{{ token }}">;
Confirma tu email
</a>

this is the link url it generates:

http://localhost:8080/check_email?token=62d08fcc31c54d1b85a73263e9b3a76ajnEBOLZdz9cbC2P6LzxOOJdEd2TWQNwNpOlcpTwmz0g%3D

In the check_email page I pick the token and send a post request to the controller:

CheckEmail.vue

created(){
    this.checkToken(this.$route.query.token)
 },
methods:{
            checkToken(token){
                axios.post('confirm',
                    {token:token})
                    .then((response)=>{
                        this.$router.push({name:'login',params:{confirmed:true}})
                    })
                    .catch((error)=>{

                    })
            }

And in the LoginController I have this function that picks the token and checks it:

async confirmEmail({response,request}){

    const token = request.only(['token'])

// tried also with 
// const realToken = await querystring.parse(token) 
// but I get realToken ={}

    const user = await Persona.verifyEmail(token)

    return response.send(user)

  }

I’m having a ER_PARSE_ERROR in the verifyEmail function probably because the token gets corrupted somewhere.

Any idea on what I’m doing wrong?

Thanks.

1 Like

I solved using token.token on verifyEmail, just a little mistake.

async confirmEmail({response,request}){

    const token = request.only(['token'])

    const user = await Persona.verifyEmail(token.token)

    return response.send(user)

  }
1 Like

Hi! Why did you use events for sending email? Is it easier way to send email directly in “register” method of UserController after registration, isn’t it?

You are right. It’s not needed, but I use it to keep the Controller with just the tasks it has to do. That’s why I use also hooks for other tasks. Just a matter of taste I think.

1 Like

@jorgeyoma Hi!

You can also do something like this :

const { token } = request.only(['token'])

so you can write token instead of token.token.

Also, you can delegate sending mail to a service and call it from your controller instead of using events, but as you said, it’s just a matter of taste.

1 Like