Skip to content

Commit

Permalink
So, now you can log in to the system and you'll be redirected back to…
Browse files Browse the repository at this point in the history
… where you were trying to go
  • Loading branch information
joshe committed Aug 14, 2011
1 parent c09bb4f commit ac439d6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ GEM
mime-types (~> 1.16) mime-types (~> 1.16)
treetop (~> 1.4.8) treetop (~> 1.4.8)
mime-types (1.16) mime-types (1.16)
mysql2 (0.2.7) mysql2 (0.2.11)
polyglot (0.3.1) polyglot (0.3.1)
rack (1.2.3) rack (1.2.3)
rack-mount (0.6.14) rack-mount (0.6.14)
Expand Down
18 changes: 12 additions & 6 deletions app/controllers/access_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,12 +1,14 @@
class AccessController < ApplicationController class AccessController < ApplicationController


before_filter :confirm_logged_in, :except => [:login, :attempt_login, :logout]

def index def index
menu menu
render 'menu' render 'menu'
end end


def new def new
@user = User.new @user = User.new
end end


def create def create
Expand All @@ -28,19 +30,23 @@ def login
end end


def attempt_login def attempt_login
authorized_user = User.authenticate(params[:username], params[:password]) @incomingController = params[:controller]
@incomingAction = params[:action]

authorized_user = User.authenticate(params[:email], params[:password])
if authorized_user if authorized_user
# TODO: mark user as logged in session[:user_id] = authorized_user.id
flash[:message] = "You are now logged in." session[:email] = authorized_user.email
redirect_to :action => 'menu' redirect_back(:action => 'index')
else else
flash[:message] = "Invalid username/password combination" flash[:message] = "Invalid username/password combination"
redirect_to :action => 'login' redirect_to :action => 'login'
end end
end end


def logout def logout
# TODO: mark user as logged out session[:user_id] = nil
session[:email] = nil
flash[:message] = "You have been logged out." flash[:message] = "You have been logged out."
redirect_to :action => "login" redirect_to :action => "login"
end end
Expand Down
30 changes: 30 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,33 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery protect_from_forgery

# redirect somewhere that will eventually return back to here
def redirect_away(*params)
session[:original_uri] = request.request_uri
redirect_to(*params)
end

# returns the person to either the original url from a redirect_away or to a default url
def redirect_back(*params)
uri = session[:original_uri]
session[:original_uri] = nil
if uri
redirect_to uri
else
redirect_to(*params)
end
end

protected

def confirm_logged_in
unless session[:user_id]
flash[:message] = "Please log in."
redirect_away(:controller => "access", :action => "login")
return false # halts the before_filter
else
return true
end
end

end end
3 changes: 3 additions & 0 deletions app/controllers/events_controller.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,10 @@
class EventsController < ApplicationController class EventsController < ApplicationController


before_filter :confirm_logged_in

def index def index
@events = Event.all @events = Event.all
session[:return_to] ||= request.referer
end end


def new def new
Expand Down
4 changes: 2 additions & 2 deletions app/views/access/login.html.haml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
= form_tag :action => "attempt_login" do = form_tag :action => "attempt_login" do
%table %table
%tr %tr
%td= label_tag(:username) %td= label_tag(:email)
%td= text_field_tag(:username) %td= text_field_tag(:email)
%tr %tr
%td= label_tag(:password) %td= label_tag(:password)
%td= password_field_tag(:password) %td= password_field_tag(:password)
Expand Down
1 change: 1 addition & 0 deletions app/views/access/menu.html.haml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


.menu .menu
%h2 Admin menu %h2 Admin menu
.username= "Logged in as #{session[:email]}"
%ul.identity %ul.identity
%li= link_to "Manage events", events_path %li= link_to "Manage events", events_path
%li= link_to "Manage users", "#" %li= link_to "Manage users", "#"
Expand Down
4 changes: 3 additions & 1 deletion app/views/layouts/_header.html.haml
Original file line number Original file line Diff line number Diff line change
@@ -1 +1,3 @@
%h1 Header %h1 Header
- unless session[:user_id].nil?
= link_to 'Logout', :controller => "access", :action => "logout"

0 comments on commit ac439d6

Please sign in to comment.