1

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.

2 Answers 2

2
  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

Sign up to request clarification or add additional context in comments.

3 Comments

Current user -> This is a JWT token with AdonisJS : jwt.io/… . The uid field represents the id of the user
Thank you for your kind reply. I want a proper way doing these things in adonis.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.