public
Description: List of Todos
Homepage: http://www.google.com
Clone URL: git://github.com/payalgupta/todo-list.git
Login/Logout Functionality
payalgupta (author)
Thu Jul 17 23:00:47 -0700 2008
commit  0619471b221332da38decda84383086bd4677c88
tree    8f41fa0353397bc5e20a0b8f51cbfc59a9eac07a
parent  b872fdc0794e35873244ad6b87e4bc080bd5b1b3
...
7
8
9
 
 
 
 
 
 
 
 
 
10
11
12
...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -7,6 +7,15 @@ class ApplicationController < ActionController::Base
0
   # See ActionController::RequestForgeryProtection for details
0
   # Uncomment the :secret if you're not using the cookie session store
0
   protect_from_forgery # :secret => '707a4952295d1c5b3fb29c6e90697724'
0
+
0
+  helper_method :current_user, :logged_in?
0
+  def current_user
0
+    @current_user ||= User.find_by_id(session[:user_id])
0
+  end
0
+
0
+  def logged_in?
0
+   current_user != nil
0
+  end
0
   
0
   # See ActionController::Base for details 
0
   # Uncomment this to filter the contents of submitted sensitive data parameters
...
36
37
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
40
41
...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
0
@@ -36,6 +36,23 @@ class LoginController < ApplicationController
0
   end
0
 
0
   def login
0
+    session[:user_id] = nil
0
+    if request.post?
0
+      user = User.authenticate(params[:username], params[:password])
0
+      if user
0
+        session[:user_id] = user.id
0
+        redirect_to(:action => "index", :controller => :login )
0
+      else
0
+        flash.now[:error] = "Enter valid username/password"
0
+        render(:action => "login" )
0
+      end
0
+    end 
0
+  end
0
+
0
+  def logout
0
+    session[:user_id] = nil
0
+    flash[:notice] = "You have successfully Logged out"
0
+    redirect_to(:action => "login" )    
0
   end
0
 
0
 end
...
2
3
4
 
 
5
6
7
...
18
19
20
 
 
 
 
 
 
 
 
 
 
 
21
22
23
...
2
3
4
5
6
7
8
9
...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
0
@@ -2,6 +2,8 @@ class User < ActiveRecord::Base
0
   require 'digest/sha1'
0
 
0
   validates_presence_of  :username, :firstname, :lastname, :email
0
+  validates_format_of :email,
0
+                      :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
0
   validates_uniqueness_of  :username, :email
0
   
0
   attr_accessor :password_confirmation
0
@@ -18,6 +20,17 @@ class User < ActiveRecord::Base
0
     self.hashed_password = User.encrypted_password(self.password, self.salt)
0
   end
0
 
0
+  def self.authenticate(username, password)
0
+    user = self.find_by_username(username)
0
+    if user
0
+      expected_password = encrypted_password(password, user.salt)
0
+      if user.hashed_password != expected_password
0
+        user = nil
0
+      end
0
+    end
0
+    user
0
+  end
0
+
0
   private
0
 
0
   def self.encrypted_password(password, salt)
...
1
2
 
 
...
1
 
2
3
0
@@ -1,2 +1,3 @@
0
 <h1>Login#index</h1>
0
-<p>Find me in app/views/login/index.html.erb</p>
0
+<p><%= "welcome #{current_user.username}, u r logged in!"%></p>
0
+<%= link_to 'Logout', :action => "logout", :controller => :login %>
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -1,2 +1,21 @@
0
-<h1>Login#login</h1>
0
-<p>Find me in app/views/login/login.html.erb</p>
0
+<div class="loginarea">
0
+  <fieldset>
0
+    <legend>Enter Login Details</legend>
0
+    <% form_tag do %>
0
+      <div>
0
+         <p>
0
+           <label for="username" class="left" style="width:40%;">UserName:</label>
0
+           <%= text_field_tag :username, params[:username] %>
0
+         </p>
0
+         <p>
0
+           <label for="password" class="left" style="width:40%;">Password:</label>
0
+           <%= password_field_tag :password, params[:password] %>
0
+         </p>
0
+       </div>
0
+       <p>
0
+         <%= submit_tag "Login" %>
0
+         <%= link_to 'NewUser', :action => "new", :controller => :login %>
0
+       </p>  
0
+    <% end %>
0
+  </fieldset>
0
+</div>

Comments