glinda93
glinda93

Reputation: 8459

Authentication flow in Vue.js + REST api

I'm building a project with vue cli and adonis.js. The frontend will be only with Vue.js and backend will only provide REST api. I'm going to use jwt for authentication. The problem is, I don't understand authentication flow with jwt. I recently migrated from Laravel to adonis, so I have little knowledge in Vue + Ajax + REST api. Here are my questions:

  1. Do you have to send token in every Ajax request?

  2. How do you get current user associated with given jwt token?

  3. What is refreshToken?

Providing a link of detailed guide will be enough.

Upvotes: 1

Views: 716

Answers (2)

crbast
crbast

Reputation: 2292

To complete @ljcordero's answer

Adonis side:

You can get user with auth object. Like:

async example ({ auth, request }) {
    const user = await auth.getUser()
    ...
}

Complete documentation of auth.

VueJS side:

The token created by Adonis contains a UID field. It represents the id of the user.

Here's an example token:

enter image description here

Upvotes: 1

ljcordero
ljcordero

Reputation: 185

  1. Do you have to send token in every Ajax request?

Yes, the client must send his token (getter after login success) in order to access secure resources.

  1. How do you get current user associated with given jwt token?

JWT has an «sub» property, in which you can save the id of the user for example.

  1. What is refreshToken?

When the client token get expired, you must provide a new one, without the process of login again.

Here is a guide with nodejs: https://www.codementor.io/@olatundegaruba/5-steps-to-authenticating-node-js-with-jwt-7ahb5dmyr

Upvotes: 2

Related Questions