"golang jwt example" Code Answer's
You're definitely familiar with the best coding language Go that developers use to develop their projects and they get all their queries like "golang jwt example" answered properly. Developers are finding an appropriate answer about golang jwt example related to the Go coding language. By visiting this online portal developers get answers concerning Go codes question like golang jwt example. Enter your desired code related query in the search bar and get every piece of information about Go code related question on golang jwt example.
golang jwt example
package main
import "fmt"
// example usage
func main() {
userData := map[string]interface{}{"id": 1, "email": "[email protected]", "github_name": "restuwahyu13"}
accessToken, err := util.Sign(userData, "JWT_SECRET", 1) // data -> secretkey env name -> expiredAt
fmt.Println("my accessToken here", accessToken)
}
package util
import (
"encoding/json"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// metadata for your jwt
type MetaToken struct {
ID int
Email string
ExpiredAt time.Time
Authorization bool
}
type AccessToken struct {
Claims MetaToken
}
func Sign(Data map[string]interface{}, SecrePublicKeyEnvName string, ExpiredAt time.Duration) (string, error) {
expiredAt := time.Now().Add(time.Duration(time.Second) * ExpiredAt).Unix()
jwtSecretKey := GodotEnv(SecrePublicKeyEnvName)
// metadata for your jwt
claims := jwt.MapClaims{}
claims["expiredAt"] = expiredAt
claims["authorization"] = true
for i, v := range Data {
claims[i] = v
}
to := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
accessToken, err := to.SignedString([]byte(jwtSecretKey))
if err != nil {
logrus.Error(err.Error())
return accessToken, err
}
return accessToken, nil
}
func VerifyTokenHeader(ctx *gin.Context, SecrePublicKeyEnvName string) (*jwt.Token, error) {
tokenHeader := ctx.GetHeader("Authorization")
accessToken := strings.SplitAfter(tokenHeader, "Bearer")[1]
jwtSecretKey := GodotEnv(SecrePublicKeyEnvName)
token, err := jwt.Parse(strings.Trim(accessToken, " "), func(token *jwt.Token) (interface{}, error) {
return []byte(jwtSecretKey), nil
})
if err != nil {
logrus.Error(err.Error())
return nil, err
}
return token, nil
}
func VerifyToken(accessToken, SecrePublicKeyEnvName string) (*jwt.Token, error) {
jwtSecretKey := GodotEnv(SecrePublicKeyEnvName)
token, err := jwt.Parse(accessToken, func(token *jwt.Token) (interface{}, error) {
return []byte(jwtSecretKey), nil
})
if err != nil {
logrus.Error(err.Error())
return nil, err
}
return token, nil
}
func DecodeToken(accessToken *jwt.Token) AccessToken {
var token AccessToken
stringify, _ := json.Marshal(&accessToken)
json.Unmarshal([]byte(stringify), &token)
return token
}
All those coders who are working on the Go based application and are stuck on golang jwt example can get a collection of related answers to their query. Programmers need to enter their query on golang jwt example related to Go code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about golang jwt example for the programmers working on Go code while coding their module. Coders are also allowed to rectify already present answers of golang jwt example while working on the Go language code. Developers can add up suggestions if they deem fit any other answer relating to "golang jwt example". Visit this developer's friendly online web community, CodeProZone, and get your queries like golang jwt example resolved professionally and stay updated to the latest Go updates.