In this blog we will learn about how to get started with Golang and create a auth with Google Login. We will be using the Gin and gorm ORM. During my initial uses of Gin I felt for new users the documentation is not friendly enough, hence I decided to write this article. If you are coming from a Python/Ruby world Gin is NOT like Django or Rails. Rather, its more like Flask and Sinatra. This is how the framework folks defines Gin:
What is Gin?
Gin is a web framework written in Golang. It features a Martini-like API, but with performance up to 40 times faster than Martini. If you need performance and productivity, you will love Gin.
Gin supports the following features:
- API Performance - Has a very small memory footprint and radix tree based routing.
- Middleware Support - Supports chain of middlewares to modify an incoming HTTP request.
- Crash Safety - Can capture panic during request processing and recover from it.
- JSON Validation - Inbuilt JSON parsing and validation.
- Route Grouping - Provides us with an option of grouping multiple routes rogether.
- Error Management - Provides us with an easy way to collect errors and use them in middlewares if required.
- Rendering - Has an inbuilt renderer that can be used with JSON and HTML.
- Extendable - Can extend functionality with middlewares.
Creating our first Gin app
Now that we know a little bit about Gin lets get started with our app.
Assuming Go is installed in the system, create a directory myginapp
and run the command go mod init myginapp
. If Go is not already installed follow the instructions from here.
The command creates the go.mod
file and is required to run our application.
Run the command go get -u github.com/gin-gonic/gin
to add gin as dependency.
Lets now create an app.go file. This is the main app of the gin project and runs the application.
You can now run your server by running the command go run app.go
.
Now to test our API we can run curl http://localhost:8080/hello
and we should see the following response -
|
|
Add Google OAuth
Now that our Gin application is up we will work on the authentication with Google OAuth. The API flow will work in the following manner:
- Load the page with Google Sign In Button which triggers the beginning of the authentication process. For our usecase this will use the
/auth/google/login
route. - Initiate the authentication process using the gothic handler which invokes the Google APIs. For this process we will use the
/auth/google/begin
route. - The final step is the callback which is received from Google and is based on the configuration in Google Cloud Console. Here, we have defined the route as
/auth/google/callback
.
We also need to create a Google Client ID and Client Secret. You need to follow below steps once you open Google API Console
- From the project drop-down, select an existing project, or create a new one by selecting Create a new project
- In the sidebar under “APIs & Services”, select Credentials
- In the Credentials tab, select the Create credentials drop-down list, and choose OAuth client ID.
- Under Application type, select Web application.
- In Authorized redirect URI use
http://localhost:8080/auth/google/callback
- Press the Create button and copy the generated client ID and client secret
Next, create a directory call templates
and add an index.tmpl
with the following the code.
|
|
Next we will add a success.tmpl
template file in the same templates directory.
|
|
Now that the templates are ready we need to add the routers and controllers for the same. We start with adding the dependencies:
We also need to load our environments for our Google Client ID and secret loading from env variables. Lets create a loadEnvs.go
in core
module. We will load this in our app.go.
Next, we add our controllers code which performs the actual authentication. Add the following code to googleauth.go
.
|
|
Finally we add the routers in our app.go
as shown below and restart our application server.
|
|
Once this is done, visit the login page at /auth/google/login
and click on Signin with Google
button. This should start the authentication process and when completed successfully it would return an access token with user
object. This AccessToken
can then be used for authenticating other APIs.
I hope this article is helpful in getting started with Google SignIn using Gin. If you have questions or further doubts please feel free to add in comments.