Skip to content

Commit 03e8c03

Browse files
committed
Merge pull request #1 from undercase/master
merge from uppercase master
2 parents d25ea7d + 476ce2f commit 03e8c03

13 files changed

Lines changed: 92 additions & 16 deletions

File tree

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ For other authentication solutions or custom Devise setups, you must first use p
3939

4040
`authenticate:` specifies the name of the method on the User model which accepts a password to authenticate the user. This is the name of an instance method on the User model which accepts the `password` param, and returns either true or false to indicate authentication.
4141

42+
Proof also allows for a optional block that returns a hash to modify the json return
43+
44+
proof_actions authenticatable: :User do |user, token|
45+
{
46+
user_id: user.id,
47+
email: user.email,
48+
auth_token: token
49+
}
50+
end
51+
4252
When your application sends a `POST` request to the `login` action, it will return JSON with the key `auth_token` if it finds a valid user. Your application must then save this token and send it with every request under the `Authorization` HTTP header, in the Bearer format: `Bearer [token]`.
4353

4454
You must route the `login` action yourself. For example, if you had a controller named `AuthenticationController`, you could create a `/login` route like so:
4555

46-
post '/login' => 'authentication#login'
56+
post '/login', to: 'authentication#login'
4757

4858
In order to restrict an action to authenticated users, simply use a `before_action` call for the actions you'd like to restrict:
4959

lib/generators/proof/devise/devise_generator.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ module Proof
22
module Generators
33
class DeviseGenerator < Rails::Generators::NamedBase
44
source_root File.expand_path('../templates', __FILE__)
5+
argument :class_name, type: :string, default: "User"
56
desc "This generator creates a 'login' route, and configures it to work with Devise (with the default model name User)."
67

78
def create_controller
8-
controller_file = "authentication_controller.rb"
9+
controller_file = "authentication_controller.rb.erb"
910
destination_file = "app/controllers/authentication_controller.rb"
10-
copy_file controller_file, destination_file
11-
gsub_file destination_file, 'authenticatable_class_name', name
11+
template controller_file, destination_file
1212
end
1313

1414
def create_routes
15-
route "post 'login' => 'authentication#login'"
15+
route "post 'login', to: 'authentication#login'"
1616
end
1717
end
1818
end

lib/generators/proof/devise/templates/authentication_controller.rb

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class AuthenticationController < ApplicationController
2+
proof_actions authenticatable: :<%= class_name %>, authenticate: :valid_password?
3+
end

lib/proof/proof_actions.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ module ProofActions
66
end
77

88
module ClassMethods
9-
def proof_actions(options={})
9+
def proof_actions(options={}, &block)
1010
options[:authenticatable] ||= :User
1111
options[:identifier] ||= :email
1212
options[:password] ||= :password
1313
options[:authenticate] ||= :authenticate
14+
options[:block] = nil
15+
if block_given?
16+
options[:block] = block
17+
end
1418
cattr_accessor :proof_options
1519
self.proof_options = options
1620
include Proof::ProofActions::LocalInstanceMethods
@@ -20,9 +24,15 @@ def proof_actions(options={})
2024
module LocalInstanceMethods
2125
def login
2226
proof_class = self.class.proof_options[:authenticatable].to_s.camelize.constantize
23-
user = proof_class.find_by(self.class.proof_options[:identifier] => params[:identifier])
27+
identifier = self.class.proof_options[:identifier]
28+
user = proof_class.find_by(identifier => params[identifier])
2429
if user && user.send(self.class.proof_options[:authenticate], params[self.class.proof_options[:password]])
25-
render json: { auth_token: Proof::Token.from_data({ user_id: user.id }) }
30+
auth_token = Proof::Token.from_data({ user_id: user.id })
31+
json = { auth_token: auth_token }
32+
if !self.class.proof_options[:block].nil?
33+
json = self.class.proof_options[:block].call(user, auth_token)
34+
end
35+
render json: json
2636
else
2737
render json: { error: "Invalid Credentials." }, status: :unauthorized
2838
end

lib/proof/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Proof
2-
VERSION = "1.1.1"
2+
VERSION = "1.1.2"
33
end

test/block_actions_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'json'
2+
require 'test_helper'
3+
4+
class BlockActionsTest < ActionController::TestCase
5+
def setup
6+
User.create(email: 'real@email.com', password: 'realpassword')
7+
8+
@controller = BlockController.new
9+
@request = ActionController::TestRequest.new
10+
@response = ActionController::TestResponse.new
11+
12+
Rails.application.routes.draw do
13+
post 'login', to: 'block#login'
14+
end
15+
end
16+
17+
def test_proof_actions_block_works
18+
post :login, { 'email' => 'real@email.com', 'password' => 'realpassword' }
19+
response = JSON.parse(@response.body)
20+
21+
assert_response :success
22+
23+
assert_not_nil response['user_id']
24+
assert_not_nil response['email']
25+
assert_not_nil response['auth_token']
26+
end
27+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Place all the behaviors and hooks related to the matching controller here.
2+
// All this logic will automatically be available in application.js.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Place all the styles related to the matching controller here.
3+
They will automatically be included in application.css.
4+
*/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class BlockController < ApplicationController
2+
proof_actions authenticatable: :User do |user, token|
3+
{
4+
user_id: user.id,
5+
email: user.email,
6+
auth_token: token
7+
}
8+
end
9+
before_action :require_proof, except: :login
10+
11+
def test
12+
render json: { status: 'authorized' }
13+
end
14+
end

0 commit comments

Comments
 (0)