jt-rails-generator-user 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +3 -0
- data/LICENSE +19 -0
- data/README.md +44 -0
- data/jt-rails-generator-user.gemspec +13 -0
- data/lib/generators/jt/user/USAGE +3 -0
- data/lib/generators/jt/user/templates/controllers/concerns/current_user.rb +29 -0
- data/lib/generators/jt/user/templates/controllers/sessions_controller.rb +24 -0
- data/lib/generators/jt/user/templates/controllers/users_controller.rb +49 -0
- data/lib/generators/jt/user/templates/mailers/user_mailer.rb +8 -0
- data/lib/generators/jt/user/templates/models/concerns/user_authentication.rb +41 -0
- data/lib/generators/jt/user/templates/models/user.rb +5 -0
- data/lib/generators/jt/user/templates/views/sessions/new.html.erb +8 -0
- data/lib/generators/jt/user/templates/views/user_mailer/reset_password.html.erb +1 -0
- data/lib/generators/jt/user/templates/views/users/new.html.erb +8 -0
- data/lib/generators/jt/user/templates/views/users/password_forgot.html.erb +7 -0
- data/lib/generators/jt/user/user_generator.rb +28 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0f5b69d33209d4864f7bd83f4fca4af58259746f
|
4
|
+
data.tar.gz: fa045b65a2bd51a7599a36c56ad7c904b318ad97
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a1049df6a02d21388947ccd4bc19f7bf1cad9ec9c2d0b792920b65eb10d933514ccbb0faac0c18e875e0a5eac364cb550df403bc56448fd1840bfdcd628df881
|
7
|
+
data.tar.gz: 48e1a357772036978a584b29b91db88a717ac641188bd7ead43a4231c47ce71d52e893517c5d46bd74ad0f1adaa8c0db37d03f3293bc095d879cb0bbcbc7e3c8
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2015 Jonathan Tribouharet
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# JTRailsGeneratorUser
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/jt-rails-generator-user)
|
4
|
+
|
5
|
+
JTRailsGeneratorUser is a generator for user authentication. You have the login and sign up page, password forgot feature.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
JTRailsGeneratorUser is distributed as a gem, which is how it should be used in your app.
|
10
|
+
|
11
|
+
Include the gem in your Gemfile:
|
12
|
+
|
13
|
+
gem 'jt-rails-generator-user', '~> 1.0'
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
You just have to use the generator with:
|
18
|
+
|
19
|
+
rails g jt:user
|
20
|
+
|
21
|
+
Include `CurrentUser` in your `ApplicationController`:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class ApplicationController < ActionController::Base
|
25
|
+
include CurrentUser
|
26
|
+
...
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
## What's does it generate?
|
31
|
+
|
32
|
+
- `User` model with only email, password and password_token (for password forgot feature)
|
33
|
+
- `SessionController` for the login
|
34
|
+
- `UsersController` for the sign up and password forgot feature
|
35
|
+
- `UserMailer` for sending password reset instructions
|
36
|
+
- `CurrentUser` module which manage `current_user` variable and `require_user` filter
|
37
|
+
|
38
|
+
## Author
|
39
|
+
|
40
|
+
- [Jonathan Tribouharet](https://github.com/jonathantribouharet) ([@johntribouharet](https://twitter.com/johntribouharet))
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
JTRailsGeneratorUser is released under the MIT license. See the LICENSE file for more info.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'jt-rails-generator-user'
|
3
|
+
s.summary = "Generate a scaffold for user authentication in Ruby On Rails"
|
4
|
+
s.description = "JTRailsGeneratorUser is a generator for user authentication. You have the login and sign up page, password forgot feature."
|
5
|
+
s.homepage = 'https://github.com/jonathantribouharet/jt-rails-generator-user'
|
6
|
+
s.version = '1.0.0'
|
7
|
+
s.files = `git ls-files`.split("\n")
|
8
|
+
s.require_paths = ['lib']
|
9
|
+
s.authors = ['Jonathan TRIBOUHARET']
|
10
|
+
s.email = 'jonathan.tribouharet@gmail.com'
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.platform = Gem::Platform::RUBY
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module CurrentUser
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
helper_method :current_user
|
6
|
+
end
|
7
|
+
|
8
|
+
def set_current_user(user)
|
9
|
+
session[:user_id] = user.id
|
10
|
+
end
|
11
|
+
|
12
|
+
def current_user
|
13
|
+
@current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
|
14
|
+
@current_user
|
15
|
+
end
|
16
|
+
|
17
|
+
def require_user
|
18
|
+
if !current_user
|
19
|
+
redirect_to login_url
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def require_no_user
|
24
|
+
if current_user
|
25
|
+
redirect_to root_url
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class SessionsController < ApplicationController
|
2
|
+
|
3
|
+
before_action :require_no_user, except: [:destroy]
|
4
|
+
|
5
|
+
def new
|
6
|
+
end
|
7
|
+
|
8
|
+
def create
|
9
|
+
user = User.authenticate(params[:email], params[:password])
|
10
|
+
|
11
|
+
if user
|
12
|
+
set_current_user(user)
|
13
|
+
redirect_to root_url
|
14
|
+
else
|
15
|
+
render :new
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def destroy
|
20
|
+
reset_session
|
21
|
+
redirect_to root_url
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class UsersController < ApplicationController
|
2
|
+
|
3
|
+
before_action :require_no_user
|
4
|
+
|
5
|
+
def new
|
6
|
+
@user = User.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def create
|
10
|
+
@user = User.new(user_params)
|
11
|
+
if @user.save
|
12
|
+
set_current_user(@user)
|
13
|
+
redirect_to root_url
|
14
|
+
else
|
15
|
+
render :new
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def password_forgot
|
20
|
+
if request.post?
|
21
|
+
@user = User.search_by_email_for_authentication(params[:email]).first
|
22
|
+
if @user
|
23
|
+
@user.generate_new_password_token!
|
24
|
+
UserMailer.reset_password(@user.id).deliver_later
|
25
|
+
|
26
|
+
redirect_to root_url
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def reset_password
|
32
|
+
user = User.where(password_token: params[:token]).first
|
33
|
+
if user
|
34
|
+
set_current_user(user)
|
35
|
+
user.clear_password_token!
|
36
|
+
|
37
|
+
redirect_to root_url
|
38
|
+
else
|
39
|
+
redirect_to root_url
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def user_params
|
46
|
+
params.require(:user).permit(:email, :password)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module UserAuthentication
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
|
6
|
+
has_secure_password(validations: false)
|
7
|
+
|
8
|
+
validates :email, presence: true, email_format: true, uniqueness: { case_sensitive: false}
|
9
|
+
validates :password_token, uniqueness: true, if: Proc.new {|u| !u.password_token.blank? }
|
10
|
+
|
11
|
+
before_save :downcase_email
|
12
|
+
|
13
|
+
scope :search_by_email_for_authentication, ->(email = nil) { where(email: email.to_s.downcase).where('password_digest IS NOT NULL') }
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
|
19
|
+
def authenticate(email, password)
|
20
|
+
self.search_by_email_for_authentication(email).first.try(:authenticate, password)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def downcase_email
|
26
|
+
self.email.downcase! if self.email
|
27
|
+
end
|
28
|
+
|
29
|
+
def generate_new_password_token!
|
30
|
+
self.password_token = loop do
|
31
|
+
random_token = SecureRandom.urlsafe_base64(128, false)
|
32
|
+
break random_token unless self.class.exists?(password_token: random_token)
|
33
|
+
end
|
34
|
+
self.save!
|
35
|
+
end
|
36
|
+
|
37
|
+
def clear_password_token!
|
38
|
+
self.update_column(:password_token, nil)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= link_to reset_password_users_url(@user.password_token), reset_password_users_url(@user.password_token) %>
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Jt
|
2
|
+
class UserGenerator < Rails::Generators::Base
|
3
|
+
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
def create_initializer_file
|
7
|
+
generate "migration", "CreateUsers", "email:string password_digest:string password_token:string"
|
8
|
+
|
9
|
+
directory 'controllers', 'app/controllers'
|
10
|
+
directory 'mailers', 'app/mailers'
|
11
|
+
directory 'models', 'app/models'
|
12
|
+
directory 'views', 'app/views'
|
13
|
+
|
14
|
+
route "resources :users, only: [:new, :create] do
|
15
|
+
collection do
|
16
|
+
get 'password_forgot' => 'users#password_forgot', as: :password_forgot
|
17
|
+
post 'password_forgot' => 'users#password_forgot'
|
18
|
+
|
19
|
+
get 'reset_password/:token' => 'users#reset_password', as: :reset_password
|
20
|
+
end
|
21
|
+
end"
|
22
|
+
|
23
|
+
route "get 'logout' => 'sessions#destroy', as: :logout"
|
24
|
+
route "post 'login' => 'sessions#create'"
|
25
|
+
route "get 'login' => 'sessions#new', as: :login"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jt-rails-generator-user
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan TRIBOUHARET
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: JTRailsGeneratorUser is a generator for user authentication. You have
|
14
|
+
the login and sign up page, password forgot feature.
|
15
|
+
email: jonathan.tribouharet@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- jt-rails-generator-user.gemspec
|
25
|
+
- lib/generators/jt/user/USAGE
|
26
|
+
- lib/generators/jt/user/templates/controllers/concerns/current_user.rb
|
27
|
+
- lib/generators/jt/user/templates/controllers/sessions_controller.rb
|
28
|
+
- lib/generators/jt/user/templates/controllers/users_controller.rb
|
29
|
+
- lib/generators/jt/user/templates/mailers/user_mailer.rb
|
30
|
+
- lib/generators/jt/user/templates/models/concerns/user_authentication.rb
|
31
|
+
- lib/generators/jt/user/templates/models/user.rb
|
32
|
+
- lib/generators/jt/user/templates/views/sessions/new.html.erb
|
33
|
+
- lib/generators/jt/user/templates/views/user_mailer/reset_password.html.erb
|
34
|
+
- lib/generators/jt/user/templates/views/users/new.html.erb
|
35
|
+
- lib/generators/jt/user/templates/views/users/password_forgot.html.erb
|
36
|
+
- lib/generators/jt/user/user_generator.rb
|
37
|
+
homepage: https://github.com/jonathantribouharet/jt-rails-generator-user
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.0.14
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Generate a scaffold for user authentication in Ruby On Rails
|
61
|
+
test_files: []
|