How can I do to return a user's data after authentication in JWT using the Auth adonis library?
This is my current code:
async auth({request, response, auth}) {
const { email, password } = request.all()
return await auth.attempt(email, password)
}
It returns with the following answer:
{
"type": "bearer",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1YTc5ZjI1Nzc3OThkNDJmYjhlMDU4OTIiLCJpYXQiOjE1MTgzMDgzNTN9.9FAmBBkSLwS_zIBDE_zPz6DFYfohh3grcdrA70JPWgs",
"refreshToken": null
}
I would like it to return the following way, how to do it?
{
"type": "bearer",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1YTc5ZjI1Nzc3OThkNDJmYjhlMDU4OTIiLCJpYXQiOjE1MTgzMDgzNTN9.9FAmBBkSLwS_zIBDE_zPz6DFYfohh3grcdrA70JPWgs",
"user": {
"_id": id of user,
"name": "Name of user",
"email": "email of user"
"user_type": "type of user"
},
"refreshToken": null
}
Hi @bryannbarbosa,
You might try something like this
async auth({request, response, auth}) {
const { email, password } = request.all()
let token = await auth.attempt(email, password)
let user = auth.user
return {} // your structured object
}
@antwaanvh
My request response was as follows, it returns null on user property:
I'm using Lucid-Mongo as database for Auth.
{
"response": {
"type": "bearer",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1YTc5ZjI1Nzc3OThkNDJmYjhlMDU4OTIiLCJpYXQiOjE1MTgzMTgwNzl9.lyG3sId7hI4R8UiS2qfshN7oPzOryB5gEMR754ULOJw",
"refreshToken": null
},
"user": null
}
I've never used Lucid-Mongo, but I would assume the api is the same; however, since you already have the user's email you can query the user model to return it
const User = use('App/Models/User')
async auth({request, response, auth}) {
const { email, password } = request.all()
let token = await auth.attempt(email, password)
let user = await User.query().where('email', email).fetch()
return {} // your structured object
}
Hi, just do this:
async auth({request, response, auth}) {
const { email, password } = request.all();
let auth0 = await auth.attempt(email, password);
auth0.user = await auth.getUser();
return auth0;
}
@touskar Using this code, I received the following response:
{
"response": "E_INVALID_JWT_TOKEN: jwt must be provided"
}
Hi @bryannbarbosa I don't think this is an issue and it is inappropriate to post it here.
It's best suited for the forum and you will get faster response there.
I'm having the same issue :(
Hey @jpcbarros! 馃憢
If you have any bugs with the framework feel free to open a new issue.
If you have any questions, please open a thread on the forum or join the discord server.
This is in fact an issue, there's no login method to return the user information and auth.attempt returns token.
Right now i need to include some of the user info as jwtPayload when user signs in and certainly you can't do it with auth.attempt because you don't have the user object yet.
It's not ideal at all to use auth.attempt just to check uid and pass matches and then try to get the user and then regenerate the token with your jwt playload/claim. For one you're making extra call to the database for second you're wasting resources calculating jwt token twice.
There needs to be either something like auth.login method which checks the credential and returns user object if successful or make auth.attempt do it and use auth.generate for generating tokens.
This is in fact something we can have in consideration for the next release of Adonis @Tim-MCC.
I would recommend to create an RFC explaining exactly what you want and expect. 馃憤
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
I've never used Lucid-Mongo, but I would assume the api is the same; however, since you already have the user's email you can query the user model to return it