simple_login 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- 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/version.rb +1 -1
- metadata +19 -4
@@ -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/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_login
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-05-03 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: simple-login
|
16
|
-
requirement: &
|
16
|
+
requirement: &70134171539760 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,13 +21,28 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
-
description:
|
24
|
+
version_requirements: *70134171539760
|
25
|
+
description: Use simple-login instead.
|
26
26
|
email: designium+simple@gmail.com
|
27
27
|
executables: []
|
28
28
|
extensions: []
|
29
29
|
extra_rdoc_files: []
|
30
30
|
files:
|
31
|
+
- lib/rails/generators/simple_login/simple_login_generator.rb
|
32
|
+
- lib/rails/generators/simple_login/templates/application_controller.rb
|
33
|
+
- lib/rails/generators/simple_login/templates/controllers/password_resets_controller.rb
|
34
|
+
- lib/rails/generators/simple_login/templates/controllers/sessions_controller.rb
|
35
|
+
- lib/rails/generators/simple_login/templates/controllers/users_controller.rb
|
36
|
+
- lib/rails/generators/simple_login/templates/create_users.rb
|
37
|
+
- lib/rails/generators/simple_login/templates/mailers/user_mailer.rb
|
38
|
+
- lib/rails/generators/simple_login/templates/models/user.rb
|
39
|
+
- lib/rails/generators/simple_login/templates/views/password_resets/edit.html.erb
|
40
|
+
- lib/rails/generators/simple_login/templates/views/password_resets/new.html.erb
|
41
|
+
- lib/rails/generators/simple_login/templates/views/sessions/new.html.erb
|
42
|
+
- lib/rails/generators/simple_login/templates/views/user_mailer/account_confirmation.text.erb
|
43
|
+
- lib/rails/generators/simple_login/templates/views/user_mailer/password_reset.text.erb
|
44
|
+
- lib/rails/generators/simple_login/templates/views/users/new.html.erb
|
45
|
+
- lib/rails/generators/simple_login/USAGE
|
31
46
|
- lib/simple_login/version.rb
|
32
47
|
- lib/simple_login.rb
|
33
48
|
- Gemfile
|