My code used to work. After I moved my server to a new one, suddenly I can't log in with google accounts anymore. I can see google presents me with a list of my accounts and google redirects to my server with all the params after I choose one account.
Here is the log on my server.
method=GET path=/user/auth/google_oauth2/callback format=html controller=Users::OauthController action=failure status=302 duration=1.79 view=0.00 db=0.00 location=http://gokien.info/user/sign_in time=2020-12-09 14:35:39 +1100 remote_ip=123.123.123.123 params={"state"=>"the state", "code"=>"the code", "scope"=>"email profile openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile", "authuser"=>"0", "prompt"=>"none"} user_id= referer=https://accounts.google.com/ log_level=debug
Here is my code for OauthController
1 class Users::OauthController < Devise::OmniauthCallbacksController
2 rescue_from UserError::MissingEmailAddress,
3 with: :missing_email_address
4
5 # This is the key method
6 # used to authenticate user
7 # after they are redirected from
8 # Google login page
9 def google_oauth2
10 @user = User.from_omniauth(request.env[GK::Constants::OMNIAUTH_KEY ])
11 sign_in_and_redirect @user, event: :authentication
12 end
13
14 def missing_email_address
15 flash[:missing_email_address] = I18n.t 'loginPage.error.missingEma ilAddress'
16 redirect_to new_user_session_path
17 end
18
19 # redirect after successful login with google or facebook
20 # https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-s pecific-page-on-successful-sign-in
21 def after_sign_in_path_for(resource)
22 user_dashboard_path
23 end
24
25
26 alias_method :facebook, :google_oauth2
27
28 end