27 lines
526 B
Go
27 lines
526 B
Go
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()
|
|
}
|