Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How do you add "Sign In with Google" to a Ruby on Rails web application that is using the Devise authentication framework?
I want to add the ability to "Sign in with Google" into a Ruby on Rails web application, that is using Devise to handle authentication. How do I do that?
1 answer
Step 1: Locate your Gem file and add the omniauth-google-oauth2 gem to it.
Step 2: Add configuration for Google OAuth2 to your Devise configuration file, located in /config/initializers/devise.rb.
Step 3: Create authentication credentials (a Client ID and Client Secret) on the Google Cloud console, for an OAuth2 application. Enter them into the Google OAuth2 configuration in the Devise configuration file.
Step 3: Create a controller that handles the backend logic. When the user "signs in with Google", their browser opens a new page in which they authenticate with Google. When successful, Google sends an authentication token back to the page which the user came from. This works because of a 'callback URL'. A callback URL is a URL that the original, calling app provides to Google, when it redirects the user to Google. Once authenticated, Google knows where to "call back", and return the authentication information back to. What it specifically returns is an activation code. This is a session token which permits accessing particular data from Google. Your callback controller has methods to determine what data you want to pull in from Google, once authenticated, such as the user's name, email, profile picture, etc. The callback controller decides how to store that information in the user authentication database on your own application's side. It also determines how to handle the authentication logic regarding if there is a new user being created or a pre-existing user being signed in; as well as if Google Sign-In can be used to connect to alternative sign-in methods, such as signing in to the same profile with GitHub, or with a user-created profile using an email, username, and/or password.
Step 4: Update the model. Update the application to enable the user to click the Sign in with Google button.
1 comment thread