Last active
October 9, 2020 12:29
-
-
Save drnic/c5628368df08bc13f10aa75440e133aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
# support User#external_avatar_url for Google One Tap avatars | |
def avatar_url_for(user, opts = {}) | |
size = opts[:size] || 48 | |
if user.respond_to?(:avatar) && user.avatar.attached? && user.avatar.variable? | |
user.avatar.variant(combine_options: { | |
thumbnail: "#{size}x#{size}^", | |
gravity: "center", | |
extent: "#{size}x#{size}" | |
}) | |
elsif user.respond_to?(:external_avatar_url) && user.external_avatar_url | |
user.external_avatar_url | |
else | |
hash = Digest::MD5.hexdigest(user.email.downcase) | |
"https://secure.gravatar.com/avatar/#{hash}.png?height=#{size}&width=#{size}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "google-id-token" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GoogleAuthController < ApplicationController | |
include Devise::Controllers::SignInOut | |
skip_before_action :verify_authenticity_token | |
def login | |
if !cookies[:g_csrf_token] || !params[:g_csrf_token] || params[:g_csrf_token] != cookies[:g_csrf_token] | |
return render text: "400 Missing g_csrf_token", status: 400 | |
end | |
id_token = params[:credential] | |
validator = GoogleIDToken::Validator.new | |
payload = validator.check(id_token, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_ID"]) | |
name = payload["name"] | |
email = payload["email"] | |
avatar_url = payload["picture"] | |
unless user = User.find_by(email: email) | |
user = User.create!( | |
email: email, | |
name: name, | |
password: SecureRandom.hex, | |
terms_of_service: true, | |
external_avatar_url: avatar_url | |
) | |
end | |
sign_in(user) | |
redirect_to root_path | |
end | |
# developers can clear exponential delete g_state from cookies | |
# https://stackoverflow.com/a/64055782/36170 | |
def clear | |
cookies.delete(:g_state) | |
redirect_back(fallback_location: "/") | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
post "/auth/google/login", to: "google_auth#login" | |
get "/auth/google/clear", to: "google_auth#clear" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment