Created
June 17, 2018 10:36
-
-
Save xdite/80391cf4c5258652666c3dfaaa595ca0 to your computer and use it in GitHub Desktop.
app/controllers/users/omniauth_callbacks_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def self.provides_callback_for(*providers) | |
providers.each do |provider| | |
class_eval %{ | |
def #{provider} | |
Rails.logger.warn(request.env["omniauth.auth"]) | |
if not current_user.blank? | |
current_user.bind_service(request.env["omniauth.auth"])#Add an auth to existing | |
redirect_to edit_user_registration_path, :notice => "成功绑定了 #{provider} 帐号。" | |
else | |
@user = User.find_or_create_for_#{provider}(request.env["omniauth.auth"]) | |
if @user.persisted? | |
flash[:notice] = "Signed in with #{provider.to_s.titleize} successfully." | |
sign_in_and_redirect @user, :event => :authentication, :notice => "登陆成功。" | |
else | |
redirect_to new_user_registration_url | |
end | |
end | |
end | |
} | |
end | |
end | |
provides_callback_for :slack | |
# This is solution for existing accout want bind Google login but current_user is always nil | |
# https://github.com/intridea/omniauth/issues/185 | |
def handle_unverified_request | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment