jeudi 3 mars 2016

Devise Rails 3.2.22 remember_user_token not set

I'm trying to manage some weird issue with devise 3.5.2 and rails 3.2.22.

The issue is devise did not set remember_user_token cookie. My App is an API so I have sessions_controller overriden.

def create
  # build_resource
  resource = User.find_for_database_authentication(email: params[:email])
  return invalid_login_attempt(email: t('api.errors.email.user_not_exists', email: params[:email])) unless resource

  unless resource.active_for_authentication?
    render json: { success: false }.merge(errors: t('api.errors.email.inactive')), status: :ok
    return
  end

  if resource.is_deleted
    render json: { success: false }.merge(errors: t('api.errors.email.deleted')), status: :ok
    return
  end

  if resource.valid_password?(params[:password])
    sign_in("user", resource)
    resource.enable_beta unless resource.beta_enabled?
    render json: { success: true }.merge(user: resource.as_json(user: resource)), status: :ok
    return
  end
  invalid_login_attempt(password: t('api.errors.email.wrong_password'))
end

Here is my User model (devise section)

# Extensions
devise :database_authenticatable, :registerable, :confirmable,
     :recoverable, :rememberable, :trackable, :validatable,  :lastseenable

Login is working like a charm, but remember_user_token cookie is not set.

Started POST "/api/v1/users/sign_in" for 127.0.0.1 at 2016-03-03 16:09:23 +0600
Processing by API::V1::SessionsController#create as JSON
Parameters: {"email"=>"email@gmail.com", "password"=>"[FILTERED]",  "remember_me"=>true, "session"=>{"email"=>"email@gmail.com", "password"=>" [FILTERED]", "remember_me"=>true}}
User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."is_deleted" = 'f' AND "users"."email" = 'email@gmail.com' LIMIT 1
(0.2ms)  BEGIN
(0.5ms)  UPDATE "users" SET "last_sign_in_at" = '2016-03-03  10:06:43.642950', "current_sign_in_at" = '2016-03-03 10:09:23.152373',  "sign_in_count" = 179, "updated_at" = '2016-03-03 10:09:23.154418',  "users_for_mentions" = '---
- ''no''
' WHERE "users"."id" = 300
Tag Load (0.4ms)  SELECT "tags".* FROM "tags" WHERE "tags"."host_id" =  300 AND "tags"."host_type" = 'User' LIMIT 1
(0.5ms)  COMMIT
User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 300]]
City Load (0.3ms)  SELECT "cities".* FROM "cities" WHERE "cities"."id" =  3 LIMIT 1
Completed 200 OK in 2515.4ms (Views: 8.7ms | ActiveRecord: 4.4ms |  Sphinx: 0.0ms)

cookie browser I also tried to set cookie by hands:

  cookies["remember_user_token"] = {
    value: user.class.serialize_into_cookie(user.reload),
    expires: 3.year.from_now,
    domain: RAILS_DOMAIN
  }

But with no luck too. It seems somethong prevents to cookie setting. Could you please share your thoughts?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire