Authentication JWT using Golang and Mux Middleware

Aditya Erlangga

Sosial Media


0 orang menyukai ini
Suka

Summary

Authentication with JSON Web Tokens (JWT) is a popular method for securing web applications and APIs. JWT authentication offers several benefits, such as statelessness, scalability, and easy integration with different services. Since the token contains all the necessary user information, there's no need to store session data on the server, making it scalable for distributed systems. Additionally, JWTs can be used across multiple services or microservices without the need for shared session stores, simplifying the authentication process. In Go (Golang), the Mux package is commonly used as a powerful HTTP router and middleware framework. This summary will explain how to implement JWT authentication using Golang and Mux middleware. By implementing JWT authentication using Golang and Mux middleware, you can secure your web application or API endpoints effectively. Remember to handle token expiration, revocation, and other security considerations based on your specific requirements.

Description

On this occasion we will try to implement the use of microservices using the golang programming language in the simple case of Authentication JWT

Tools and materials that need to be prepared

  1. Visual studio code for code editor
  2. Postman for sending API requests to any REST API, and to receive responses to your requests.
  3. MySQL for managing information in the database on the server side with the SQL programming language

 

Coding and Testing

  • first, we create a folder that contains the program that we will create, such as controllers, helpers, middlewares, models, and the main program.

  • in the main program, we make the main function, namely to login, register and logout

  • in the controller file, we create an authcontroller file. In the controller file, we create the authcontroller file. Then we create an authcontrollers program that contains functions to handle login, register, and logout functions. 

  • In the controllers folder, create a productcontrollers.go file which contains the protected product API. It contains product data

  • in the models folder we create a user.go file as the user table structure. And we create a setup.go for connection to database

  • In the helper folder we create a file response.go which serves to send a JSON response in HTTP format to the user

  • in the config folder create a file jwt.go that works With to generate and process JWT tokens in your Go application. The JWT_KEY variable is the secret key used to sign and verify tokens.

  • In the middlewares folder create a jwt.go file as an intermediary between the server and the application. Middleware functions are usually placed between request processing and response processing in web applications or APIs. 

This JWTMiddleware function is used to check and validate the JWT token sent by the client in the request. If the token is valid, control is passed to the next handler in the processing chain. If the token is invalid or some other error occurs in processing the token, the middleware will send an "Unauthorized" response with a relevant message.

 

Testing

First run the main program in the terminal

When we want to enter the API product but we haven't logged in or don't have a token, an unautorize message will appear

Then let's try to register first with the structure that was made before

and we try to log in using the account that we registered

then when we try to access api products, data will appear from that directory..

Because we have logged in and managed to get cookies in the form of tokens

Informasi Course Terkait
  Kategori: Cyber Security
  Course: Teknologi Cyber Security/Cloud Computing