Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Fix sign in with multiple fields #60

Open
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/oath/configuration.rb
Expand Up @@ -54,8 +54,7 @@ def default_hashing_method
# @see Oath.config.user_class
def default_find_method
->(params) do
updated_params = Oath.transform_params(params)
Oath.config.user_class.find_by(updated_params)
Oath.config.user_class.find_by(params)
end
end

Expand Down
19 changes: 19 additions & 0 deletions spec/features/user/user_signs_in_spec.rb
Expand Up @@ -11,4 +11,23 @@

expect(current_path).to eq posts_path
end

scenario 'with email or username' do
user = User.create!(
email: "example@example.com",
password_digest: "password",
username: "example",
)
allow(User).to receive(:where).with({ id: user.id }).and_call_original
allow(User).to receive(:where).with(
["email = ? OR username = ?", "example", "example"]
).and_return([user])

visit multiple_attributes_sign_in_path
fill_in "session_email_or_username", with: user.username
fill_in "session_password", with: 'password'
click_button 'go'

expect(current_path).to eq posts_path
end
end
@@ -0,0 +1,28 @@
class MultipleAttributesSessionsController < ApplicationController
def new
end

def create
user = authenticate_session(
session_params,
email_or_username: [:email, :username],
)

if sign_in(user)
redirect_to posts_path
else
redirect_to root_path, notice: "Invalid email or password"
end
end

def destroy
sign_out
redirect_to root_path
end

private

def session_params
params.require(:session).permit(:email_or_username, :password)
end
end
2 changes: 1 addition & 1 deletion spec/rails_app/app/models/user.rb
@@ -1,7 +1,7 @@
require 'active_hash'
class User < ActiveHash::Base
include ActiveModel::Validations
attr_accessor :email, :password_digest, :password
attr_accessor :email, :password_digest, :password, :username
validates :email, presence: true

def self.find_by(params)
Expand Down
@@ -0,0 +1,5 @@
<%= form_for :session do |f| %>
<%= f.text_field :email_or_username %>
<%= f.text_field :password %>
<%= f.submit 'go' %>
<% end %>
5 changes: 5 additions & 0 deletions spec/rails_app/config/routes.rb
Expand Up @@ -18,7 +18,12 @@
post "sign_in" => "sessions#create"
delete "sign_out" => "sessions#destroy"
get "sign_up" => "users#new"

get "invalid_sign_in" => "invalid_sessions#new"
post "invalid_sign_in" => "invalid_sessions#create"

get "multiple_attributes_sign_in" => "multiple_attributes_sessions#new"
post "multiple_attributes_sign_in" => "multiple_attributes_sessions#create"

get "basic_auth" => "basic_auth#show"
end