views:

95

answers:

1

I just got done setting up my app to work with authlogic following this exactly

before beginning I had already created a products catalog with basic CRUD functionality.

Right now I want the products catalog to only be accesible if the user is alread logged in.. so, basically if the user is not logged in it should go to the login page.. if he is, then localhost:3000 should take him to the products catalog.... m really confused...I dont even know what to do to logout...

Instead, right now, being logged in and going to localhost:3000 redirects me to http://localhost:3000/account

help please.

I have also noticed that if I go to my products catalog while being logged in I get this message on top f the page

"You must be logged out to access this page"

+1  A: 

Nacho, let me suggest you watch http://railscasts.com/episodes/160-authlogic

It should answer all your questions, and more.

Off the top of my head...

Start with setting up your routes if you have not already:

  map.login 'login', :controller => 'user_sessions', :action => 'new'

map.logout 'logout', :controller => 'user_sessions', :action => 'destroy'

Next, do this in your application controller:

  before_filter :authenticate, :except => [:login, :logout, :destroy, :index, :new]

  private #--------------------

  def authenticate
    unless current_user
      flash[:notice] = "You must be loged in first"
      redirect_to(login_url)
      return false
    end
  end

  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.record
  end

This should equip you to solve the issues mentioned above. If people are not logged in, they will be redirected to the login page. Also, to logout just point to logout_url (localhost:3000/logout)

MrCheetoDust