You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetUser() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
tokenCookie, err := c.Cookie("token")
|
|
|
|
if err != nil || tokenCookie == "" {
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
claims, err := VerifyJWT(tokenCookie)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Set("user_id", claims["user_id"])
|
|
|
|
c.Set("username", claims["username"])
|
|
|
|
c.Set("user_email", claims["email"])
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|