Skip to content

Instantly share code, notes, and snippets.

@gamov
Created March 3, 2011 04:55
Show Gist options
  • Save gamov/852351 to your computer and use it in GitHub Desktop.
Save gamov/852351 to your computer and use it in GitHub Desktop.
session controller
class SessionsController < ApplicationController
skip_before_filter :login_required, :only => [:new, :create]
layout nil
def new
render #:layout => nil
end
def create
user = User.authenticate(params[:username], params[:password])
if user
session[:user_id] = user.id
logger.info "User '#{user.username} logged in"
flash[:notice]= "Welcome back #{user.name}!"
redirect_to_target_or_default(root_url)
# redirect_to root_url, :notice => "Logged in!"
else
logger.info "Failed login: u=#{params[:username]}"
flash.now[:alert]= "Invalid username or password"
render :action => 'new' #, :layout => nil
end
# puts current_user.inspect
end
def destroy
logger.info "User '#{current_user.username} logged out" if current_user
session[:user_id] = nil
redirect_to login_url, :notice => "Logged out, bye."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment