feat: jwt middleware
This commit is contained in:
29
middleware/auth.go
Normal file
29
middleware/auth.go
Normal 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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user