Answers · Keys & Services

How do I add sign in with Google to a Rails app?

Create an OAuth 2.0 client in Google Cloud Console, add your callback URL as an authorized redirect URI, and set the client id and secret. One Shot already has the OmniAuth callback route and the user upsert, so no code changes. Google's host also has to be in the content security policy's form-action list.

What Google needs from you

An OAuth 2.0 Web application client, with the redirect URI set to https://yourdomain.com/auth/google_oauth2/callback exactly. Google matches it literally, so a trailing slash or http instead of https is a rejection with a message that does not explain itself.

Add http://localhost:3000/auth/google_oauth2/callback as a second authorized URI so development works too. Google allows multiple.

What the app already has

bin/rails routes | grep auth

The callback route is declared in config/routes.rb and handled by a controller that hands off to User.upsert_by_email!. That method is the single signup chokepoint: first sign-in creates both the User and the Account in one transaction, whether the person arrived by magic code or by Google.

Which means adding Google does not add a second signup path. Someone who signed up by email and later clicks the Google button on the same address lands in the same account.

The two settings that are easy to miss

The consent screen. An unpublished app in testing mode only admits the test users you list. Everyone else gets an access-denied screen that looks like a code bug and is not.

The content security policy. Google's host must be in form-action in config/initializers/content_security_policy.rb, and the sign-in form needs data-turbo="false". Miss either and the button silently does nothing.

Verify

bin/dev

Click the button, complete consent, and check you land signed in. Then sign out and repeat with the same address via a magic code: you should arrive in the same account, not a second one.

In the native mobile shells

Social sign-in has to leave the web view. Google blocks OAuth inside embedded web views, so the native apps open a system browser for it. See Auth in the App.

Related questions

Why does my OAuth sign-in button do nothing when clicked?

The content security policy is blocking the navigation. form-action covers the whole redirect chain a form starts, so the provider's host must be listed in config/initializers/content_security_policy.rb. The form also needs data-turbo="false", or Turbo submits it by fetch and the cross-origin redirect is blocked as an XHR instead.

Why does sign in with Apple need a paid developer account?

The client id and private key come from the Apple Developer Program, which is a paid membership at 99 dollars a year. There is no free tier for issuing Sign in with Apple credentials. Google's OAuth client is free, so if you only want one social provider to start, Google is the one with no standing cost.

Do I need API keys to run a Rails starter kit locally?

No. Billing, email, bot checking and error reporting each sit behind an adapter that returns a real client when its credential is present and a deterministic local fake when it is not. A fresh clone runs bin/setup and bin/dev with no keys, and checkout, sign-in and gated features all work end to end.