Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
Created January 30, 2016 19:04
Show Gist options
  • Save stevesohcot/51d93d68286a1ee1e95e to your computer and use it in GitHub Desktop.
Save stevesohcot/51d93d68286a1ee1e95e to your computer and use it in GitHub Desktop.
OmniAuth Identity - Sessions Controller
class SessionsController < ApplicationController
# to avoid a redirect loop, need to skip (exclude) it validating the autentication in this controller
skip_before_action :require_authentication
def create
user = User.from_omniauth(auth, params)
if logged_out?
# Run function to set cookies to remember the user logged in
remember_me(user)
# Actually log in the user
login(user)
redirect_to root_path
else # Already logged in
redirect_to root_path
return
end
end
def new
# if the user is NOT logged in,
# see if the cookies are set that should allow them to be logged in
if logged_out?
check_if_remembered
end
if logged_in?
redirect_to root_path
return
end
end
def failure
redirect_to login_path , alert: 'Invalid Login'
end
def destroy
session[:user_id] = nil
forget_me
redirect_to login_path , notice: 'Logged out!'
end
private
def auth
request.env['omniauth.auth']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment