Search code examples
ruby-on-railsdevisedevise-token-auth

devise_token_auth how to access user.created_at


I'm using devise_token_auth on my Rails 5 api

Currently, when I make a POST request to /auth/sign_in everything goes well, I get data looking like this:

"data": {
    "id": 3,
    "email": "me@user.com",
    "name": null,
    "nickname": null,
    "bio": null,
    "description": null,
    "url": null,
    "image": {
        "url": null
    },
    "provider": "email",
    "uid": "me@user.com",
    "allow_password_change": false,
    "role": null
}

however, I'd like to also be able to access timestamp attributes (created_at, updated_at), which are already enabled:

ActiveRecord::Schema.define(version: 2018_10_18_201621) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "users", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false

Any ideas? Thanks in advance


Solution

  • Check this issue discussion about overriding the rendering methods on devise_token_auth gem on it's github repo: https://github.com/lynndylanhurley/devise_token_auth/issues/597.

    Try the code below on your User model so you can use your custom serializer instead of the default one:

    def token_validation_response                                                                                                                                         
      UserSerializer.root = false
      UserSerializer.new(self).as_json
    end