Skip to content

Commit

Permalink
Allow users to sign in with their e-mail address
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwillms committed Aug 23, 2016
1 parent 0b70c7b commit c4fb914
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def authenticate(password)
end

def self.from_token_request(request)
login = request.params['auth'] && request.params['auth']['login']
active.find_by login: login
return nil unless request.params['auth'] && request.params['auth']['login']
credential = request.params['auth']['login']
active.where(['login = ? OR email = ?', credential, credential]).first
end

def self.from_token_payload(payload)
logger.fatal payload.inspect
active.find_by id: (payload['sub'] || payload[:sub])
end

Expand Down
17 changes: 16 additions & 1 deletion spec/controllers/user_token_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe UserTokenController do
describe '#create' do
context 'with valid credentials' do
context 'with valid login credentials' do
before(:each) do
user = create(:user)
post :create, params: {auth: {login: user.login, password: 'password'}}
Expand All @@ -17,6 +17,21 @@
end
end

context 'with valid email credentials' do
before(:each) do
user = create(:user)
post :create, params: {auth: {login: user.email, password: 'password'}}
end

it 'returns JWT token' do
expect(json_response).to have_key('jwt')
end

it 'has a 200 status' do
expect(response.status).to be(201)
end
end

context 'with invalid credentials' do
it 'has a 404 status for unconfirmed account' do
user = create(:user, email_confirmed: false)
Expand Down
9 changes: 9 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@
expect(User.from_token_request(request)).to eq(user)
end

it 'finds record by email based on params[auth][login]' do
user = create(:user)
request = instance_double(ActionDispatch::Request)
allow(request).to receive(:params).and_return(
'auth' => { 'login' => user.email }
)
expect(User.from_token_request(request)).to eq(user)
end

it 'returns nil if cannot find by params[auth][login]' do
create(:user)
request = instance_double(ActionDispatch::Request)
Expand Down

0 comments on commit c4fb914

Please sign in to comment.