Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Devise and devise_token_auth. Not working alongside

I have a Rails 4.1 Application running with Devise for authentication.

For access via mobile apps i would like to implement token auth with the recommended devise_token_auth gem. I do not use Omniauth

The functionality of the existing app should not be altered.

What i did:

Installed devise_token_auth via gemfile.

Used the generator: rails g devise_token_auth:install User auth

Changed the migration to add the required fields. Migration failed due missing of Omniauth. So i also installed it.

Changed routes.rb

devise_for :users, :skip => [:sessions, :registrations, :omniauth_callbacks]
  as :user do
    get 'register' => 'users/registrations#new', :as => :new_user_registration
    post 'register' => 'users/registrations#create', :as => :user_registration
    get 'sign_in' => 'devise/sessions#new', :as => :new_user_session
    post 'sign_in' => 'devise/sessions#create', :as => :user_session
    delete '/' => 'users/sessions#destroy', :as => :destroy_user_session
  end

added:

namespace :api do
scope :v1 do
  mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks]
end

end

In User Model i have:

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :confirmable, :timeoutable, :lockable
include DeviseTokenAuth::Concerns::User

Now when i try to sign_up a new user it gives me the validation error:

Uid can't be blank

Does someone had the same problem and resolved it?

What i find strange is that it needs to have Omniauth installed.

Update:

I overwrite the Devise registration controller create action:

build_resource(sign_up_params)
 resource.uid = resource.email
 resource.provider = ''

Now when i sign_in i get:

{"errors":["Authorized users only."]}

in Browser.

like image 552
Nico Avatar asked Mar 09 '15 14:03

Nico


1 Answers

Adding the following to app/models/user.rb:

before_validation do
  self.uid = email if uid.blank?  
end

did it for me. Also make sure the provider is set to "email" for "provider".

like image 68
Thirdium Avatar answered Sep 20 '22 09:09

Thirdium