-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add authentication with JWT tokens in http header.
- Need to provide a Authentication header with a JWT token to access events_controller. - Add API endpoint to create users. - Add API endpoint to authenticate. - provide email and password to get token back.
- Loading branch information
1 parent
df38a1b
commit ad20bbe
Showing
21 changed files
with
405 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
class ApplicationController < ActionController::API | ||
AuthenticationError = Class.new(StandardError) | ||
|
||
class ApplicationController < ActionController::API | ||
include RecordInvalidHandler | ||
include RecordNotFoundHandler | ||
include Authentication | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class AuthenticationsController < ApplicationController | ||
|
||
def create | ||
@user = User.find_by(email: params[:email]) | ||
invalid_credentials if @user.nil? | ||
|
||
if @user.authenticate(params[:password]) | ||
render json: {'auth_token' => auth_token}.to_json | ||
else | ||
invalid_credentials | ||
end | ||
end | ||
|
||
private | ||
|
||
def auth_token | ||
JWT.encode({ | ||
user_id: @user.id, | ||
exp: 24.hours.from_now.to_i | ||
}, | ||
Rails.application.secrets.secret_key_base | ||
) | ||
end | ||
|
||
def invalid_credentials | ||
raise AuthenticationError, 'Invalid credentials' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Authentication | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
|
||
rescue_from AuthenticationError do |e| | ||
message = { message: e.message } | ||
render json: message, status: :unauthorized | ||
end | ||
|
||
attr_reader :current_user | ||
|
||
def load_current_user | ||
raise AuthenticationError, 'Token required' if auth_header.blank? | ||
@current_user = User.find( get_user_id ) | ||
rescue JWT::DecodeError => e | ||
raise AuthenticationError, 'Invalid token' | ||
end | ||
|
||
private | ||
|
||
def get_user_id | ||
JWT.decode(auth_header, secret_key)[0]['user_id'] | ||
end | ||
|
||
def auth_header | ||
request.headers['Authorization'] | ||
end | ||
|
||
def secret_key | ||
Rails.application.secrets.secret_key_base | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class UsersController < ApplicationController | ||
|
||
def create | ||
User.create!(user_params) | ||
render json: {message: 'Account created'}, status: :created | ||
end | ||
|
||
private | ||
|
||
def user_params | ||
params.require(:user).permit(:full_name, :email, :password) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
class User < ApplicationRecord | ||
|
||
has_secure_password | ||
|
||
validates :email, presence: true | ||
|
||
acts_as_human | ||
|
||
has_many :events, foreign_key: 'organiser_id' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ class EventSerializer < ActiveModel::Serializer | |
:description | ||
|
||
belongs_to :group | ||
belongs_to :organiser | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,8 @@ | |
|
||
root to: 'events#index' | ||
|
||
post 'login', to: 'authentications#create' | ||
|
||
resources :users | ||
resources :events | ||
end |
10 changes: 10 additions & 0 deletions
10
db/migrate/20180122130957_add_email_and_password_digest_to_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AddEmailAndPasswordDigestToUsers < ActiveRecord::Migration[5.1] | ||
|
||
def change | ||
add_column :users, :email, :string | ||
change_column_null :users, :email, false | ||
|
||
add_column :users, :password_digest, :string | ||
change_column_null :users, :password_digest, false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddOrganiserToEvents < ActiveRecord::Migration[5.1] | ||
|
||
def change | ||
add_column :events, :organiser_id, :integer, foreign_key: true | ||
change_column_null :events, :organiser_id, false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
FactoryGirl.define do | ||
|
||
factory :user do | ||
full_name 'Nelson Mandela' | ||
full_name 'kurt russell' | ||
email '[email protected]' | ||
password 'password' | ||
|
||
factory :organiser do | ||
full_name 'Carl Cox' | ||
email '[email protected]' | ||
password 'techno' | ||
end | ||
|
||
factory :someone_else do | ||
full_name 'Someone Else' | ||
email '[email protected]' | ||
password 'strange' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Authentications", type: :request do | ||
|
||
describe "POST /authentications" do | ||
let(:user) { create(:user) } | ||
|
||
let(:valid) do | ||
{ email: user.email, password: 'password' } | ||
end | ||
|
||
let(:app_json) do | ||
{"Content-Type" => "application/json"} | ||
end | ||
|
||
context 'logging in' do | ||
before do | ||
expect(JWT).to receive(:encode).with( | ||
{ | ||
user_id: user.id, | ||
exp: 24.hours.from_now.to_i | ||
}, | ||
Rails.application.secrets.secret_key_base | ||
).and_return 'TOKEN' | ||
end | ||
before { post '/login', params: valid.to_json, headers: app_json } | ||
|
||
it 'returns an auth_token' do | ||
expect( json['auth_token'] ).to eq 'TOKEN' | ||
end | ||
end | ||
|
||
context 'An email that does NOT exist' do | ||
let(:invalid_email) { valid.merge(email: '[email protected]') } | ||
|
||
before { post '/login', params: invalid_email.to_json, headers: app_json } | ||
|
||
it 'should error' do | ||
expect( response ).to have_http_status(401) | ||
expect( response.body ).to match /Invalid credentials/ | ||
end | ||
end | ||
|
||
context 'The wrong password' do | ||
let(:wrong_pass) { valid.merge(password: 'wrong') } | ||
|
||
before { post '/login', params: wrong_pass.to_json, headers: app_json } | ||
|
||
it 'should error' do | ||
expect( response ).to have_http_status(401) | ||
expect( response.body ).to match /Invalid credentials/ | ||
end | ||
end | ||
|
||
context 'A blank password' do | ||
let(:blank_pass) { valid.merge(password: '') } | ||
|
||
before { post '/login', params: blank_pass.to_json, headers: app_json } | ||
|
||
it 'should error' do | ||
expect( response ).to have_http_status(401) | ||
expect( response.body ).to match /Invalid credentials/ | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.