updating rest-api
This commit is contained in:
26
rest-api/middlewares/auth.go
Normal file
26
rest-api/middlewares/auth.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"example.com/rest-api/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Authenticate(context *gin.Context) {
|
||||
token := context.Request.Header.Get("Authorization")
|
||||
|
||||
if token == "" {
|
||||
context.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"message": "Not authorized"})
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := utils.VerifyToken(token)
|
||||
|
||||
if err != nil {
|
||||
context.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"message": "Not authorized"})
|
||||
}
|
||||
|
||||
context.Set("userId", userId)
|
||||
context.Next()
|
||||
}
|
||||
Reference in New Issue
Block a user