authentication 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a79cf272b8049c3c5c4079363518bea601ef966
4
- data.tar.gz: ebcdb430bc467c2586e1ac3d9adc2787c3a25996
3
+ metadata.gz: b741f3ded1d07c5ac43f416625e8eae8b38e9e0f
4
+ data.tar.gz: f0e964c8170feb59eb40e609c3ea08b05539e9ab
5
5
  SHA512:
6
- metadata.gz: 1504438b306f6a02d00b1aab3122fa95833b0a74bfe03b6187ef70974ed37850e220d47106cb4ed77ea06e1ae3077ee2c55c596aa811aa65fcf5365a348958de
7
- data.tar.gz: 2597a069244ef5ffde178d66d2005ce1f037447f3e690747e61448b9e4ea90d65b6b1a1abf84497cfdb63f63b9c3174bd7c0bfcbd5cfe6ff0b378e632c0753c5
6
+ metadata.gz: fbd5f60b0e1ce9db7c2530bc59e59815f4604f80be98108964abf6cf0be21e56b70d6d888372a9af77352f3e9f7a791e0e4bc10eecdfd1e6ea501df01f0d4b97
7
+ data.tar.gz: 6d32c98f9d298145fe1ef817a8d4f75e0f7c643760491c52d73c044ffe99028a053da52f9a43380a17cf3349936727c8cbfb052a615262742e05b2c88d0364af
data/.gitignore CHANGED
@@ -16,3 +16,5 @@ tmp
16
16
  .yardoc
17
17
  _yardoc
18
18
  doc/
19
+
20
+ Gemfile.lock
@@ -1,4 +1,9 @@
1
1
  language: ruby
2
+
3
+ before_install:
4
+ - gem update --system
5
+ - gem install bundler --pre
6
+
2
7
  rvm:
3
8
  - 1.9.3
4
9
  - 2.0.0
@@ -6,5 +11,3 @@ rvm:
6
11
  - 2.2
7
12
  - jruby-19mode
8
13
  - rbx-19mode
9
-
10
- sudo: false
@@ -0,0 +1,3 @@
1
+ # 0.0.3
2
+
3
+ - Add callback after `#login!` and `#logout!`.
data/README.md CHANGED
@@ -18,6 +18,8 @@ or add ```gem 'authentication'``` to your Gemfile.
18
18
  - `#login!` and `#logout!` to log in/out.
19
19
  - `#current_user` and `#current_user_id` to get current user or its id.
20
20
  - `#logged_in?` to ask logged in or not.
21
+ - `#after_login` will be invoked after `#login!`.
22
+ - `#after_logout` will be invoked after `#logout!`.
21
23
 
