FAQ

I have missing headers or issues with batch requests

Try disabling change_headers_on_each_request, it's a nice to have security enhancement but not crucial. If you are curious, you can check how we manage the tokens and batch requests

Can I use this gem alongside standard Devise?

Yes! But you will need to enable the support of separate routes for standard Devise. So do something like this:

config/initializers/devise_token_auth.rb

1
DeviseTokenAuth.setup do |config|
2
config.enable_standard_devise_support = true
3
end
Copied!

config/routes.rb

1
Rails.application.routes.draw do
2
3
# standard devise routes available at /users
4
# NOTE: make sure this comes first!!!
5
devise_for :users
6
7
# token auth routes available at /api/v1/auth
8
namespace :api do
9
scope :v1 do
10
mount_devise_token_auth_for 'User', at: 'auth'
11
end
12
end
13
14
end
Copied!

Another method for using this gem alongside standard Devise (updated May 2018)

Some users have been experiencing issues with using this gem alongside standard Devise, with the config.enable_standard_devise_support = true method.
Another method suggested by jotolo is to have separate child application_controller.rb files that use either DeviseTokenAuth or standard Devise, which all inherit from a base application_controller.rb file. For example, you could have an api/v1/application_controller.rb file for the API of your app (which would use Devise Token Auth), and a admin/application_controller.rb file for the full stack part of your app (using standard Devise). The idea is to redirect each flow in your application to the appropriate child application_controller.rb file. Example code below:

controllers/api/v1/application_controller.rb

Child application controller for your API, using DeviseTokenAuth.
1
module Api
2
module V1
3
class ApplicationController < ::ApplicationController
4
skip_before_action :verify_authenticity_token
5
include DeviseTokenAuth::Concerns::SetUserByToken
6
end
7
end
8
end
Copied!

controllers/admin/application_controller.rb

Child application controller for full stack section, using standard Devise.
1
module Admin
2
class ApplicationController < ::ApplicationController
3
before_action :authenticate_admin!
4
end
5
end
Copied!

controllers/application_controller.rb

The base application controller file. If you're using CSRF token protection, you can skip it in the API specific application controller (api/v1/application_controller.rb).
1
class ApplicationController < ActionController::Base
2
protect_from_forgery with: :exception
3
end
Copied!

config/initializers/devise_token_auth.rb

Keep the enable_standard_devise_support configuration commented out or set to false.
1
# config.enable_standard_devise_support = false
Copied!

Why are the new routes included if this gem doesn't use them?

Removing the new routes will require significant modifications to devise. If the inclusion of the new routes is causing your app any problems, post an issue in the issue tracker and it will be addressed ASAP.

I'm having trouble using this gem alongside ActiveAdmin...

For some odd reason, ActiveAdmin extends from your own app's ApplicationController. This becomes a problem if you include the DeviseTokenAuth::Concerns::SetUserByToken concern in your app's ApplicationController.
The solution is to use two separate ApplicationController classes - one for your API, and one for ActiveAdmin. Something like this:
1
# app/controllers/api_controller.rb
2
# API routes extend from this controller
3
class ApiController < ActionController::Base
4
include DeviseTokenAuth::Concerns::SetUserByToken
5
end
6
7
# app/controllers/application_controller.rb
8
# leave this for ActiveAdmin, and any other non-api routes
9
class ApplicationController < ActionController::Base
10
end
Copied!

How can I use this gem with Grape?

You may be interested in GrapeTokenAuth or GrapeDeviseTokenAuth.

How can I use this gem with Solidus/Spree?

You may be interested in solidus_devise_token_auth.

I already have a user, how can I add the new fields?

  1. 1.
    First, remove the migration generated by the following commandrails g devise_token_auth:install [USER_CLASS] [MOUNT_PATH] and then:.
  2. 2.
    Create another fresh migration:
1
# create migration by running a command like this (where `User` is your USER_CLASS table):
2
# `rails g migration AddTokensToUsers provider:string uid:string tokens:text`
3
4
def up
5
add_column :users, :provider, :string, null: false, default: 'email'
6
add_column :users, :uid, :string, null: false, default: ''
7
add_column :users, :tokens, :text
8
9
# if your existing User model does not have an existing **encrypted_password** column uncomment below line.
10
# add_column :users, :encrypted_password, :string, null: false, default: ''
11
12
# if your existing User model does not have an existing **allow_password_change** column uncomment below line.
13
# add_column :users, :allow_password_change, :boolean, default: false
14
15
# the following will update your models so that when you run your migration
16
17
# updates the user table immediately with the above defaults
18
User.reset_column_information
19
20
# finds all existing users and updates them.
21
# if you change the default values above you'll also have to change them here below:
22
User.find_each do |user|
23
user.uid = user.email
24
user.provider = 'email'
25
user.save!
26
end
27
28
# to speed up lookups to these columns:
29
add_index :users, [:uid, :provider], unique: true
30
end
31
32
def down
33
# if you added **encrypted_password** above, add here to successfully rollback
34
# if you added **allow_password_change** above, add here to successfully rollback
35
remove_columns :users, :provider, :uid, :tokens
36
end
Copied!

I want to add a new param for sign up and account update

Override the controller and describe the new parameters you want to add in the configure_permitted_parameters method.
When creating an account, add params under sign_up.
When updating your account, add params under account_update.
For example:
1
class RegistrationsController < DeviseTokenAuth::RegistrationsController
2
before_action :configure_permitted_parameters
3
4
protected
5
6
def configure_permitted_parameters
7
devise_parameter_sanitizer.permit(:sign_up, keys: %i(name))
8
devise_parameter_sanitizer.permit(:account_update, keys: %i(name))
9
end
10
end
Copied!
Last modified 5mo ago