Have Google and Github Sign In Options
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
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.
This would remove a barrier to entry and we'd like to grow, so I've raised the priority.
I would like to try to take this on if possible. (I now see that I posted this as a duplicate here.)
I am currently working on this. Will post updates.
Adding some info here on what I’ve learned so far.
- you add the omniauth-google gem to the Gemfile
- you add a configuration line in /configuration/initializers/devise.ru to pull in the Omniauth Google credentials
- 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