Explanation of JWT Authentication in Golang using Gin Web Framework
Introduction
Do you know, How to create a JWT Authentication Application in Golang using Gin Web Framework? If not!! Then no worries, you clicked the right content. In this tutorial, I will give you some idea about JWT Authentication with an example code. This blog will also cover an explanation regarding some concepts that are required to create a JWT Authentication App in Golang. But before proceeding the first thing that, I will mention is the prerequisites part that you need to have the knowledge of and the second thing is What exactly JWT Authentication is in Golang web development.
Prerequisites
You should be aware of the below-mentioned concepts so that you can understand the tutorial and get a proper learning outcome from it.
● You should know how to set up a workspace for your project. If you don’t know about it you can refer to this page.
● You should know what are the commands and processes to import mod files and other external packages.
● You should know the concept of Struct.
● You should know about Routing and concepts like API.
● Downloads
1) Go Programming Language
2) IDE (VS CODE)
3) PostMan Application
JWT Authentication
Let’s start with a definition of JWT Authentication. JWT is an abbreviation for JSON web token, which is a token-based stateless authentication method. It is frequently used as a client-side stateless session, allowing the server to save session data without relying entirely on a database. JWT is most commonly used in server-to-server authorization and API authentication.
Integration of JWT
Hereafter creating the directory on our command prompt, I will start building the Application to explain how you can create JWT Authentication Application. I hope you know about the command “code.” which will open up the IDE on your screen. Here the first file with which we will begin is main.go. In that main.go file I will import some internal packages like “log” and “os”. And some external packages are also required to be imported like github.com/gin-gonic/gin” (Gin), “github.com/golangcompany/JWT-Authentication/routes” (routes), and “github.com/joho/godotenv. Then after importing the packages, I will move ahead with my main.go function which includes the entry point of the program and it’s executed when you run your application. We’re using
“if port == "" {port = "8000"}”
to set a default value for the port. If the environment variable PORT is not set, we’ll use 8000 as the default value. Here, “routes.AuthRoutes(router)” and “routes.UserRoutes(router)” call the functions AuthRoutes and UserRoutes from the package routes. The function takes in an argument of type *gin.Engine, which is a pointer to a struct called Engine that belongs to the package gin. Then we state a simple API that returns JSON data that has two endpoints: /api-1 and /api-2. Finally, we instruct the program to start the server with router.Run(“:” + port). This is how you will proceed with the main.go file. (Refer to Image 1)
Now, let’s proceed towards struct. Here, my struct will carry different fields like ID, First Name, Last Name, Password, E-Mail, Phone Number, Token ID, User Type, Refresh Token ID, Created at, Updated at, and User ID. I have the primitive package in my models since, I want that for every user, who gets registered gets a unique ID in my database so that while fetching the users, I can use that ID to fetch the user without any conflicts. I used “validate”, which checks if the data entered from the client side is in the format we’ve programmed for and returns an error if it isn’t. In the User type field, I mentioned eq=ADMIN|eq=USER because it validates the value of the field user type to be either ADMIN or USER. I will now guide you on the routes folder.