Home > Software engineering >  Return authorization token with users credentials after sign in using devise-auth-token
Return authorization token with users credentials after sign in using devise-auth-token

Time:09-12

I am using devise-auth-token. It is working perfectly. I want when a user is signed in, she/she should get authorization not only in header section but also with the user fields. Following are the screen shots attached for everyone to understand it better.Authorization token is in the headers section and I want that in the user returning variables like under image or nickname. [1]: https://i.stack.imgur.com/viJOf.png

CodePudding user response:

If you go through devise_token_auth's codebase, you can see how the tokens are generated here.

You can override the sessions_controller's create action like this:

def create
  super do
    render json: { user: current_user, 
                   token: @token }.to_json and return
  end
end

The and return prevents from DoubleRenderError.

It returns token along with your current_user in this format :

"token": {
        "client": "ZYUyzKCspyEN1dhUg",
        "token": "_y3JeRYQaEqH1cA",
        "token_hash": "ZGzsWyFNcmfoq5YlD9c2",
        "expiry": 166411321 
        }
  • Related