Exemple #1
0
func getUserFromJwt(webtoken, secret string) (*models.User, error) {
	token, err := jwt.Parse(webtoken, func(token *jwt.Token) (interface{}, error) {
		return []byte(secret), nil
	})

	if err != nil {
		return nil, err
	}

	user, err := models.FindUser(token.Claims["id"].(string))
	if err != nil {
		return nil, err
	}
	return user, nil
}
func UsersFindOne(c *gin.Context) {
	secret := os.Getenv("SECRET")
	token, err := jwt.ParseFromRequest(c.Request, func(token *jwt.Token) (interface{}, error) {
		return []byte(secret), nil
	})

	if err != nil {
		c.Fail(401, err)
	}

	user, err := models.FindUser(token.Claims["id"].(string))

	if err != nil {
		c.Fail(401, err)
	}

	c.JSON(200, gin.H{"user": user})
}
Exemple #3
0
func Auth(secret string) gin.HandlerFunc {
	return func(c *gin.Context) {
		token, err := jwt.ParseFromRequest(c.Request, func(token *jwt.Token) (interface{}, error) {
			return []byte(secret), nil
		})

		if err != nil {
			c.Fail(401, err)
		}

		user, err := models.FindUser(token.Claims["id"].(string))
		if err != nil {
			c.Fail(401, err)
		}

		c.Set("user", user)

		c.Next()

		models.TouchUser(user.Id)
	}
}