Transfer the result from a function to another function

There are two functions in one package.

func ArtToken(w http.ResponseWriter, r *http.Request) {

    c, err := r.Cookie("Visitor")
    if err != nil {
        if err == http.ErrNoCookie {
            w.WriteHeader(http.StatusUnauthorized)
            return
        }
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    tknStr := c.Value
    claims := &Claims{}

    token, err := jwt.ParseWithClaims(tknStr, claims, func(token *jwt.Token) (any, error) {
        return key, nil
    })

    if err != nil {
        if err == jwt.ErrSignatureInvalid {
            w.WriteHeader(http.StatusUnauthorized)
            return
        }
        w.WriteHeader(http.StatusBadRequest)
        return
    }
    if !token.Valid {
        w.WriteHeader(http.StatusUnauthorized)
        return
    }

    return
}

result of func Art Token() in func Creativity()
error staticcheck: article\creativity.go:25:22: ArtToken(w, r) (no value) used as value

// creativity.go
func Creativity(w http.ResponseWriter, r *http.Request) {

    tpl = template.Must(template.ParseFiles("./tpl/art/creativity.html"))

    if r.Method == "POST" {
        user := CreatArticle{
            Title: r.FormValue("title"),
            Description: r.FormValue("description"),
        }

        sqlStatement := `INSERT INTO article (title, description, author_id, created_at) VALUES ($1,$2,$3,$4)`

        var claims = ArtToken(w,r)

        _, err := db.Exec(sqlStatement, user.Title, user.Description, claims.User_id, time.Now())
        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            panic(err)
        }
    }
    tpl.Execute(w, nil)
}

The ArtToken function does not return anything, but when it is called in the Creativity function its (non-existent) return value is assigned to claims. I’m guessing that you intended to return a Claims object from ArtToken.

So something like this

func ArtToken(w http.ResponseWriter, r *http.Request) *Claims {
    ...
    ...
    return claims
}

That’s right, I intended to return the Claims object from the Art Token.
But with

return claims
article\main.go:74:12: too many return values
        have (*Claims)
        want ()

All the same, but with

    tpl = template.Must(template.ParseFiles("./tpl/auth.html"))
..
tpl.Execute(w, claims)

It works great

too many return values

That’s because you need to declare that ArtToken returns *Claims

replace

func ArtToken(w http.ResponseWriter, r *http.Request) {

with

func ArtToken(w http.ResponseWriter, r *http.Request) *Claims {

arttoken

article\main.go:48:13: not enough return values
        have ()
        want (*Claims)
article\main.go:51:9: not enough return values
        have ()
        want (*Claims)
article\main.go:64:13: not enough return values
        have ()
        want (*Claims)
article\main.go:67:9: not enough return values
        have ()
        want (*Claims)
article\main.go:71:9: not enough return values
        have ()
        want (*Claims)
type Claims struct {
    User_id int `json:"user_id"`
    Email string `json:"email"`
    jwt.RegisteredClaims
}

All of your return statements in the ArtToken function will need to return a pointer to a Claim object, or nil. It appears that currently many of them don’t return anything.

Another thing, I found your parameter w in ArtToken pass in by value not by reference
so you WriteHead in ArtToken does not change anything for your main function

The first error was: referencing the function (Method == “GET”) in the function (Method== “POST”) . The second is for everything that you have indicated… But the main thing is

    token, err := jwt.ParseWithClaims(tknStr, claims, func(token *jwt.Token) (interface{}, error) {
        return []byte(os.Getenv("JWT_SECRET")), nil
    })

incorrect statement:

..
var key = []byte(os.Getenv("JWT_SECRET"))
..
func
    token, err := jwt.ParseWithClaims(tknStr, claims, func(token *jwt.Token) (any, error) {
        return key, nil
    })