qpixel icon indicating copy to clipboard operation
qpixel copied to clipboard

Have Google and Github Sign In Options

Open kousikmitra opened this issue 5 years ago • 5 comments

Is your feature request related to a problem? Please describe.

It would be nice and convenient to have one click sign in option via Google and Github

Describe the solution you'd like

If I click on Sign in with Google/Github, It should fetch my basic information and create my codidact account.

Describe alternatives you've considered

Additional context

kousikmitra avatar Dec 23 '20 05:12 kousikmitra

Agree it would be nice to have; prioritizing low only by comparison to other issues, not in isolation. When we clear some of the other issues I want to move this up.

cellio avatar Dec 23 '20 13:12 cellio

This would remove a barrier to entry and we'd like to grow, so I've raised the priority.

cellio avatar Jul 26 '23 02:07 cellio

I would like to try to take this on if possible. (I now see that I posted this as a duplicate here.)

hmltn-0 avatar Feb 10 '24 04:02 hmltn-0

I am currently working on this. Will post updates.

hmltn-0 avatar Feb 10 '24 16:02 hmltn-0

Adding some info here on what I’ve learned so far.

  1. you add the omniauth-google gem to the Gemfile
  2. you add a configuration line in /configuration/initializers/devise.ru to pull in the Omniauth Google credentials
  3. You get the Oauth2 credentials from Google developer and store them as an environmental secret NOT committed to the repo (ie locally or in GitHub secrets of developing in a codespace)

Now exploring next steps. Thanks.

———

Add a controller to the routes: devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }

implement the callback controller:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def google_oauth2 # You will 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
  set_flash_message(:notice, :success, kind: "Google") if is_navigational_format?
else
  session["devise.google_data"] = request.env["omniauth.auth"].except(:extra) # Removing extra as it can overflow some session stores
  redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end

end end

Update the user model:

def self.from_omniauth(access_token) data = access_token.info user = User.find_by(email: data['email'])

unless user user = User.create(name: data['name'], email: data['email'], password: Devise.friendly_token[0,20]) end user end

hmltn-0 avatar Feb 10 '24 18:02 hmltn-0