Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/store recognisable session on registration #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/devise_recognisable/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

class DeviseRecognisable::RegistrationsController < Devise::RegistrationsController
append_after_action :store_recognisable_details, only: :create

# After the user has been signed up, we save the ip address in the
# DeviseRecognisable::RecognisableSession table.
def store_recognisable_details
return unless resource.persisted?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @quorauk! I think the main question I'd be asking is if we have some social proof that this is the right way to add a hook after #create e.g. this guard is enough to confirm that we should be creating a RecogniseableSession. Do you have anything you can point to?

DeviseRecognisable::RecognisableSession.create!(
recognisable: resource_class.find_by(email: params[resource_name][:email]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you can't just pass resource here?

sign_in_ip: request.location.ip,
sign_in_at: Time.now,
user_agent: request.user_agent,
accept_header: request.headers["HTTP_ACCEPT"]
)
end
end
1 change: 1 addition & 0 deletions lib/devise-recognisable/mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def default_controllers(options)
options[:controllers] ||= {}
# Use our SessionsController instead of Devise's.
options[:controllers][:sessions] ||= 'devise_recognisable/sessions'
options[:controllers][:registrations] ||= 'devise_recognisable/registrations'
super
end

Expand Down
Empty file.
2 changes: 1 addition & 1 deletion spec/dummy-app/spec/features/sign_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Log in'
}.to change { DeviseRecognisable::RecognisableSession.count }.from(0).to(1)
}.to change { DeviseRecognisable::RecognisableSession.count }.from(1).to(2)
end

it "doesn't create a new DeviseRecognisable::RecognisableSession on unsuccessful sign in" do
Expand Down
23 changes: 23 additions & 0 deletions spec/dummy-app/spec/features/sign_up_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'rails_helper'

RSpec.feature "Sign in" do
let(:email) { Faker::Internet.email }
let(:password) { Faker::Internet.password }

context 'as a new user' do
it 'creates a devise recognisable session' do
visit '/'
expect(page).to have_content 'Welcome to my website'
click_link 'Log in'
click_link 'Sign up'
fill_in 'Email', with: email
fill_in 'Password', with: password
fill_in 'Password confirmation', with: password
expect {
click_button 'Sign up'
}.to change(DeviseRecognisable::RecognisableSession, :count).by(1)
expect(page).to have_content 'Welcome!'
expect(page).to have_content('signed up successfully')
end
end
end