Created
August 21, 2013 20:02
-
-
Save bleakwood/6299451 to your computer and use it in GitHub Desktop.
a simple controller to allow user sign in and sign in w/ Facebook and google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserSessionsController < ApplicationController | |
skip_before_filter :require_login, :only => [:new, :create, :authenticate] | |
rescue_from User::NotActivated do |e| | |
flash.now.alert = "You are not activted" | |
render :not_activated | |
end | |
rescue_from User::InvalidPassword do |e| | |
flash.now.alert = "Invalid email or password" | |
render :new | |
end | |
rescue_from User::EmptyPassword do |e| | |
redirect_to new_password_reset_url, :notice => "Password Empty. Please fill out the form below to set up a password." | |
end | |
def new | |
# Set proper redirect url for ajax requests. | |
# It is probably better to store the :return_to hash by request type | |
session[:return_to] = request.referer unless ( request.referer.nil? || request.referer.include?("password_resets"))# Sets | |
redirect_to dashboard_users_path if current_user | |
end | |
def create | |
@user = User.find_by_email(params[:email]) | |
if @user && @user.authenticate_user(params[:password]) | |
store_remember_me | |
session[:user_id] = @user.id | |
@user.increase_login_count | |
@user.update_ip(request) | |
respond_to do |format| | |
format.html { redirect_back_or_default(dashboard_users_path) } | |
format.json { render :json => @user.to_json } | |
end | |
else | |
raise User::InvalidPassword | |
end | |
end | |
def authenticate | |
omniauth = request.env["omniauth.auth"] | |
authentication = Account.find_by_account_type_and_uid(omniauth['provider'], omniauth['uid']) | |
if authentication | |
session[:user_id] = authentication.user.id | |
authentication.user.increase_login_count | |
authentication.user.update_ip(request) | |
redirect_back_or_default(dashboard_users_path) | |
else | |
user = User.find_by_email(omniauth['info']['email']) | |
if user | |
session[:user_id] = user.id | |
user.accounts.create(:account_type => omniauth['provider'], :uid => omniauth['uid'], :credentials => {:token => omniauth['credentials']['token']}) | |
user.increase_login_count | |
user.update_ip(request) | |
redirect_back_or_default(dashboard_users_path) | |
else | |
user = User.new | |
if user.apply_omniauth(omniauth) | |
session[:user_id] = user.reload.id | |
redirect_back_or_default(dashboard_users_path) | |
else | |
session[:omniauth] = omniauth['info'] | |
redirect_to sign_up_path, :notice => "Hi #{omniauth['info']['first_name']}, Please fill out the missing fields to finish sign up." | |
end | |
end | |
end | |
end | |
def destroy | |
remove_remember_me | |
reset_session | |
redirect_to sign_in_path, :notice => "Signed out!" | |
end | |
private | |
def store_remember_me | |
cookies.permanent[:auth_token] = @user.auth_token if params[:remember_me] | |
end | |
def remove_remember_me | |
cookies.delete(:auth_token) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment