Monday, May 20, 2024
HomeGolangSwitch the end result from a operate to a different operate -...

Switch the end result from a operate to a different operate – Getting Assist


There are two capabilities in a single bundle.

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

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

    tknStr := c.Worth
    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.Legitimate {
        w.WriteHeader(http.StatusUnauthorized)
        return
    }

    return
}

results of func Artwork Token() in func Creativity()
error staticcheck: articlecreativity.go:25:22: ArtToken(w, r) (no worth) used as worth

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

    tpl = template.Should(template.ParseFiles("./tpl/artwork/creativity.html"))

    if r.Technique == "POST" {
        person := 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, person.Title, person.Description, claims.User_id, time.Now())
        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            panic(err)
        }
    }
    tpl.Execute(w, nil)
}

The ArtToken operate doesn’t return something, however when it’s referred to as within the Creativity operate its (non-existent) return worth is assigned to claims. I’m guessing that you just supposed to return a Claims object from ArtToken.

So one thing like this

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

That’s proper, I supposed to return the Claims object from the Artwork Token.
However with

return claims
articlemain.go:74:12: too many return values
        have (*Claims)
        need ()

All the identical, however with

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

It really works nice

too many return values

That’s as a result of it is advisable to declare that ArtToken returns *Claims

substitute

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

with

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

arttoken

articlemain.go:48:13: not sufficient return values
        have ()
        need (*Claims)
articlemain.go:51:9: not sufficient return values
        have ()
        need (*Claims)
articlemain.go:64:13: not sufficient return values
        have ()
        need (*Claims)
articlemain.go:67:9: not sufficient return values
        have ()
        need (*Claims)
articlemain.go:71:9: not sufficient return values
        have ()
        need (*Claims)
sort Claims struct {
    User_id int `json:"user_id"`
    E mail string `json:"electronic mail"`
    jwt.RegisteredClaims
}

Your whole return statements within the ArtToken operate might want to return a pointer to a Declare object, or nil. It seems that presently lots of them don’t return something.

One other factor, I discovered your parameter w in ArtToken go in by worth not by reference
so that you WriteHead in ArtToken doesn’t change something to your important operate

The primary error was: referencing the operate (Technique == “GET”) within the operate (Technique== “POST”) . The second is for every thing that you’ve indicated… However the principle factor is

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

incorrect assertion:

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments