simple-login 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +48 -0
- data/Rakefile +2 -0
- data/lib/rails/generators/simple_login/USAGE +6 -0
- data/lib/rails/generators/simple_login/simple_login_generator.rb +49 -0
- data/lib/rails/generators/simple_login/templates/application_controller.rb +15 -0
- data/lib/rails/generators/simple_login/templates/controllers/password_resets_controller.rb +25 -0
- data/lib/rails/generators/simple_login/templates/controllers/sessions_controller.rb +25 -0
- data/lib/rails/generators/simple_login/templates/controllers/users_controller.rb +17 -0
- data/lib/rails/generators/simple_login/templates/create_users.rb +13 -0
- data/lib/rails/generators/simple_login/templates/mailers/user_mailer.rb +18 -0
- data/lib/rails/generators/simple_login/templates/models/user.rb +32 -0
- data/lib/rails/generators/simple_login/templates/views/password_resets/edit.html.erb +35 -0
- data/lib/rails/generators/simple_login/templates/views/password_resets/new.html.erb +20 -0
- data/lib/rails/generators/simple_login/templates/views/sessions/new.html.erb +31 -0
- data/lib/rails/generators/simple_login/templates/views/user_mailer/account_confirmation.text.erb +5 -0
- data/lib/rails/generators/simple_login/templates/views/user_mailer/password_reset.text.erb +5 -0
- data/lib/rails/generators/simple_login/templates/views/users/new.html.erb +40 -0
- data/lib/simple_login.rb +6 -0
- data/lib/simple_login/version.rb +3 -0
- metadata +68 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Chim Kan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# SimpleLogin
|
2
|
+
|
3
|
+
simple_login creates a basic and simple login system for Rails 3 apps. It is based on Railscasts Authentication from Scratch videos.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_login'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_login
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Once you've added the gem into your Gemfile, you only need to run the following command:
|
22
|
+
|
23
|
+
rails g simple_login
|
24
|
+
|
25
|
+
Rails will generate the login and view files.
|
26
|
+
|
27
|
+
Then run:
|
28
|
+
|
29
|
+
rake db:migrate
|
30
|
+
|
31
|
+
The user table will be created.
|
32
|
+
|
33
|
+
I also recommend activating the root_path.
|
34
|
+
|
35
|
+
To add the links to your app just add the following paths:
|
36
|
+
|
37
|
+
signup_path
|
38
|
+
login_path
|
39
|
+
logout_path
|
40
|
+
password_reset_path
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module SimpleLogin
|
2
|
+
module Generators
|
3
|
+
class SimpleLoginGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
|
7
|
+
def generate_user
|
8
|
+
# Copy the controllers for user, sessions and password_reset
|
9
|
+
directory "controllers", "app/controllers/"
|
10
|
+
directory "mailers", "app/mailers/"
|
11
|
+
directory "models", "app/models/"
|
12
|
+
directory "views", "app/views/"
|
13
|
+
end
|
14
|
+
|
15
|
+
def insert_general_methods
|
16
|
+
inject_into_file "app/controllers/application_controller.rb", after: "protect_from_forgery" do
|
17
|
+
a = "\n\n private\n\n def current_user\n"
|
18
|
+
b = " @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]\n"
|
19
|
+
c = " end\n"
|
20
|
+
d = "\n helper_method :current_user\n\n"
|
21
|
+
e = " def authorize\n"
|
22
|
+
f = " redirect_to login_url, alert: 'Not authorized. Please login.' if current_user.nil?\n"
|
23
|
+
g = " end\n"
|
24
|
+
a+b+c+d+e+f+g
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def insert_routes
|
29
|
+
route("match 'signup', to: 'users#new', as: 'signup'")
|
30
|
+
route("match 'login', to: 'sessions#new', as: 'login'")
|
31
|
+
route("match 'logout', to: 'sessions#destroy', as: 'logout'")
|
32
|
+
route("resources :users")
|
33
|
+
route("resources :sessions")
|
34
|
+
route("resources :password_resets")
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_user
|
38
|
+
generate("model", "users email:string password_digest:string auth_token:string password_reset_token:string password_reset_sent_at:datetime")
|
39
|
+
rake("db:migrate")
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_gems
|
43
|
+
gem("bcrypt-ruby")
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class ApplicationController < ActionController::Base
|
2
|
+
protect_from_forgery
|
3
|
+
|
4
|
+
private #PRIVATE PRIVATE PRIVATE
|
5
|
+
|
6
|
+
def current_user
|
7
|
+
@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
|
8
|
+
end
|
9
|
+
|
10
|
+
helper_method :current_user
|
11
|
+
|
12
|
+
def authorize
|
13
|
+
redirect_to login_url, alert: "Not authorized. Please login." if current_user.nil?
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class PasswordResetsController < ApplicationController
|
2
|
+
def new
|
3
|
+
end
|
4
|
+
|
5
|
+
def create
|
6
|
+
user = User.find_by_email(params[:email])
|
7
|
+
user.send_password_reset if user
|
8
|
+
redirect_to root_url, :notice => "Email sent with password reset instructions."
|
9
|
+
end
|
10
|
+
|
11
|
+
def edit
|
12
|
+
@user = User.find_by_password_reset_token!(params[:id])
|
13
|
+
end
|
14
|
+
|
15
|
+
def update
|
16
|
+
@user = User.find_by_password_reset_token!(params[:id])
|
17
|
+
if @user.password_reset_sent_at < 2.hours.ago
|
18
|
+
redirect_to new_password_reset_path, :alert => "Password reset has expired."
|
19
|
+
elsif @user.update_attributes(params[:user])
|
20
|
+
redirect_to root_url, :notice => "Password has been reset!"
|
21
|
+
else
|
22
|
+
render :edit
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class SessionsController < ApplicationController
|
2
|
+
def new
|
3
|
+
end
|
4
|
+
|
5
|
+
def create
|
6
|
+
user = User.find_by_email(params[:email])
|
7
|
+
if user && user.authenticate(params[:password])
|
8
|
+
if params[:remember_me]
|
9
|
+
cookies.permanent[:auth_token] = user.auth_token
|
10
|
+
else
|
11
|
+
cookies[:auth_token] = user.auth_token
|
12
|
+
end
|
13
|
+
redirect_to root_url, :notice => "Logged in!"
|
14
|
+
else
|
15
|
+
flash.now.alert = "Invalid email or password"
|
16
|
+
render "new"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def destroy
|
21
|
+
cookies.delete(:auth_token)
|
22
|
+
redirect_to root_url, :notice => "Logged out!"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class UsersController < ApplicationController
|
2
|
+
|
3
|
+
def new
|
4
|
+
@user = User.new
|
5
|
+
end
|
6
|
+
|
7
|
+
def create
|
8
|
+
@user = User.new(params[:user])
|
9
|
+
if @user.save
|
10
|
+
cookies[:auth_token] = @user.auth_token
|
11
|
+
redirect_to root_url
|
12
|
+
else
|
13
|
+
render "new"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CreateUsers < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :users do |t|
|
4
|
+
t.string :email
|
5
|
+
t.string :password_digest
|
6
|
+
t.string :auth_token
|
7
|
+
t.string :password_reset_token
|
8
|
+
t.datetime :password_reset_sent_at
|
9
|
+
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class UserMailer < ActionMailer::Base
|
2
|
+
default from: "your_email@example.com"
|
3
|
+
|
4
|
+
# Subject can be set in your I18n file at config/locales/en.yml
|
5
|
+
# with the following lookup:
|
6
|
+
#
|
7
|
+
# en.user_mailer.password_reset.subject
|
8
|
+
#
|
9
|
+
def password_reset(user)
|
10
|
+
@user = user
|
11
|
+
mail to: user.email, subject: "Password Reset"
|
12
|
+
end
|
13
|
+
|
14
|
+
def account_confirmation(user)
|
15
|
+
@user = user
|
16
|
+
mail to: @user.email, subject: "Welcome!"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
|
3
|
+
has_secure_password
|
4
|
+
|
5
|
+
attr_accessible :email, :password, :password_confirmation
|
6
|
+
|
7
|
+
has_many :posts
|
8
|
+
|
9
|
+
validates_uniqueness_of :email
|
10
|
+
validates_presence_of :email
|
11
|
+
|
12
|
+
before_create { generate_token(:auth_token) }
|
13
|
+
after_create { send_account_confirmation }
|
14
|
+
|
15
|
+
def send_password_reset
|
16
|
+
generate_token(:password_reset_token)
|
17
|
+
self.password_reset_sent_at = Time.zone.now
|
18
|
+
save!
|
19
|
+
UserMailer.password_reset(self).deliver
|
20
|
+
end
|
21
|
+
|
22
|
+
def send_account_confirmation
|
23
|
+
UserMailer.account_confirmation(self).deliver
|
24
|
+
end
|
25
|
+
|
26
|
+
def generate_token(column)
|
27
|
+
begin
|
28
|
+
self[column] = SecureRandom.urlsafe_base64
|
29
|
+
end while User.exists?(column => self[column])
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
<!-- Edit password Non Modal -->
|
2
|
+
<div class="container">
|
3
|
+
<div class="row center">
|
4
|
+
<h1 style="margin-bottom: 20px;">Change Password</h1>
|
5
|
+
</div>
|
6
|
+
<div class="row">
|
7
|
+
<div class="span4 offset4 well">
|
8
|
+
<%= form_for @user, :url => password_reset_path(params[:id]), html: {class: "form"} do |f| %>
|
9
|
+
<% if @user.errors.any? %>
|
10
|
+
<div class="alert alert-error">
|
11
|
+
<h2>Form is invalid</h2>
|
12
|
+
<ul>
|
13
|
+
<% for message in @user.errors.full_messages %>
|
14
|
+
<li><%= message %></li>
|
15
|
+
<% end %>
|
16
|
+
</ul>
|
17
|
+
</div>
|
18
|
+
<% end %>
|
19
|
+
<fieldset>
|
20
|
+
<legend>Change Password</legend>
|
21
|
+
<div class="control-group">
|
22
|
+
<%= f.label :password, nil ,class: "control-label" %>
|
23
|
+
<div class="controls"><%= f.password_field :password %></div>
|
24
|
+
</div>
|
25
|
+
<div class="control-group">
|
26
|
+
<%= f.label :password_confirmation, nil ,class: "control-label" %>
|
27
|
+
<div class="controls"><%= f.password_field :password_confirmation %></div>
|
28
|
+
</div>
|
29
|
+
<div class="actions"><%= f.submit "Update Password", class: "btn btn-large btn-primary" %></div>
|
30
|
+
</fieldset>
|
31
|
+
<% end %>
|
32
|
+
</div>
|
33
|
+
</div>
|
34
|
+
</div>
|
35
|
+
<!-- End of Edit Password Non Modal -->
|
@@ -0,0 +1,20 @@
|
|
1
|
+
<!-- Reset Non Modal -->
|
2
|
+
|
3
|
+
<div class="container">
|
4
|
+
<div class="row">
|
5
|
+
<div class="span4 offset4 well">
|
6
|
+
<%= form_tag password_resets_path, :method => :post do %>
|
7
|
+
<fieldset>
|
8
|
+
<legend>Reset Password</legend>
|
9
|
+
<div class="control-group">
|
10
|
+
<%= label_tag :email, nil, class: "control-label" %>
|
11
|
+
<div class="controls"><%= text_field_tag :email, params[:email] %></div>
|
12
|
+
</div>
|
13
|
+
<div class="actions"><%= submit_tag t("user.reset_password", default: "Reset password"), class: "btn btn-large btn-primary" %></div>
|
14
|
+
</fieldset>
|
15
|
+
<% end %>
|
16
|
+
</div>
|
17
|
+
</div>
|
18
|
+
</div>
|
19
|
+
|
20
|
+
<!-- End of Reset Non Modal -->
|
@@ -0,0 +1,31 @@
|
|
1
|
+
<!-- Login Non Modal -->
|
2
|
+
|
3
|
+
<div class="container">
|
4
|
+
<div class="row">
|
5
|
+
<div class="span4 offset4 well">
|
6
|
+
<%= form_tag (sessions_path), class: "form" do %>
|
7
|
+
<fieldset>
|
8
|
+
<legend>Login</legend>
|
9
|
+
<div class="control-group">
|
10
|
+
<%= label_tag :email, nil ,class: "control-label" %>
|
11
|
+
<div class="controls"><%= text_field_tag :email, params[:email], class: "input-xlarge" %></div>
|
12
|
+
</div>
|
13
|
+
<div class="control-group">
|
14
|
+
<%= label_tag :password, nil, class: "control-label" %>
|
15
|
+
<div class="controls"><%= password_field_tag (:password), nil ,class: "input-xlarge" %></div>
|
16
|
+
</div>
|
17
|
+
<div class="control-group">
|
18
|
+
<%= label_tag :remember_me, nil, class: "control-label" %>
|
19
|
+
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
|
20
|
+
</div>
|
21
|
+
<div class="actions"><%= submit_tag t("user.log_in", default: "Log in"), class: "btn btn-large btn-primary" %></div>
|
22
|
+
</fieldset>
|
23
|
+
<% end %>
|
24
|
+
<%= link_to t("user.forgotten_password", default: "Forgotten password?"), new_password_reset_path %> - <%= link_to t("user.sign_up", default: "Sign Up"), new_user_path %>
|
25
|
+
|
26
|
+
|
27
|
+
</div>
|
28
|
+
</div>
|
29
|
+
</div>
|
30
|
+
|
31
|
+
<!-- End of Login Non Modal -->
|
@@ -0,0 +1,40 @@
|
|
1
|
+
<!-- Sign UP Non Modal -->
|
2
|
+
<div class="container">
|
3
|
+
<div class="row center">
|
4
|
+
<h1 style="margin-bottom: 20px;">Sign Up</h1>
|
5
|
+
</div>
|
6
|
+
<div class="row">
|
7
|
+
<div class="span4 offset4 well">
|
8
|
+
<%= form_for @user, html: {class: "form"} do |f| %>
|
9
|
+
<% if @user.errors.any? %>
|
10
|
+
<div class="alert alert-error">
|
11
|
+
<h2>Form is invalid</h2>
|
12
|
+
<ul>
|
13
|
+
<% @user.errors.full_messages.each do |message| %>
|
14
|
+
<li><%= message %></li>
|
15
|
+
<% end %>
|
16
|
+
</ul>
|
17
|
+
</div>
|
18
|
+
<% end %>
|
19
|
+
<fieldset>
|
20
|
+
<legend>Sign Up</legend>
|
21
|
+
<div class="control-group">
|
22
|
+
<%= f.label :email, nil ,class: "control-label" %>
|
23
|
+
<div class="controls"><%= f.text_field :email, class: "input-xlarge" %></div>
|
24
|
+
</div>
|
25
|
+
<div class="control-group">
|
26
|
+
<%= f.label :password, nil ,class: "control-label" %>
|
27
|
+
<div class="controls"><%= f.password_field :password, class: "input-xlarge" %></div>
|
28
|
+
</div>
|
29
|
+
<div class="control-group">
|
30
|
+
<%= f.label :password_confirmation, nil ,class: "control-label" %>
|
31
|
+
<div class="controls"><%= f.password_field :password_confirmation, class: "input-xlarge" %></div>
|
32
|
+
</div>
|
33
|
+
|
34
|
+
<div class="actions"><%= f.submit t("user.sign_up", default: "Sign Up"), class: "btn btn-large btn-primary" %></div>
|
35
|
+
</fieldset>
|
36
|
+
<% end %>
|
37
|
+
</div>
|
38
|
+
</div>
|
39
|
+
</div>
|
40
|
+
<!-- End of Sing UP Non Modal -->
|
data/lib/simple_login.rb
ADDED
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-login
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chim Kan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-03 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: simple_login creates a basic and simple login system for Rails 3 apps.
|
15
|
+
It is based on Railscasts Authentication from Scratch videos.
|
16
|
+
email:
|
17
|
+
- designium@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/rails/generators/simple_login/simple_login_generator.rb
|
23
|
+
- lib/rails/generators/simple_login/templates/application_controller.rb
|
24
|
+
- lib/rails/generators/simple_login/templates/controllers/password_resets_controller.rb
|
25
|
+
- lib/rails/generators/simple_login/templates/controllers/sessions_controller.rb
|
26
|
+
- lib/rails/generators/simple_login/templates/controllers/users_controller.rb
|
27
|
+
- lib/rails/generators/simple_login/templates/create_users.rb
|
28
|
+
- lib/rails/generators/simple_login/templates/mailers/user_mailer.rb
|
29
|
+
- lib/rails/generators/simple_login/templates/models/user.rb
|
30
|
+
- lib/rails/generators/simple_login/templates/views/password_resets/edit.html.erb
|
31
|
+
- lib/rails/generators/simple_login/templates/views/password_resets/new.html.erb
|
32
|
+
- lib/rails/generators/simple_login/templates/views/sessions/new.html.erb
|
33
|
+
- lib/rails/generators/simple_login/templates/views/user_mailer/account_confirmation.text.erb
|
34
|
+
- lib/rails/generators/simple_login/templates/views/user_mailer/password_reset.text.erb
|
35
|
+
- lib/rails/generators/simple_login/templates/views/users/new.html.erb
|
36
|
+
- lib/rails/generators/simple_login/USAGE
|
37
|
+
- lib/simple_login/version.rb
|
38
|
+
- lib/simple_login.rb
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- Rakefile
|
42
|
+
- README.md
|
43
|
+
homepage: ''
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.11
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: simple_login can be used easily by adding the gem into your Gemfile and type
|
67
|
+
the following command 'rails g simple_login'.
|
68
|
+
test_files: []
|