user_authentication 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ class ApplicationController < ActionController::Base
2
+ before_filter :set_current_user
3
+
4
+ private
5
+
6
+ def authorize
7
+ # Not using skip before_filter, since main app could inadvertantly override that.
8
+ # For example, it could have required the filter in its application_controller.
9
+
10
+ return true if params[:controller] == 'users' && params[:action] == 'login'
11
+
12
+ if current_user.nil?
13
+ session[:return_to] = url_for params
14
+ redirect_to root_path, alert: "Please log in to access this page."
15
+ end
16
+ end
17
+
18
+ def current_user
19
+ @current_user
20
+ end
21
+
22
+ def set_current_user
23
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
24
+ true
25
+ end
26
+
27
+ helper_method :current_user
28
+ end
@@ -0,0 +1,21 @@
1
+ class UsersController < ApplicationController
2
+ skip_before_filter :authorize, :only => [:login]
3
+
4
+ def login
5
+ user = User.find_by_email params[:email]
6
+ if user && user.authenticate(params[:password])
7
+ session[:user_id] = user.id
8
+ redirect_to session.delete(:return_to) || root_path
9
+ else
10
+ redirect_to root_path, alert: "Please check email and password."
11
+ end
12
+ end
13
+
14
+ def logout
15
+ session.delete :return_to
16
+ session.delete :user_id
17
+
18
+ redirect_to root_path
19
+ end
20
+ end
21
+
@@ -0,0 +1,5 @@
1
+ class User < ActiveRecord::Base
2
+ validates_presence_of :email
3
+
4
+ has_secure_password
5
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ match "login" => "users#login", :via => :post
3
+ match "logout" => "users#logout", :via => :get
4
+ end
@@ -0,0 +1,12 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :email, :null => false
5
+ t.string :password_digest
6
+
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :users, [:email]
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module UserAuthentication
2
+ class Engine < Rails::Engine
3
+ engine_name 'user_authentication'
4
+
5
+ initializer 'user_authentication_engine.app_controller' do |app|
6
+ ActiveSupport.on_load(:action_controller) do
7
+ require File.expand_path('../../app/controllers/user_authentication_application_controller', __FILE__)
8
+ end
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: user_authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sujoy Gupta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: sujoyg@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - app/controllers/user_authentication_application_controller.rb
21
+ - app/controllers/users_controller.rb
22
+ - app/models/user.rb
23
+ - config/routes.rb
24
+ - db/migrate/20121009010000_create_users.rb
25
+ - lib/user_authentication.rb
26
+ homepage: http://github.com/sujoyg/user_authentication
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.24
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: A rails engine for adding users and authentication to rails applications.
50
+ test_files: []