-
Notifications
You must be signed in to change notification settings - Fork 0
/
me.go
35 lines (29 loc) · 869 Bytes
/
me.go
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
33
34
35
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
)
// me handler for user details
func makeMeHandler(users *mongo.Collection) gin.HandlerFunc {
return func(c *gin.Context) {
// Get the JWT token from cookie
token, err := c.Cookie("token")
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"message": "Unable to get JWT token from cookie"})
return
}
code, claim := processClaim(token)
if code != http.StatusOK {
c.AbortWithStatusJSON(code, gin.H{"message": "Unable to process JWT token"})
return
}
status, user := authAndAuthorised(users, claim)
if status != http.StatusOK {
c.AbortWithStatusJSON(status, gin.H{"message": "Unable to authenticate and authorise user"})
return
}
// Return the user details
c.JSON(http.StatusOK, gin.H{"user": user})
}
}