Name: google_sign_in
Owner: Basecamp
Description: Sign in (or up) with Google for Rails applications
Created: 2017-05-30 13:46:32.0
Updated: 2018-03-29 03:16:22.0
Pushed: 2018-03-02 14:48:12.0
Homepage: null
Size: 17
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Google Sign-In provides an easy and secure way to let users signin into and up for your service, without adding yet-another per-app email/password combination. Integrating it into your Rails app should be drop-in easy. This gem makes it so.
The only configuration needed is setting the Google client id for your application. Google has a tutorial on how to setup a client id.
Once you have your client id, create a config/initializers/google_sign_in_client_id.rb
file with this:
GoogleSignIn::Identity.client_id = <THAT CLIENT ID YOU GOT FROM GOOGLE>
Now you can use the sign-in integration on your signup or signin screen.
Here's the most basic example:
p/views/layouts/application.html.erb
l>
d>
Required for google_sign_in to add the Google JS assets and meta tags! %>
yield :head %>
ad>
y>
yield %>
dy>
ml>
p/views/sessions/new.html.erb
google_sign_in(url: session_path) do %>
You can replace this with whatever design you please for the button.
You should follow Google's brand guidelines for Google Sign-In, though:
https://developers.google.com/identity/branding-guidelines
= button_tag("Signin with Google") %>
nd %>
The url
option is the URL that the hidden form will be submitted against along with the Google ID Token
that's set after the user has picked the account and authenticated in the pop-up window Google provides.
You can then use that in a sessions controller like so:
s SessionsController < ApplicationController
f new
d
f create
if user = authenticate_via_google
cookies.signed[:user_id] = user.id
redirect_to user
else
redirect_to new_session_url, alert: "authentication_failed"
end
d
ivate
def authenticate_via_google
if params[:google_id_token].present?
User.find_by google_id: GoogleSignIn::Identity.new(params[:google_id_token]).user_id
end
end
(This example assumes that a user has already signed up for your service using Google Sign-In and that
you're storing the Google user id in the User#google_id
attribute).
That's it! You can checkout the GoogleSignIn::Identity
class for the thin wrapping it provides around
the decoding of the Google ID Token using the google-id-token library. Interrogating this identity object
for profile details is particularly helpful when you use Google for signup, as you can get the name, email
address, avatar url, and locale through it.
Google's JavaScript doesn't play nice with Turbolinks. We've routed around the damage by adding a Turbolinks meta tag
on whatever page google_sign_in
is called to always do a full reload for that page. Note that this
auto-compatibility feature requires Turbolinks 5.1+.
Google Sign-In for Rails is released under the MIT License.