0
To get to the point, I want to know how to work correctly with JWT on Adonisjs (I’m learning to use the framework and I’ve never worked with JWT before). My problem now is that I don’t want to keep picking up the Bearer Token generated there in my Postman and setting it in the header so I can access my routes that need authentication. So, I first need to understand how I pass the token to the header directly to test in the browser. I’m doing it today, but I don’t know if it’s right:
"use strict";
class SessionController {
async authenticate({ request, auth, response }) {
const { email, password } = request.all();
const token = await auth.attempt(email, password);
response.response.setHeader("Authorization", `Bearer ${token.token}`);
}
}
module.exports = SessionController;
In the above example I already have the user registered in the database, only Gero for him the token. I want this token to be persisted in the browser, I do not know if I am being clear on this, the idea is that from the answer of this function above, it can already access the routes that require authentication.
below the user input controller:
"use strict";
const User = use("App/Models/User");
class UserController {
async register({ request }) {
const data = request.only(["username", "password", "email"]);
const user = await User.create(data);
return user;
}
}
module.exports = UserController;
now, at last, my routes:
"use strict";
/** @type {typeof import('@adonisjs/framework/src/Route/Manager')} */
const Route = use("Route");
Route.post("/register", "UserController.register");
Route.post("/authenticate", "SessionController.authenticate");
Route.post("/test", "WorkerController.store").middleware(["auth"]);
As you can see, I have a TEST route that calls the store function of the Worker class, I will show it, but I already say that this route is basically to see if the authentication is passing... ie, if after I authenticate there on my route /authenticate, i can already access the /test by the browser (hence the need to leave the authorization header persisted in the browser)
"use strict";
class WorkerController {
async store({ request, response, auth }) {
console.log(auth.user.email);
}
}
module.exports = WorkerController;
The authentication middleware is clear in what it does: it will only let you access the routes if you have the header authorization with a valid token. You want to change this behavior?
– Luiz Felipe
Good night Luiz, in the browser, after I do a post on the authentication route, he returns me the header with Authorization: bearer + token, only if I try to already hit a /test in the url, it returns 401 unauthorized
– Hiury B. Bressanelli
that wouldn’t already be my valid token?
– Hiury B. Bressanelli
It returns 401 because you probably didn’t set the header
Authorization: Bearer <token>
in his request to the server. Read more about this on documentation. You should always set this header in your request. Using tools like Postman, for example, you have a "header" section to insert this data.– Luiz Felipe
The documentation just says you should do this, it doesn’t show how, maybe that’s the problem. Can you show me how I would do that?! I don’t want to keep putting this in Postman, I said in the question introduction. I’d like to understand how to do this directly through the browser.
– Hiury B. Bressanelli
"My problem now is that I don’t want to keep picking up the Bearer Token generated there in my Postman and setting it in the header so I can access my routes that need authentication. So, I first need to understand how I pass the token to the header directly to test in the browser."
– Hiury B. Bressanelli
If you are using Postman, you will always have to do this manually. I don’t know how to automate this process.
– Luiz Felipe
The idea is just to make a login system? In case, an API to be consumed in a frontend?
– Victor
Yes, Victor... I am making the api to consume in React js/ React Native. Agree with me that I have no way to manually set header authorization when consuming? know how to help me?
– Hiury B. Bressanelli