I am trying to create a log in system for an iOS app with a rails back end powered by devise and door keeper.
I want to limit the number of network requests so don't want to have to get the token from credentials then get the user details as a separate request.
Here is my current attempt:
token = Doorkeeper::AccessToken.create!(application_id: @application_id,
resource_owner_id: current_user.id, :expires_in => 168.hours)
puts token.token
render :json => {:user => current_user, :token => token.as_json(:include=> token)},
status: :ok, location: :users
However what is being returned is:
{"user":{"id":2,"email":"user3@test.com","created_at":"2014-06-12T17:25:12.000Z",
"updated_at":"2014-06-13T12:20:18.536Z",
"firstName":"user","lastName":"test","subscription":null},
"token":{"resource_owner_id":2,"scopes":[],"expires_in_seconds":604800,
"application":{"uid":"[Filtered]"}}}
So the actual access_token key isn't being passed back to allow me to make future calls.
I can see that the token itself isn't returned in DoorKeeper::AccessToken.as_json
, but token.as_json(:include=> token)
still doesn't return it.
Does anyone know how to return the AccessToken, including the access token itself, as json?
I know it's been a while this has been solved. I recently wanted to implement the same behavior on my API and I relied on model association in order to achieve it :
Then using serializers :
You could simply return your data as :
which will output a JSON that looks like :
Easy! you don't need to inherit from
Doorkeeper::TokensController
but you can if it suits better to your case:The way I managed to solve this was to create my own AccessToken class that overloads the as_json method to include the fields I wanted.
e.g
If anyone has a better solution I'm all ears
The way I handled this was to create a custom tokens controller and overriding the token request action. There I could append custom stuff to response.
Just make sure that you point to this controller in
routes.rb
This is tested and it works, I am using it in my projects.
Actually we can get access token with:
you can include it in the json response so you don't need to edit anything inside doorkeeper itself.
However, don't forget to add some conditionals before including it in the json response as it doesn't seem so good to always show it in the response.