-1

Here is my authController.go file

    package controllers
    
    import (
        "strconv"
    
        "github.com/ADEMOLA200/go-admin/database"
        "github.com/ADEMOLA200/go-admin/models"
        "github.com/dgrijalva/jwt-go"
        "github.com/gofiber/fiber/v2"
        "golang.org/x/crypto/bcrypt"
        "time"
    )
    
    func Register(c *fiber.Ctx) error {
    
        var data map[string]string
    
        if err := c.BodyParser(&data); err != nil {
    
            return err
        }
    
        if data["password"] != data["password-confirm"] {
            c.Status(400)
            return c.JSON(fiber.Map{
                "message": "password do not match",
            })
        }
    
    /********************** HASH PASSWORD ********************************************/
        password, _ := bcrypt.GenerateFromPassword([]byte(data["password"]), 14)
    /*********************************************************************************/
        user := models.User{
            FirstName: data["first_name"],
            LastName: data["last_name"],
            Email: data["email"],
            Password: password,
        }
    
        database.DB.Create(&user)
    
        return c.JSON(user)
    
    }
    
    func Login(c *fiber.Ctx) error {
        var data map[string]string
    
        if err := c.BodyParser(&data); err != nil {
    
            return err
        }
    
        var user models.User
    
        database.DB.Where("email = ?", data["email"]).First(&user)
    
        if user.Id == 0 {
            c.Status(404)
            return c.JSON(fiber.Map{
                "message": "incorrect email or no user found",
            })
        }
    
        if err := bcrypt.CompareHashAndPassword(user.Password, []byte(data["password"])); err != nil {
            c.Status(400)
            return c.JSON(fiber.Map{
                "message": "incorrect password",
            })
        }
    
        claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
            Issuer: strconv.Itoa(int(user.Id)),
            ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
        })
    
        token, err := claims.SignedString([]byte("secret"))
    
        if err != nil {
            return c.SendStatus(fiber.StatusInternalServerError)
        }
    
        cookie := fiber.Cookie{
            Name: "jwt",
            Value: token,
            Expires:  time.Now().Add(time.Hour * 24),
            HTTPOnly: true,
        }
    
        c.Cookie(&cookie)
    
        return c.JSON(fiber.Map{
            "message": "success",
        })
    
    }
    
    type Claims struct {
        jwt.StandardClaims
    }
    
    func User(c *fiber.Ctx) error {
        cookie := c.Cookies("jwt")
    
        token, err := jwt.ParseWithClaims(cookie, &Claims{}, func(token *jwt.Token) (interface{}, error) {
            return []byte("secret"), nil
        })
    
        if err != nil || !token.Valid {
            c.Status(fiber.StatusUnauthorized)
            return c.JSON(fiber.Map{
                "message": "unauthenticated",
            })
        }
    
        claims := token.Claims
    
        return c.JSON(claims)
    }

And this is the error it gives in the terminal

    # github.com/gofiber/fiber
    ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam
    failed to build, error: exit status 1
    # github.com/gofiber/fiber
    ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam
    # github.com/gofiber/fiber
    ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam
    # github.com/gofiber/fiber
    ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam
    # github.com/gofiber/fiber
    ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam

This is my go.mod file

    module github.com/ADEMOLA200/go-admin
    
    go 1.20
    
    require (
        github.com/gofiber/cors v0.2.2
        github.com/gofiber/fiber/v2 v2.48.0
        golang.org/x/crypto v0.11.0
        gorm.io/driver/mysql v1.5.1
        gorm.io/gorm v1.25.2
    
    )
    
    require (
        github.com/gofiber/fiber v1.14.6 // indirect
        github.com/gofiber/utils v1.1.0 // indirect
        github.com/gorilla/schema v1.2.0 // indirect
    )
    
    require (
        github.com/andybalholm/brotli v1.0.5 // indirect
        github.com/dgrijalva/jwt-go v3.2.0+incompatible
        github.com/go-sql-driver/mysql v1.7.1 // indirect
        github.com/google/uuid v1.3.0 // indirect
        github.com/jinzhu/inflection v1.0.0 // indirect
        github.com/jinzhu/now v1.1.5 // indirect
        github.com/klauspost/compress v1.16.7 // indirect
        github.com/mattn/go-colorable v0.1.13 // indirect
        github.com/mattn/go-isatty v0.0.19 // indirect
        github.com/mattn/go-runewidth v0.0.15 // indirect
        github.com/rivo/uniseg v0.4.4 // indirect
        github.com/valyala/bytebufferpool v1.0.0 // indirect
        github.com/valyala/fasthttp v1.48.0 // indirect
        github.com/valyala/tcplisten v1.0.0 // indirect
        golang.org/x/sys v0.10.0 // indirect
    )




I clicked on the go fiber package which took me to the `path.go` file in the go fiber and I think the error is from this line in the code

paramName := utils.GetTrimmedParam(processedPart)

I tried a clean install of the go fiber but it didn't solve the problem

I tried a clean install of the go fiber package but it didn't fix the error.

I tried a debugging and doing a clean install of the go fiber package but it didn't solve the problem

  • 2
    You have a compiling error, which is not related to an http `GET /api/user`. You are trying to use two incompatible versions if the gofiber project. It's probably the `github.com/gofiber/cors` package, their [repo says it's deprecated](https://github.com/gofiber/cors), and you should Fiber v2. – JimB Jul 31 '23 at 17:22

1 Answers1

2

In my main.go file, my fiber/v2 package was dependent, so I had to do this:

package main

import (
    fiber "github.com/gofiber/fiber/v2"

    "github.com/ADEMOLA200/go-admin/database"
    "github.com/ADEMOLA200/go-admin/routes"
    
    "github.com/gofiber/fiber/v2/middleware/cors"
)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83