Skip to content

Instantly share code, notes, and snippets.

@krittawatcode
Last active November 14, 2020 06:09
Show Gist options
  • Save krittawatcode/087a38efeb03d7f7563d8f122666b8b7 to your computer and use it in GitHub Desktop.
Save krittawatcode/087a38efeb03d7f7563d8f122666b8b7 to your computer and use it in GitHub Desktop.
package middleware
import (
"fmt"
"github/krittawatcode/go-jwt/src/service"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
func AuthorizeJWT() gin.HandlerFunc {
return func(c *gin.Context) {
const BEARER_SCHEMA = "Bearer "
authHeader := c.GetHeader("Authorization")
tokenString := authHeader[len(BEARER_SCHEMA):]
token, err := service.JWTAuthService().ValidateToken(tokenString)
if token.Valid {
claims := token.Claims.(jwt.MapClaims)
fmt.Println(claims)
} else {
fmt.Println("testing")
fmt.Println(err)
c.AbortWithStatus(http.StatusUnauthorized)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment