hanami-authentication 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/README.md +23 -0
- data/hanami-authentication.gemspec +1 -0
- data/lib/hanami/authentication.rb +30 -6
- data/lib/hanami/authentication/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b10f080ce3f404155ac3587c85b888ad678a21a
|
4
|
+
data.tar.gz: 43441401575e80b96f300cead6fe346fe63d17c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 183dcd04ce198d45cc797252f1a18971bd6fcebbb7d4f18ee2b03dd313ff217072612c100a44d703f63d0a7b4d6258717ab260a3d53bbd9de29b9eef1825ec2f
|
7
|
+
data.tar.gz: b87f623a4b60bec0727850cd3a5be0b818939086c42f2e6de59bb7ecbdfb41d1fff7d9c954e5c023796837b566cf414e8b4c890e43b58bc65201fd7a24399766
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.1
|
data/README.md
CHANGED
@@ -15,6 +15,29 @@ gem 'hanami-authentication'
|
|
15
15
|
|
16
16
|
## Usage
|
17
17
|
|
18
|
+
### Setup
|
19
|
+
```ruby
|
20
|
+
# application.rb
|
21
|
+
controller.prepare do
|
22
|
+
include Hanami::Authentication
|
23
|
+
# before authenticate >> This will use callbacks if authentication is failed.
|
24
|
+
# before authenticate! >> This forced to halt by 401 if authentication is failed.
|
25
|
+
|
26
|
+
after_session_expired do # This will be called if authenticate method is called when session is expired.
|
27
|
+
flash[:error] = 'The session is expired.'
|
28
|
+
redirect_to routes.root_path
|
29
|
+
end
|
30
|
+
|
31
|
+
after_session_failed do # This will be called if authenticate method is called when user has not logged in.
|
32
|
+
flash[:error] = 'Please login'
|
33
|
+
redirect_to routes.root_path
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
|
39
|
+
### Methods
|
40
|
+
|
18
41
|
TBD
|
19
42
|
|
20
43
|
## Contributing
|
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
25
|
spec.add_dependency 'bcrypt', '~> 3.1'
|
26
|
+
spec.add_dependency 'hanami-utils', '~> 1.0'
|
26
27
|
|
27
28
|
spec.add_development_dependency 'bundler', '~> 1.15'
|
28
29
|
spec.add_development_dependency 'rake', '~> 10.0'
|
@@ -7,14 +7,12 @@ module Hanami
|
|
7
7
|
private
|
8
8
|
|
9
9
|
def login(user, remember_me: false, expire_seconds: 1 * 60 * 60)
|
10
|
-
return if authenticated?
|
11
10
|
session[:current_user] = user
|
12
11
|
session[:session_created_at] = Time.now + expire_seconds
|
13
12
|
session[:remember_me] = remember_me
|
14
13
|
end
|
15
14
|
|
16
15
|
def logout
|
17
|
-
return unless authenticated?
|
18
16
|
session[:current_user] = nil
|
19
17
|
session[:session_created_at] = nil
|
20
18
|
session[:remember_me] = nil
|
@@ -42,11 +40,9 @@ module Hanami
|
|
42
40
|
def authenticate
|
43
41
|
if session_expired?
|
44
42
|
logout
|
45
|
-
|
46
|
-
redirect_to routes.root_path
|
43
|
+
self.class.after_session_expired_callbacks.run(self)
|
47
44
|
elsif !authenticated?
|
48
|
-
|
49
|
-
redirect_to routes.root_path
|
45
|
+
self.class.after_authentication_failed_callbacks.run(self)
|
50
46
|
end
|
51
47
|
end
|
52
48
|
|
@@ -57,5 +53,33 @@ module Hanami
|
|
57
53
|
def session_expired?
|
58
54
|
!session[:remember_me] && session[:session_expired_at] && session[:session_expired_at] < Time.now
|
59
55
|
end
|
56
|
+
|
57
|
+
def self.included(base)
|
58
|
+
base.class_eval do
|
59
|
+
extend ClassMethods
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
module ClassMethods
|
64
|
+
def self.extended(base)
|
65
|
+
base.class_eval do
|
66
|
+
include Utils::ClassAttribute
|
67
|
+
|
68
|
+
class_attribute :after_session_expired_callbacks
|
69
|
+
self.after_session_expired_callbacks = Utils::Callbacks::Chain.new
|
70
|
+
|
71
|
+
class_attribute :after_authentication_failed_callbacks
|
72
|
+
self.after_authentication_failed_callbacks = Utils::Callbacks::Chain.new
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def after_session_expired(*callbacks, &blk)
|
77
|
+
after_session_expired_callbacks.append(*callbacks, &blk)
|
78
|
+
end
|
79
|
+
|
80
|
+
def after_authentication_failed(*callbacks, &blk)
|
81
|
+
after_authentication_failed_callbacks.append(*callbacks, &blk)
|
82
|
+
end
|
83
|
+
end
|
60
84
|
end
|
61
85
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hanami-authentication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LegalForce Inc.
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hanami-utils
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,6 +88,7 @@ extensions: []
|
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
76
90
|
- ".gitignore"
|
91
|
+
- ".ruby-version"
|
77
92
|
- ".travis.yml"
|
78
93
|
- CODE_OF_CONDUCT.md
|
79
94
|
- Gemfile
|
@@ -103,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
118
|
version: '0'
|
104
119
|
requirements: []
|
105
120
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.
|
121
|
+
rubygems_version: 2.6.11
|
107
122
|
signing_key:
|
108
123
|
specification_version: 4
|
109
124
|
summary: A simple authentication module for hanami.
|