From fcfc89516760abf922315920fbb4c14b6497de80 Mon Sep 17 00:00:00 2001
From: Blake Erickson <o.blakeerickson@gmail.com>
Date: Fri, 7 Jul 2017 14:04:56 -0600
Subject: [PATCH] FIX: new sign-ups via google are added to groups

This fix ensures that users that are signing up via google oauth are
automatically added to any groups.

A similar fix will probably need to be added to other oauth providers.
---
 lib/auth/google_oauth2_authenticator.rb           |  4 ++++
 .../auth/google_oauth2_authenticator_spec.rb      | 15 +++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/lib/auth/google_oauth2_authenticator.rb b/lib/auth/google_oauth2_authenticator.rb
index 2dfc69da5cd..31e44ea25dc 100644
--- a/lib/auth/google_oauth2_authenticator.rb
+++ b/lib/auth/google_oauth2_authenticator.rb
@@ -31,6 +31,10 @@ class Auth::GoogleOAuth2Authenticator < Auth::Authenticator
   def after_create_account(user, auth)
     data = auth[:extra_data]
     GoogleUserInfo.create({user_id: user.id}.merge(data))
+    if auth[:email_valid].to_s == 'true'
+      EmailToken.confirm(user.email_tokens.first.token)
+      user.set_automatic_groups
+    end
   end
 
   def register_middleware(omniauth)
diff --git a/spec/components/auth/google_oauth2_authenticator_spec.rb b/spec/components/auth/google_oauth2_authenticator_spec.rb
index db28abfd225..fcba68cce46 100644
--- a/spec/components/auth/google_oauth2_authenticator_spec.rb
+++ b/spec/components/auth/google_oauth2_authenticator_spec.rb
@@ -82,4 +82,19 @@ describe Auth::GoogleOAuth2Authenticator do
     end
   end
 
+  context 'after_create_account' do
+    it 'confirms email' do
+      authenticator = Auth::GoogleOAuth2Authenticator.new
+      user = Fabricate(:user)
+      session = {
+        :email_valid => "true",
+        :extra_data => {
+          :google_user_id => 1
+        }
+      }
+      authenticator.after_create_account(user, session)
+      expect(user.email_confirmed?).to eq(true)
+    end
+  end
+
 end