How do I add authentication with JWT with Gin in Go?

Implementing JWT authentication in a Go application using the Gin framework can enhance the security of your API. This example demonstrates how to set up JWT authentication, which ensures that only authorized users can access certain endpoints of your application. In this example, users will be able to register, log in, and access a protected route using JWT tokens.

Example Code for JWT Authentication with Gin

package main import ( "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "net/http" "time" ) var jwtSecret = []byte("your-secret-key") func main() { r := gin.Default() r.POST("/login", login) protected := r.Group("/protected") protected.Use(authMiddleware()) protected.GET("/", protectedEndpoint) r.Run(":8080") } type User struct { Username string `json:"username"` Password string `json:"password"` } func login(c *gin.Context) { var user User if err := c.ShouldBindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // Here, you would typically validate the username and password with a database. token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "username": user.Username, "exp": time.Now().Add(time.Hour * 24).Unix(), }) tokenString, err := token.SignedString(jwtSecret) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"token": tokenString}) } func authMiddleware() gin.HandlerFunc { return func(c *gin.Context) { tokenString := c.Request.Header.Get("Authorization") if tokenString == "" { c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header required"}) c.Abort() return } claims := &jwt.MapClaims{} token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) { return jwtSecret, nil }) if err != nil || !token.Valid { c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"}) c.Abort() return } c.Set("username", (*claims)["username"]) c.Next() } } func protectedEndpoint(c *gin.Context) { username := c.MustGet("username").(string) c.JSON(http.StatusOK, gin.H{"message": "Welcome to the protected route, " + username}) }

Go JWT authentication Gin framework API security Golang tutorials JSON Web Token