-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddlewares.go
More file actions
32 lines (24 loc) · 787 Bytes
/
middlewares.go
File metadata and controls
32 lines (24 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package gah
import (
"net/http"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// AuthRequiredMiddleware // AuthRequiredMiddleware() middleware just in the "authorized"
func AuthRequiredMiddleware(c *gin.Context) {
userID, userIDErr := primitive.ObjectIDFromHex(c.GetHeader("X-User-Id"))
authToken := c.GetHeader("X-Auth-Token")
if userIDErr != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrorMessageResponse("You must be logged in to do this."))
return
}
_, authErr := GetUserByToken(userID, authToken)
if authErr != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrorMessageResponse("You must be logged in to do this."))
return
}
// set userID
c.Set("userID", userID)
// Pass on to the next-in-chain
c.Next()
}