func checkToken() gin.HandlerFunc {
	return func(c *gin.Context) {
		// If there's an error or if the token is empty
		// the user is not logged in
		cookie, err := c.Cookie("valuevillages")
		if err != nil {
			setSession(c, "", "")
			return
		}

		token, err := jwt.ParseWithClaims(cookie, &TokenClaims{}, func(token *jwt.Token) (interface{}, error) {
			// Don't forget to validate the alg is what you expect:
			if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
				return nil, errors.New("Unexpected signing method: " + token.Header["alg"].(string))
			}

			// Return signing key to check the token
			return tokenSigningKey, nil
		})

		if claims, ok := token.Claims.(*TokenClaims); ok && token.Valid {
			c.Set("username", claims.Username)
			c.Set("role", claims.Role)
			if claims.Username != "" {
				c.Set("isLoggedIn", true)
			} else {
				c.Set("isLoggedIn", false)
			}

		} else {
			c.Set("username", "")
			c.Set("role", "")
			c.Set("isLoggedIn", false)
			fmt.Println("Redirecting to /log-in from check token")
			c.Redirect(http.StatusFound, "/about")
		}
	}
}
// This middleware ensures that a request will be aborted with an error if the user is not logged in
// This method checks if the user if logged in
This middleware ensures that a request will be aborted with an error if the user is not logged in
This method checks if the user if logged in
// This middleware ensures that a request will be aborted with an error if the user is not logged in // This method checks if the user if logged in
func checkToken() gin.HandlerFunc {
	return func(c *gin.Context) {
		cookie, err := c.Cookie("valuevillages")
		if err != nil {
			setSession(c, "", "")
			return
		}
		token, err := jwt.ParseWithClaims(cookie, &TokenClaims{}, func(token *jwt.Token) (interface{}, error) {
			if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
				return nil, errors.New("Unexpected signing method: " + token.Header["alg"].(string))
			}
			return tokenSigningKey, nil
		})
		if claims, ok := token.Claims.(*TokenClaims); ok && token.Valid {
			c.Set("username", claims.Username)
			c.Set("role", claims.Role)
			if claims.Username != "" {
				c.Set("isLoggedIn", true)
			} else {
				c.Set("isLoggedIn", false)
			}
		} else {
			c.Set("username", "")
			c.Set("role", "")
			c.Set("isLoggedIn", false)
			fmt.Println("Redirecting to /log-in from check token")
			c.Redirect(http.StatusFound, "/about")
		}
	}
}
func checkToken ( ) gin . HandlerFunc { return func ( c * gin . Context ) { cookie , err := c . Cookie ( " " ) if err != nil { setSession ( c , " " , " " ) return } token , err := jwt . ParseWithClaims ( cookie , & TokenClaims { } , func ( token * jwt . Token ) ( interface { } , error ) { if _ , ok := token . Method . ( * jwt . SigningMethodHMAC ) ; ! ok { return nil , errors . New ( " " + token . Header [ " " ] . ( string ) ) } return tokenSigningKey , nil } ) if claims , ok := token . Claims . ( * TokenClaims ) ; ok && token . Valid { c . Set ( " " , claims . Username ) c . Set ( " " , claims . Role ) if claims . Username != " " { c . Set ( " " , true ) } else { c . Set ( " " , false ) } } else { c . Set ( " " , " " ) c . Set ( " " , " " ) c . Set ( " " , false ) fmt . Println ( " " ) c . Redirect ( http . StatusFound , " " ) } } }
This middleware ensures that a request will be aborted with an error if the user is not logged in
This method checks if the user if logged in
This middleware ensures that a request will be aborted with an error if the user is not logged in This method checks if the user if logged in
// If there's an error or if the token is empty // the user is not logged in // Don't forget to validate the alg is what you expect: // Return signing key to check the token