Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to i add multiple providers for social login using devise + omniauth ?? #5017

Closed
theshashiverma opened this issue Jan 30, 2019 · 9 comments

Comments

@theshashiverma
Copy link

theshashiverma commented Jan 30, 2019

I follow the omniauth overview article from the wiki part to add fb auth in my ror project, and now I also want to add the google and github I added the secret and id of google and github in devise.rb and create the same method as fb as describe in the omniauth overview article and did the changes where it was necessary. but now auth only works fine with fb , google and git is not working , the method i have created is being called but the else part of method so it redirect to the root_path or where ever we define. please help me

this is my devise.rb


config.omniauth :facebook, '<client id>', 'client secret'
  config.omniauth :github, '<client id>','client secret'
  config.omniauth :google_oauth2,'<client id>', '<client secret>'
  

user.rb


class User < ApplicationRecord

  devise :database_authenticatable, :registerable,:recoverable, :rememberable,
   			:validatable, :trackable, :lockable, :timeoutable,:omniauthable,
    		omniauth_providers: [:facebook, :github, :google_oauth2]

    def self.from_omniauth(provider_data)
    	where(provider: provider_data.provider, uid: provider_data.uid).first_or_create do | user |
    	  user.email = provider_data.info.email
    	  user.password = Devise.friendly_token[0, 20]
    	  #user.skip_confirmation!
    	end
   end

end

omniauth_callbacks_controller.rb



class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def github
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, kind: "Github") if is_navigational_format?
    else
      session["devise.github_data"] = request.env["omniauth.auth"]
      redirect_to root_path
    end
  end
  

application_controller.rb

class ApplicationController < ActionController::Base
	before_action :authenticate_user!
end

routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: {
		        sessions: 'users/sessions',
		        registrations: 'users/registrations',
		        passwords: 'users/passwords',
		        omniauth_callbacks: 'users/omniauth_callbacks'
		      }

  root to: 'pages#index'
  get 'pages/about'
end

@krtschmr
Copy link

i hope some admin is closing this, simply for the fact of your attention whoring. issues on github are for code specific problems and not for your own struggle of implementation.

in case somebody would consider taking time to help you out, they simply can't as you didn't provide any error messages or failures.

@krtschmr
Copy link

the fix would be to debug the error on your side

gem pry-rails

and then bundle.
then add binding.pry

@user = User.from_omniauth(request.env["omniauth.auth"]) binding.pry if @user.persisted?

then the server will halt at that binding point and you can go with

@user.errors

good luck.

@theshashiverma
Copy link
Author

theshashiverma commented Jan 31, 2019

@krtschmr Thank you for the reply, I did not know the rules for how to put a question on github

@tegon
Copy link
Member

tegon commented Jan 31, 2019

@theshashiverma We try to reserve the GitHub issue tracker for bugs and feature requests. It's better to post questions and ask for help in StackOverflow or forums, where a wider community will be able to help you.

Also, when you do ask for help try to give it a while until people can answer it. This issue was only open less than a day when you started mentioning us in other issues. I would definitely have answered you here when I had the time (we also have a lot of other issues to look at).

Asking people to look at this issue on the others probably caused noise and polluted previous discussions with something that doesn't add value to it. So when you want to ping us to look at an issue please do it in the issue itself - we'll receive notifications either way.

I hope these tips will help you the next time you need to open an issue.

Thank you!

@tegon
Copy link
Member

tegon commented Jan 31, 2019

@theshashiverma I just noticed that you included your app's OAuth credentials in the issue description - client ID and client secret. People can use those to access your OAuth applications now.
When posting sample codes in public places - like here - it's better to not include this kind of information.

I would advice you to delete those applications and create new ones now.

@theshashiverma
Copy link
Author

@tegon Thank you, sir, I got your point. Basically, I am new to this developing world(a fresher) I think it would take time to understand how things work.

@tegon
Copy link
Member

tegon commented Feb 4, 2019

@theshashiverma No problem, I'm happy to help.

@aravindaytha12
Copy link

aravindaytha12 commented Jun 15, 2019

How to handle email validation in User creation

I am using multiple providers(google, facebook)
Showing the error as - "Email has already been taken", as because of same email id for both the providers

def self.from_omniauth(auth)
	user = User.where(provider: auth.provider, email: auth.info.email).first
	user ||= User.create!(provider: auth.provider, uid: auth.uid, name: auth.info.name, email: auth.info.email, password: Devise.friendly_token[0,20])
	user
end

@krtschmr
Copy link

set the duplication constraint over [:email, :provider] rather than just having unique :email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants