core icon indicating copy to clipboard operation
core copied to clipboard

AdonisAuth - Return User data and JWT

Open bryannbarbosa opened this issue 8 years ago • 11 comments

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
}

bryannbarbosa avatar Feb 11 '18 00:02 bryannbarbosa

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
}

antwaanh avatar Feb 11 '18 02:02 antwaanh

@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
}

bryannbarbosa avatar Feb 11 '18 03:02 bryannbarbosa

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
}

antwaanh avatar Feb 11 '18 07:02 antwaanh

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 avatar Feb 11 '18 11:02 touskar

@touskar Using this code, I received the following response:

{
    "response": "E_INVALID_JWT_TOKEN: jwt must be provided"
}

bryannbarbosa avatar Feb 11 '18 16:02 bryannbarbosa

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.

ammezie avatar Feb 11 '18 21:02 ammezie

I'm having the same issue :(

jpcbarros avatar Oct 16 '18 19:10 jpcbarros

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.

RomainLanz avatar Oct 17 '18 06:10 RomainLanz

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.

Tim-MCC avatar Oct 15 '19 20:10 Tim-MCC

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. 👍

RomainLanz avatar Oct 22 '19 11:10 RomainLanz

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.

lock[bot] avatar Mar 10 '20 20:03 lock[bot]