Member-only story
How to Setup Google OAuth with Devise on Rails
Step-by-step guide on how to set up Google Auth for a Rails app.
If you came here, I'm assuming you already have a Rails application that is up and running and that you're using Devise for authentication. If that's not the case, I'm afraid this article is not for you 😬
Step 1: Set up your project
You should already have the first two gems below, so add the two last gems below into your Gemfile. The omniauth-google-oauth2 gem is the core of this post.
# Gemfile
gem 'devise'
gem 'devise-i18n'
gem 'omniauth-google-oauth2'
gem 'omniauth-rails_csrf_protection', '~> 1.0' # https://github.com/heartcombo/devise/issues/5236
The omniauth-rails_csrf_protection is required to work around a devise authentication issue. You can check the link in the comment below if you want more details about the issue.
Then, from your command line:
$ bundle install
You'll also need to adapt the initializer for Devise:
# config/initializers/devise.rb
Devise.setup do |config|
config.omniauth :google_oauth2,
Rails.application.credentials.dig(:google_auth, :client_id),
Rails.application.credentials.dig(:google_auth, :client_secret)…