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
config.omniauth :facebook, '<client id>', 'client secret'
config.omniauth :github, '<client id>','client secret'
config.omniauth :google_oauth2,'<client id>', '<client secret>'
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
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
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
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
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.
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.
@krtschmr Thank you for the reply, I did not know the rules for how to put a question on github
@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!
@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.
@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.
@theshashiverma No problem, I'm happy to help.
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
set the duplication constraint over [:email, :provider] rather than just having unique :email
Most helpful comment
@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!