sms-pager/app/controllers/oauths_controller.rb

56 lines
1.2 KiB
Ruby

class OauthsController < ApplicationController
skip_before_action :require_login
def oauth
login_at(params[:provider])
end
def callback
provider = params[:provider]
@user = login_from(provider)
if @user
redirect_to root_path, notice: login_msg(@user, provider)
else
@user = auth_and_login(provider)
redirect_to root_path, notice: login_msg(@user, provider)
end
end
private
def login_msg user, provider = 'oAuth'
if user.active?
"Logged in from #{provider.titleize}!"
else
"Your account must be activated by an administrator."
end
end
def auth_and_login provider
user = create_auth_from(provider, auth_info)
reset_session # protect from session fixation attack
auto_login(user) if user.active?
user
end
def create_auth_from provider, auth
user = Person.find_by_email auth[:user_info]["email"]
user.authentications.create(provider: provider, uid: auth[:uid])
user
end
def auth_info
@auth_info ||= google_hash
end
def google_hash
ga = Sorcery::Providers::Google.new
ga.get_user_hash access_token
end
def auth_params
params.permit(:code, :provider)
end
end