Last active
November 14, 2020 06:09
-
-
Save krittawatcode/087a38efeb03d7f7563d8f122666b8b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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