We've moved discussions to Discord

Getting Google Auth to work

Alex Deering
So I might be missing something from the documentation but it looks like all i should have to do is put the client id for google as the public_key and my client secret as the private_key in the development credentials.  I did that and restarted the server and there is no change to the log in and doesnt show the sign in with google.  Am I missing something that I need to change somewhere else
Chris Oliver
Hey Alex,

You'll also have to enable it afterwards in the Jumpstart admin so it installs the gem. Keys are required first so that the gem can be configured basically.
Alex Deering
When i go to the jumpstart admin it doesnt have any options to change anything under the Login & OAuth section.  Just says to go into the development credentials file and add the keys, which I have done.  I have tried hitting the save changes button and having it restart the server but that doesnt do anything
Chris Oliver
Maybe they're not in the right spot?

omniauth:
  facebook:
    public_key: 
    private_key:
  github:
    public_key: 
    private_key: 
  twitter:
    public_key: 
    private_key: 
  google_oauth2:
    public_key: 
    private_key:

Then in the Login & OAuth you'll see an Enable checkbox.

Just tested it locally and it's working for me.

Alex Deering
ok, so found the issue, the default credentials file has just 
google:
    public_key: 
    private_key:
And the jumpstart docs has it the same way so didnt think it needed to be changed.  Changed it to google_oauth2 and it showed up
Chris Oliver
Ah ha, thanks! I'll fix that right now. 
I have set up the google_oauth2 keys and enable the google_oauth button

After I  pick my google account, it goes back to the login screen (seem like not logged in).  Anything I miss ?
The console log shows the following:

Access to fetch at 'https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=84462564647-uglhrahhoseso8dvpi7qs5ktgtsteub6.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fusers%2Fauth%2Fgoogle_oauth2%2Fcallback&response_type=code&scope=email+profile&state=b228a91a817e402955a64c63afb151255365b74ab8c844cf' (redirected from 'http://localhost:3000/users/auth/google_oauth2?locale=en') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Chris Oliver
NG DAVID  What about your Rails logs?
That is from the console log


Started GET "/users/auth/facebook?locale=en" for ::1 at 2020-01-14 12:48:47 +0800
I, [2020-01-14T12:48:47.068597 #4708]  INFO -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook?locale=en" for ::1 at 2020-01-14 12:48:47 +0800
I, [2020-01-14T12:48:47.300744 #4708]  INFO -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook/callback?code=AQD3BOtrX1ivBmmpQHkbYZDfMoLhwRWCWSuMRFlntjpwZuCMo-gt80NFRmMT3zHpuf-vJ7yD9hhwdZKmXKpn5cZ70LKegwA7fEMCuZ5nN9vINDVAnM1SdynVNNhvshZtaQaQi_y1Qe4FUZa4D3Z0wRGsq2WoOk80dzSemzarcvet99V4Sg2lNXNtosNgjeNSE8egeJnkiIVw20bhj9Dmv_Y-ywMXn9KHD6AZsYm7WT1DNOAcyd5PoExhdBjrhzOZo3hTxm828UGE9HHrM2q9LwZoxVl40473zgFGbQNpDTKMYmvw0fhDSspuQt5URQCzb8n7ec0_lFhurkcRuF6K3X9y&state=b8d55c47be1973e1463b26ca545ad32410840714efc3ad4f" for ::1 at 2020-01-14 12:48:47 +0800
I, [2020-01-14T12:48:47.763662 #4708]  INFO -- omniauth: (facebook) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#facebook as HTML
  Parameters: {"code"=>"AQD3BOtrX1ivBmmpQHkbYZDfMoLhwRWCWSuMRFlntjpwZuCMo-gt80NFRmMT3zHpuf-vJ7yD9hhwdZKmXKpn5cZ70LKegwA7fEMCuZ5nN9vINDVAnM1SdynVNNhvshZtaQaQi_y1Qe4FUZa4D3Z0wRGsq2WoOk80dzSemzarcvet99V4Sg2lNXNtosNgjeNSE8egeJnkiIVw20bhj9Dmv_Y-ywMXn9KHD6AZsYm7WT1DNOAcyd5PoExhdBjrhzOZo3hTxm828UGE9HHrM2q9LwZoxVl40473zgFGbQNpDTKMYmvw0fhDSspuQt5URQCzb8n7ec0_lFhurkcRuF6K3X9y", "state"=>"b8d55c47be1973e1463b26ca545ad32410840714efc3ad4f"}
  User::ConnectedAccount Load (2.8ms)  SELECT "user_connected_accounts".* FROM "user_connected_accounts" WHERE "user_connected_accounts"."provider" = $1 AND "user_connected_accounts"."uid" = $2 ORDER BY "user_connected_accounts"."id" ASC LIMIT $3  [["provider", "facebook"], ["uid", "10157796928347789"], ["LIMIT", 1]]
  ↳ app/models/user/connected_account.rb:47:in `for_auth'
  User Load (1.6ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "david.ng.hk@gmail.com"], ["LIMIT", 1]]
  ↳ lib/jumpstart/app/controllers/jumpstart/omniauth_callbacks_controller.rb:16:in `block (2 levels) in <class:OmniauthCallbacksController>'
Redirected to http://localhost:3000/users/sign_in?locale=en
Completed 302 Found in 43ms (ActiveRecord: 17.0ms | Allocations: 5955)

Chris Oliver
That's processing a Facebook callback, not Google. Did you set the wrong callback URL?
I try both with the same result.  However, I reset the database, it is working now.  I shall investigate this more.  

Thanks for your prompt reply. 
My finding is both manual signup and OminAuth signup are working.  However, it user account is manual signup, then user can not use omniAuth to signin.  

Would be nice if we can do both in the same time. 

Chris Oliver
If you already manually signed up, you can't login to that account with OAuth until you've approved it on your existing account for security.
Ugurcan Kaya
if you want to use "google" in urls, you can use name attribute:

google_oauth2:
    public_key: 
    private_key:
    name: 'google'
Jaime Alvarez
Hi! This response was really helpful for me. But how can I set the right url in my Google app when I login to my account? 
Ugurcan Kaya
The name will become the provider value. So the endpoint would be "myapp.com/users/auth/NAME"

if you do not use name param, it will be "myapp.com/users/auth/google_oauth2"
Jaime Alvarez
Ugurcan Kaya yeahp I have it like you wrote, but when I try to login from http://myapp.com/1/users/login, Google try to redirect to http://myapp.com/1/users/auth/google_oauth2/callback. I believe that number 1 (account_id) is why give the error. 
Ugurcan Kaya
Jaime Alvarez   Jumpstart has a code in devise views that loops Omniauth providers and generate sign in buttons, which uses default provider names. You will want to tweak that code or replace with your custom buttons.
Keith Gapol
I seem a bit lost too. I am not seeing the template expected for public and secret keys. When running the generate command:
rails credentials:edit --environment development
Couldn't decrypt config/credentials/development.yml.enc. Perhaps you passed the wrong key?
Keith Gapol
Maybe a video on how to do this?
Notifications
You’re not receiving notifications from this thread.