Skip to content

Commit 3b0cff9

Browse files
committed
修正 Omniauth 登陆的错误;
1 parent ffb71db commit 3b0cff9

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

app/controllers/users_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def auth_unbind
5050
return
5151
end
5252

53-
current_user.authorizations.destroy_all(provider: provider)
53+
current_user.authorizations.where(provider: provider).delete_all
5454
redirect_to edit_user_registration_path, flash: { warring: t('users.unbind_success', provider: provider.titleize) }
5555
end
5656

app/models/authorization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Authorization < ActiveRecord::Base
2+
belongs_to :user
23
validates :uid, :provider, presence: true
34
validates :uid, uniqueness: { scope: :provider }
45
end

app/models/user/omniauth_callbacks.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ module OmniauthCallbacks
55
uid = response['uid'].to_s
66
data = response['info']
77

8-
if (user = User.where('authorizations.provider' => provider, 'authorizations.uid' => uid).first)
8+
if (user = Authorization.find_by(provider: provider, uid: uid).try(:user))
99
user
1010
else
1111
user = User.new_from_provider_data(provider, uid, data)
1212

1313
if user.save(validate: false)
14-
user.authorizations << Authorization.new(provider: provider, uid: uid)
14+
Authorization.find_or_create_by(provider: provider, uid: uid, user_id: user.id)
1515
return user
1616
else
1717
Rails.logger.warn("User.create_from_hash 失败,#{user.errors.inspect}")

0 commit comments

Comments
 (0)