Running with Ruby

Tag: authorization (page 1 of 2)

Rails + Devise and remember_me (rememberable) by default

I’ve wanted to add an automatic remember_me for Devise by default (without a checkbox). First I’ve tried to do something like this:

def create
  params[:user].merge!(remember_me: true)
  super
end

Unfortunately it doesn’t seem to work. I have a rememberable strategy included in my User model, but still, remember_user_token cookie was not created. I don’t know why, even with remember_me option provided, it sets it to false. The easiest (and working) way to fix this, is to overwrite remember_me method, so it will always return true value.

# User of our portal
class User < ActiveRecord::Base

  devise :database_authenticatable, 
    :trackable, :encryptable, :confirmable, :recoverable,
    :registerable, :validatable, :lockable, :rememberable

  # @return [Boolean] user should be remembered when he logs in (with cookie)
  #   so he won't be asked to login again
  def remember_me
    true
  end
end

To be honest I didn’t have time to investigate it more deeply, so maybe there’s a better solution but this one works. After that, remember_user_token cookie is created and the session isn’t lost after user closes his browser.

Update

As Christoph mentioned in one of the comments, this is the “official” way to do this: , so probably there’s no better way ;)

Mocking Doorkeeper access token with Mocha

Recently I’ve been testing some stuff behind Doorkeeper and I needed to mock Doorkeeper token to return a stub. In older Doorkeeper versions it was done like this:

stub_prs = {
  :accessible? => true,
  :resource_owner_id => (user.nil? ? @user.id : user.id),
  :expired? => false
}
Doorkeeper::AccessToken.expects(:find_by_token).returns(stub(stub_prs))

However in a new Doorkeeper version, the’ve changed something and my mockings stopped working. So if you use newest Doorkeeper version, the mock should look like this:

stub_prs = {
  :accessible? => true,
  :resource_owner_id => (user.nil? ? @user.id : user.id),
  :expired? => false
}.merge(params)
@controller.expects(:doorkeeper_token).returns(stub(stub_prs)).at_least_once
Olderposts

Copyright © 2018 Running with Ruby

Theme by Anders NorenUp ↑