#21 Super Simple Authentication

The final piece of the administration puzzle: authentication. There are many different approaches which is why I saved this step for last. This episode will cover a few techniques including the simple solution used for this site.
# controllers/application.rb
def admin?
  session[:password] == 'foobar'
end

# sessions_controller.rb
def create
  session[:password] = params[:password]
  flash[:notice] = "Successfully logged in"
  redirect_to home_path
end

def destroy
  reset_session
  flash[:notice] = "Successfully logged out"
  redirect_to login_path
end

# config/routes.rb
map.resources :sessions, :episodes
map.home '', :controller => 'episodes', :action => 'index'
map.login 'login', :controller => 'sessions', :action => 'new'
map.logout 'logout', :controller => 'sessions', :action => 'destroy'

你可能感兴趣的:(Flash)