Sign associated user/gestionnaire in after password change

This commit is contained in:
Julien Portalier 2016-10-13 18:26:08 +02:00 committed by Xavier J
parent c2068d055d
commit 45e14d7a43
4 changed files with 76 additions and 0 deletions

View file

@ -1,4 +1,6 @@
class Gestionnaires::PasswordsController < Devise::PasswordsController
after_action :try_to_authenticate_user, only: %i(update)
# GET /resource/password/new
# def new
# super
@ -29,4 +31,11 @@ class Gestionnaires::PasswordsController < Devise::PasswordsController
# def after_sending_reset_password_instructions_path_for(resource_name)
# super(resource_name)
# end
def try_to_authenticate_user
if gestionnaire_signed_in?
user = User.find_by(email: current_gestionnaire.email)
sign_in user if user
end
end
end

View file

@ -1,4 +1,6 @@
class Users::PasswordsController < Devise::PasswordsController
after_action :try_to_authenticate_gestionnaire, only: %i(update)
# GET /resource/password/new
# def new
# super
@ -29,4 +31,11 @@ class Users::PasswordsController < Devise::PasswordsController
# def after_sending_reset_password_instructions_path_for(resource_name)
# super(resource_name)
# end
def try_to_authenticate_gestionnaire
if user_signed_in?
gestionnaire = Gestionnaire.find_by(email: current_user.email)
sign_in gestionnaire if gestionnaire
end
end
end

View file

@ -0,0 +1,29 @@
require "spec_helper"
describe Gestionnaires::PasswordsController, type: :controller do
before do
@request.env["devise.mapping"] = Devise.mappings[:gestionnaire]
end
describe "update" do
context "when associated gestionnaire" do
let(:gestionnaire) { create(:gestionnaire, email: 'unique@plop.com', password: 'password') }
let(:user) { create(:user, email: 'unique@plop.com', password: 'password') }
before do
@token = gestionnaire.send(:set_reset_password_token)
user # make sure it's created
end
it "also signs user in" do
put :update, gestionnaire: {
reset_password_token: @token,
password: "supersecret",
password_confirmation: "supersecret",
}
expect(subject.current_gestionnaire).to eq(gestionnaire)
expect(subject.current_user).to eq(user)
end
end
end
end

View file

@ -0,0 +1,29 @@
require "spec_helper"
describe Users::PasswordsController, type: :controller do
before do
@request.env["devise.mapping"] = Devise.mappings[:user]
end
describe "update" do
context "when associated gestionnaire" do
let(:user) { create(:user, email: 'unique@plop.com', password: 'password') }
let(:gestionnaire) { create(:gestionnaire, email: 'unique@plop.com', password: 'password') }
before do
@token = user.send(:set_reset_password_token)
gestionnaire # make sure it's created
end
it "also signs gestionnaire in" do
put :update, user: {
reset_password_token: @token,
password: "supersecret",
password_confirmation: "supersecret",
}
expect(subject.current_user).to eq(user)
expect(subject.current_gestionnaire).to eq(gestionnaire)
end
end
end
end