Merge pull request #253 from differentreality/final_openid
Authenticate via openid
This commit is contained in:
commit
0c698bac10
24 changed files with 412 additions and 15 deletions
|
|
@ -1,6 +1,10 @@
|
|||
class RegistrationsController < Devise::RegistrationsController
|
||||
before_action :configure_permitted_parameters, if: :devise_controller?
|
||||
|
||||
def edit
|
||||
@openids = Openid.where(user_id: current_user.id).order(:provider)
|
||||
end
|
||||
|
||||
def update
|
||||
@user = User.find(current_user.id)
|
||||
email_changed = false
|
||||
|
|
|
|||
34
app/controllers/users/omniauth_callbacks_controller.rb
Normal file
34
app/controllers/users/omniauth_callbacks_controller.rb
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
module Users
|
||||
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
||||
skip_before_filter :verify_authenticity_token
|
||||
|
||||
[:novell, :google, :facebook, :twitter].each do |provider|
|
||||
define_method(provider) { handle(provider) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def handle(provider)
|
||||
auth_hash = request.env['omniauth.auth']
|
||||
openid = Openid.find_for_oauth(auth_hash) # Get or create openid
|
||||
# If openid exists and is associated with a user, sign in with associated user,
|
||||
# even if the email of the associated user and the email of the provided openid are different
|
||||
unless user = openid.user
|
||||
user = User.find_for_auth(auth_hash, current_user) # Get or create users
|
||||
end
|
||||
|
||||
begin
|
||||
user.save!
|
||||
if openid.user != user
|
||||
openid.user = user
|
||||
end
|
||||
openid.save!
|
||||
|
||||
sign_in user
|
||||
redirect_to root_path, notice: user.email + " signed in successfully with #{provider}"
|
||||
rescue Exception => e
|
||||
redirect_back_or_to new_user_registration_path, alert: 'Failed' + e.message
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue