Wednesday, 19 March 2025

restruct handler

 package controller 


import (

"net/http"

"os"

"time"

"github.com/gin-gonic/gin"

"github.com/golang-jwt/jwt/v5"

)


var jwtSignKey = []byte(os.Getenv("SERVER_KEY"))


func HandleJWTToken(c *gin.Context) {


// generate token expires 10 hours

token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{

"exp": time.Now().Add(10 * time.Hour).Unix(),

"userName": os.Getenv("BASIC_AUTH_USER"),

})

// sign token with private key

tokenString, err := token.SignedString(jwtSignKey)

if err != nil {

c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to sign JWT"})

return

}

c.JSON(http.StatusOK, gin.H{"token": tokenString})


}

No comments:

Post a Comment