2011-10-11 21:56:51 +01:00
|
|
|
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
2013-03-25 23:13:15 +00:00
|
|
|
|
|
|
|
def facebook
|
|
|
|
oauthorize 'Facebook'
|
|
|
|
end
|
|
|
|
|
|
|
|
def google_oauth2
|
|
|
|
oauthorize 'google_oauth2'
|
|
|
|
end
|
|
|
|
|
|
|
|
def github
|
|
|
|
oauthorize 'GitHub'
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def passthru
|
2014-01-21 04:51:49 +00:00
|
|
|
render file: "#{Rails.root}/public/404.html", status: 404, layout: false
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
|
2013-03-25 23:13:15 +00:00
|
|
|
private
|
2011-10-11 21:56:51 +01:00
|
|
|
|
2013-03-25 23:13:15 +00:00
|
|
|
def oauthorize(kind)
|
|
|
|
provider = kind.downcase
|
|
|
|
@user = find_for_ouath(env["omniauth.auth"], current_user)
|
2013-03-26 13:30:43 +00:00
|
|
|
if @user && @user.persisted?
|
2014-01-21 04:51:49 +00:00
|
|
|
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", kind: action_name.classify
|
|
|
|
sign_in_and_redirect @user, event: :authentication
|
2013-03-25 23:13:15 +00:00
|
|
|
else
|
|
|
|
session["devise.#{provider}_data"] = env["omniauth.auth"]
|
|
|
|
redirect_to new_user_registration_url
|
2014-01-21 04:51:49 +00:00
|
|
|
end
|
2013-03-25 23:13:15 +00:00
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
|
2013-03-25 23:13:15 +00:00
|
|
|
def find_for_ouath(auth, resource=nil)
|
|
|
|
provider, uid = auth['provider'], auth['uid']
|
|
|
|
authentication = Authentication.find_or_initialize_by_provider_and_uid(provider, uid)
|
2011-10-11 21:56:51 +01:00
|
|
|
if authentication.new_record?
|
|
|
|
if user_signed_in? # New authentication method for current_user
|
|
|
|
authentication.user = current_user
|
2011-10-13 11:19:42 +01:00
|
|
|
else # Register new user from session
|
2013-03-25 23:13:15 +00:00
|
|
|
case provider
|
|
|
|
when 'facebook'
|
|
|
|
name = auth['extra']['raw_info']['name']
|
2013-03-26 13:35:02 +00:00
|
|
|
when 'google_oauth2', 'github'
|
|
|
|
name = auth['info']['nickname'] || auth['info']['name']
|
2013-03-25 23:13:15 +00:00
|
|
|
else
|
|
|
|
raise 'Provider #{provider} not handled'
|
|
|
|
end
|
2013-03-26 13:35:02 +00:00
|
|
|
user = User.find_or_initialize_by_email(auth['info']['email'])
|
|
|
|
if user.new_record?
|
|
|
|
user.name = name
|
|
|
|
user.uname = name.gsub(/\s/, '').underscore
|
|
|
|
user.password = Devise.friendly_token[0,20]
|
|
|
|
user.confirmed_at = Time.zone.now
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
authentication.user = user
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|
2013-03-26 13:30:43 +00:00
|
|
|
authentication.save
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|
2013-03-26 12:24:15 +00:00
|
|
|
return authentication.user
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|