How can I add Google server side sign in authentication to my Golang web application?

Viewed 1241

My question may lack specifics but I just cant understand how the whole process should work.. How I should implement it? What are the ways? Any guidance is welcome.

I have followed official documentation, their guides but even if I get something to work I dont really know if I am doing the right thing as is the end result what I need?

I think I need server side in order to store the sessions and credentials to the database.

It is a side task in my school, I am also limited to using only Golangs standard packages.

1 Answers

Not sure, What exactly you are looking but few things i am mentioning which might help you.

Google Sign-In for server-side apps :

Implementing the one-time-code flow : The Google Sign-In button provides both an access token and an authorization code. The code is a one-time code that your server can exchange with Google's servers for an access token.

  1. Create a client ID and client secret
  2. Include the Google platform library on your page
  3. Initialize the GoogleAuth object
  4. Add the sign-in button to your page
  5. Sign in the user
  6. Send the authorization code to the server
  7. Exchange the authorization code for an access token

For better understanding refer here : https://developers.google.com/identity/sign-in/web/server-side-flow and https://cloud.google.com/go/getting-started/authenticate-users-with-iap and https://skarlso.github.io/2016/06/12/google-signin-with-go/

In Golang we have this library : https://pkg.go.dev/golang.org/x/oauth2/google

This is the example you can try by your own : https://dev.to/douglasmakey/oauth2-example-with-go-3n8a

Related