Reputation: 8459
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:
Do you have to send token in every Ajax request?
How do you get current user associated with given jwt token?
What is refreshToken?
Providing a link of detailed guide will be enough.
Upvotes: 1
Views: 716
Reputation: 2292
To complete @ljcordero
's answer
You can get user with auth
object. Like:
async example ({ auth, request }) {
const user = await auth.getUser()
...
}
Complete documentation of auth
.
The token created by Adonis contains a UID
field. It represents the id of the user.
Here's an example token:
Upvotes: 1
Reputation: 185
Yes, the client must send his token (getter after login success) in order to access secure resources.
JWT has an «sub» property, in which you can save the id of the user for example.
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