filling in everything needed to login and view the account page, wheeeee.

This commit is contained in:
Dan Buch 2011-08-09 22:03:10 -04:00
parent 7ca80880a8
commit b3d2c45c36
5 changed files with 50 additions and 5 deletions

View File

@ -1,3 +1,21 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
before_filter :set_user
protect_from_forgery protect_from_forgery
protected
def set_user
@user = User.find(session[:id]) if @user.nil? && session[:id]
end
def login_required
return true if @user
access_denied
return false
end
def access_denied
session[:return_to] = request.request_uri
flash[:error] = 'Oops. You need to login before you can view that page.'
redirect_to :controller => 'user', :action => 'login'
end
end end

View File

@ -1,11 +1,25 @@
class UserController < ApplicationController class UserController < ApplicationController
before_filter :login_required, :only => :my_account
def login def login
@user = User.new
@user.username = params[:username]
end end
def process_login def process_login
if user = User.authenticate(params[:user])
session[:id] = user.id
redirect_to session[:return_to] || '/'
else
flash[:error] = 'Invalid login.'
redirect_to :action => 'login', :username => params[:user][:username]
end
end end
def logout def logout
reset_session
flash[:message] = 'Logged out.'
redirect_to :action => 'login'
end end
def my_account def my_account

View File

@ -1,5 +1,5 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
validates_uniquness_of :username validates_uniqueness_of :username
validates_confirmation_of :password, :on => :create validates_confirmation_of :password, :on => :create
validates_length_of :password, :within => 5..40 validates_length_of :password, :within => 5..40

View File

@ -1,2 +1,14 @@
<h1>User#login</h1> <% if @flash %>
<p>Find me in app/views/user/login.html.erb</p> <% if @flash[:message] %>
<div id="flash-message"><%= @flash[:message] %></div>
<% end %>
<% if @flash[:error] %>
<div id="flash-error"><%= @flash[:error] %></div>
<% end %>
<% end %>
<%= form_tag :action => 'process_login' do %>
<label for="username">Username:</label> <%= text_field "user", "username" %>&#x00A;
<label for="password">Password:</label> <%= password_field "user", "password" %>&#x00A;
<%= submit_tag %>
<% end %>

View File

@ -1,2 +1,3 @@
<h1>User#my_account</h1> <h1>Account Info</h1>
<p>Find me in app/views/user/my_account.html.erb</p>
<p>Your username is <%= User.find(session[:id]).username %>