Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise token auth - Can't create user account after installing devise token authgem

I installed devise token auth gem.https://github.com/lynndylanhurley/devise_token_auth#model-concerns

I managed to get routes and all. But unfortuntely the signup is stopped working.

when I try to signup, it shows an error like this.

    NoMethodError in Devise::RegistrationsController#create
    undefined method `provider' for #<User:0x007f49fd5da2e8>

 Extracted source (around line #433):

  else
    match = match_attribute_method?(method.to_s)
    match ? attribute_missing(match, *args, &block) : super
  end
end

Here is my routes

   devise_for :admin_users, ActiveAdmin::Devise.config
   ActiveAdmin.routes(self)
   devise_for :users
   namespace :api do
     scope :v1 do
      mount_devise_token_auth_for 'User', at: 'auth'
     end
   end

According to the documentation, signup requires email, password, and password_confirmation. I'm not what I miss here.

and here is the schema for 'User'

create_table "users", force: :cascade do |t|
t.string   "email",                  default: "", null: false
t.string   "encrypted_password",     default: "", null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,  null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "authentication_token"
t.string   "provider",                            null: false
t.string   "uid",                    default: "", null: false
t.string   "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string   "unconfirmed_email"
t.string   "name"
t.string   "nickname"
t.string   "image"
t.string   "tokens"
end

   add_index "users", ["authentication_token"], name: "index_users_on_authentication_token", using: :btree
   add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
   add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
   add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree

Here is user model

     class User < ActiveRecord::Base
      # Include default devise modules.
      devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable,
        :confirmable, :omniauthable
     include DeviseTokenAuth::Concerns::User
    has_many :movies
    has_many :reviews

   end

This is the POST request which I used for creating user account?

    {
    "user": {
    "email": "[email protected]",
    "password": "password12345678",
    "password_confirmation": "password12345678" }

     }

and the response was something like this.

      {"status":"error","errors":["Please submit proper sign up data in request body."]}

Here is the log for corresponding request.

  Started POST "/api/v1/auth/" for 127.0.0.1 at 2015-05-28 15:51:28 +0530
Processing by DeviseTokenAuth::RegistrationsController#create as */*
  Parameters: {"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "registration"=>{"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}}
Can't verify CSRF token authenticity
Unpermitted parameters: user, registration
Filter chain halted as :validate_sign_up_params rendered or redirected
Completed 422 Unprocessable Entity in 4ms (Views: 0.3ms | ActiveRecord: 0.0ms)

What will be the issue behind that error, how can I solve it?

like image 417
user121212 Avatar asked Feb 11 '23 00:02

user121212


1 Answers

Somehow I managed figured out what missing with the implementation. When I use email authentication, the devise token auth gem requires uid and a provider.

When we trying to post the request without those parameters, It will fail due to parameter validation. To bypass it, we can generate a uid and a provider using before_validation in the user method.

Here I use email as provider if the provider is blank, and for the uid, I use email if it is blank.

This is what I wrote in the user model.

  before_validation :set_provider
  before_validation :set_uid

  def set_provider
    self[:provider] = "email" if self[:provider].blank?
  end

  def set_uid
    self[:uid] = self[:email] if self[:uid].blank? && self[:email].present?
  end

By this way, you will get the necessary parameters for user signup.

like image 120
user121212 Avatar answered Feb 23 '23 07:02

user121212