Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
app/models/user/omniauth_callbacks.rb
class User
module OmniauthCallbacks
["slack"].each do |provider|
define_method "find_or_create_for_#{provider}" do |response|
uid = response["uid"]
data = response["info"]
if identity = Authorization.where("provider" => provider, "uid" => uid).first
user = identity.user
elsif user = User.find_by_email(data["email"])
user.bind_service(response)
user
else
user = User.new_from_provider_data(provider, uid, data)
if user.save(validate: false)
user.authorizations << Authorization.new(provider: provider, uid: uid)
if response["credentials"]["token"].present?
Rails.logger.warn response["credentials"]["token"]
user.update(:slack_token => response["credentials"]["token"] )
end
team = Team.find_or_create_by(:slack_id => response["extra"]["raw_info"]["team_info"]["team"]["id"])
user.teams << team
team.update(:name => response["extra"]["raw_info"]["team_info"]["team"]["name"])
return user
else
Rails.logger.warn("User.create_from_hash 失败,#{user.errors.inspect}")
return nil
end
end
end
end
def new_from_provider_data(provider, uid, data)
User.new do |user|
user.email = data["email"]
user.name = data["name"]
user.slack_id = uid
user.password = Devise.friendly_token[0, 20]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.