22
24
  [Example](https://github.com/fujimura/authentication_rails_example) and [spec](https://github.com/fujimura/authentication/blob/master/spec/lib/authentication_spec.rb) might be also helpful.
23
25
 
@@ -21,4 +21,5 @@ Gem::Specification.new do |gem|
21
21
 
22
22
  gem.add_development_dependency "rake"
23
23
  gem.add_development_dependency "rspec"
24
+ gem.add_development_dependency "pry"
24
25
  end
@@ -24,7 +24,9 @@ module Authentication
24
24
  @__authenticator ||= Authentication::Authenticator.new(
25
25
  session: session,
26
26
  session_key: :current_user_id,
27
- finder: -> { find_current_user }
27
+ finder: -> { find_current_user },
28
+ after_login_callback: -> { self.respond_to?(:after_login, true) && after_login },
29
+ after_logout_callback: -> { self.respond_to?(:after_logout, true) && after_logout }
28
30
  )
29
31
  end
30
32
  end
@@ -6,10 +6,14 @@ module Authentication
6
6
  # @option options [Hash] :session Hash-like session object.
7
7
  # @option options [Symbol] :session_key Key of session_id.
8
8
  # @option options [Proc] :finder A proc which returns client to authenticate.
9
+ # @option options [Proc] :after_login_callback A proc which will be invoked after {#login!}.
10
+ # @option options [Proc] :after_logout_callback A proc which will be invoked after {#logout}.
9
11
  def initialize(options)
10
12
  @session = options.fetch :session
11
13
  @session_key = options.fetch :session_key
12
14
  @finder = options.fetch :finder
15
+ @after_login_callback = options[:after_login_callback]
16
+ @after_logout_callback = options[:after_logout_callback]
13
17
  end
14
18
 
15
19
  # Set current_client.
@@ -19,6 +23,7 @@ module Authentication
19
23
  raise Unauthenticated unless client
20
24
  @current_client = client
21
25
  @session[@session_key] = client.id
26
+ invoke_after_login_callback!
22
27
  end
23
28
 
24
29
  # Return current_client.
@@ -51,6 +56,17 @@ module Authentication
51
56
  def logout!
52
57
  return unless current_client
53
58
  @current_client = @session[@session_key] = nil
59
+ invoke_after_logout_callback!
60
+ end
61
+
62
+ private
63
+
64
+ def invoke_after_login_callback!
65
+ @after_login_callback && @after_login_callback.call
66
+ end
67
+
68
+ def invoke_after_logout_callback!
69
+ @after_logout_callback && @after_logout_callback.call
54
70
  end
55
71
  end
56
72
  end
@@ -1,3 +1,3 @@
1
1
  module Authentication
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'ostruct'
2
3
 
3
4
  class Controller
4
5
  include Authentication
@@ -15,22 +16,33 @@ class Controller
15
16
  nil
16
17
  end
17
18
  end
18
- end
19
19
 
20
- class User
21
- def initialize(attributes)
22
- @attributes = attributes
20
+ private
21
+
22
+ def after_login
23
23
  end
24
- def id
25
- @attributes[:id]
24
+
25
+ def after_logout
26
26
  end
27
27
  end
28
28
 
29
29
  describe Authentication do
30
30
  let(:controller) { Controller.new }
31
- let(:user) { User.new(id: 300) }
31
+ let(:user) { OpenStruct.new(id: rand(100)) }
32
32
 
33
33
  describe '#login!' do
34
+ context 'with client' do
35
+ it "should set current user" do
36
+ controller.login! user
37
+ expect(controller.current_user).to eq user
38
+ end
39
+
40
+ it "should invoke after_login callback" do
41
+ expect(controller).to receive(:after_login)
42
+ controller.login! user
43
+ end
44
+ end
45
+
34
46
  context 'with nil' do
35
47
  it "should raise Unauthenticated" do
36
48
  expect do
@@ -86,6 +98,12 @@ describe Authentication do
86
98
  expect(controller.current_user).to eq nil
87
99
  expect(controller.session[:current_user_id]).to eq nil
88
100
  end
101
+
102
+ it "should invoke after_logout callback" do
103
+ expect(controller).to receive(:after_logout)
104
+ controller.login! user
105
+ controller.logout!
106
+ end
89
107
  end
90
108
  end
91
109
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authentication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fujimura Daisuke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-24 00:00:00.000000000 Z
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Minimalist authentication library for Ruby
42
56
  email:
43
57
  - me@fujimuradaisuke.com
@@ -47,8 +61,8 @@ extra_rdoc_files: []
47
61
  files:
48
62
  - ".gitignore"
49
63
  - ".travis.yml"
64
+ - CHANGELOG.md
50
65
  - Gemfile
51
- - Gemfile.lock
52
66
  - LICENSE.txt
53
67
  - README.md
54
68
  - Rakefile
@@ -77,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
91
  version: '0'
78
92
  requirements: []
79
93
  rubyforge_project: authentication
80
- rubygems_version: 2.4.5.1
94
+ rubygems_version: 2.5.1
81
95
  signing_key:
82
96
  specification_version: 4
83
97
  summary: Minimalist authentication library for Ruby
@@ -1,34 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- authentication (0.0.2)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- diff-lcs (1.2.5)
10
- rake (10.4.2)
11
- rspec (3.4.0)
12
- rspec-core (~> 3.4.0)
13
- rspec-expectations (~> 3.4.0)
14
- rspec-mocks (~> 3.4.0)
15
- rspec-core (3.4.1)
16
- rspec-support (~> 3.4.0)
17
- rspec-expectations (3.4.0)
18
- diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.4.0)
20
- rspec-mocks (3.4.0)
21
- diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.4.0)
23
- rspec-support (3.4.0)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- authentication!
30
- rake
31
- rspec
32
-
33
- BUNDLED WITH
34
- 1.10.6