feat: jwt middleware

This commit is contained in:
fallen-angle
2022-02-12 16:32:34 +08:00
parent 2d43931fc8
commit 72ef5c92c4
10 changed files with 83 additions and 33 deletions

29
middleware/auth.go Normal file
View File

@@ -0,0 +1,29 @@
package middleware
import (
"github.com/gin-gonic/gin"
"nCovTrack-Backend/utils"
"net/http"
)
const UNAUTH_MSG = "unauthorized"
func Auth() gin.HandlerFunc {
return func(c *gin.Context) {
oldToken := c.Request.Header["Token"]
c.Writer.Header().Set("X-Token", "")
if len(oldToken) != 1 || oldToken[0] == "" {
utils.Err(c, http.StatusUnauthorized, http.StatusUnauthorized, UNAUTH_MSG)
c.Abort()
return
}
renewToken := utils.RenewToken(oldToken[0])
if renewToken == "" {
utils.Err(c, http.StatusUnauthorized, http.StatusUnauthorized, UNAUTH_MSG)
c.Abort()
return
}
c.Writer.Header().Set("X-Token", renewToken)
c.Next()
}
}