fix middleware logic error
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -38,45 +38,47 @@ func JwtAuthMiddleware() gin.HandlerFunc {
|
|||||||
|
|
||||||
func JwtAuthAdminMiddleware() gin.HandlerFunc {
|
func JwtAuthAdminMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
/*
|
|
||||||
err := token.TokenValid(c)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("JwtAuthAdminMiddleware token is not valid : '%s'\n", err)
|
|
||||||
c.String(http.StatusUnauthorized, "Unauthorized")
|
|
||||||
c.Abort()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once we know the token is valid, figure out if this user is an admin
|
err := token.TokenValid(c)
|
||||||
user_id, err := token.ExtractTokenID(c)
|
if err != nil {
|
||||||
|
log.Printf("JwtAuthAdminMiddleware token is not valid : '%s'\n", err)
|
||||||
if err != nil {
|
c.String(http.StatusUnauthorized, "Unauthorized")
|
||||||
log.Printf("JwtAuthAdminMiddleware could not extract user ID from context : '%s'\n", err)
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
||||||
c.Abort()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Printf("JwtAuthAdminMiddleware determined user id as '%v'\n", user_id)
|
|
||||||
*/
|
|
||||||
|
|
||||||
//user_id := c.GetInt("user-id")
|
|
||||||
var user_id int
|
|
||||||
if val, ok := c.Get("user-id"); !ok {
|
|
||||||
log.Printf("JwtAuthAdminMiddleware : user-id not in context. Keys : '%+v'\n", c.Keys)
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "error determining user id"})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
user_id = val.(int)
|
|
||||||
}
|
|
||||||
|
|
||||||
if user_id == 0 {
|
|
||||||
errString := "could not extract user ID from context"
|
|
||||||
log.Printf("JwtAuthAdminMiddleware '%s'\n", errString)
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": errString})
|
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Once we know the token is valid, figure out if this user is an admin
|
||||||
|
user_id, err := token.ExtractTokenID(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("JwtAuthAdminMiddleware could not extract user ID from context : '%s'\n", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("JwtAuthAdminMiddleware determined user id as '%v'\n", user_id)
|
||||||
|
c.Set("user-id", user_id)
|
||||||
|
|
||||||
|
/*
|
||||||
|
//user_id := c.GetInt("user-id")
|
||||||
|
var user_id int
|
||||||
|
if val, ok := c.Get("user-id"); !ok {
|
||||||
|
log.Printf("JwtAuthAdminMiddleware : user-id not in context. Keys : '%+v'\n", c.Keys)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "error determining user id"})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
user_id = val.(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
if user_id == 0 {
|
||||||
|
errString := "could not extract user ID from context"
|
||||||
|
log.Printf("JwtAuthAdminMiddleware '%s'\n", errString)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": errString})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// TODO determine user role
|
// TODO determine user role
|
||||||
|
|
||||||
//ur, err := models.GetUserRoleByID(user_id)
|
//ur, err := models.GetUserRoleByID(user_id)
|
||||||
|
Reference in New Issue
Block a user