Search code examples
gogo-gin

GO Gin Keeps returning empty status if Any part of my code returns false


I am relatively new to Golang and Gin(framework)

I am writing some really simple API endpoints . But I notice something really weird about Gin, if there is any codition or function within my code that returns false the rest of my code or conditions does not get executed and Gin just returns a JSON with empty status :

{"status":""}

Here is a very simple code to explain what I mean

In a functions.go file I have :

func VerifyUserLogin(username,password,userAgent string) (string, string) {
    userData := Users{}
    userQueryColumn := "username=?"
    // if they are trying to login with email
    if nEmailHelpers.EmailIsValid(username) == true{
        userQueryColumn = "email=?"
    }
    if getUserData := db.Select("password").Where(userQueryColumn, strings.ToLower(username)).First(&userData); getUserData.Error != nil {
        //  Meaning there was an error, most likely no data found , so we just return false todo improve this error handling later to handle more specific errors

        return "", feedback["InvalidLogin"]
    } else {
        if getUserData.RowsAffected == 1 {
            if nSecurityHelpers.CheckPasswordHash(password, userData.Password)==true {
                token, tokenError := CreateToken(username, userAgent, false, 60)
                if tokenError == nil {
                    return token, feedback["ValidLogin"]

                } else {
                    return "", feedback["TokenNotGenerated"]

                }
            } else {
                return "", feedback["InvalidLogin"]
            }
        }
        return "", feedback["InvalidLogin"]

    }
}

In another go file that references the functions.go file I have :

func main(){
    router := gin.Default()
    router.POST ("login",loginUser)
    router.Run()
}

var feedback = userFeedback.Users()


// loginUser function to login a  user
func loginUser(c *gin.Context){
    requestBody := neielRequestsHelpers.RequestBody(c)
    username := requestBody["username"]
    password := requestBody["password"]
    userAgent := c.Request.Header.Get("User-Agent")
    token, loginMessage := userFunctions.VerifyUserLogin(username,password,userAgent)
    // todo come back and fix proper error message when user does not exist
    fmt.Println(loginMessage)
    if loginMessage==feedback["ValidLogin"]{
        c.JSON(http.StatusOK, gin.H{"status":loginMessage,"token":token})

    }else{
        c.JSON(http.StatusUnauthorized, gin.H{"status":feedback["InvalidLogin"]})

    }


}

If all my inputs are correct , all goes well (Username exists and password is correct). But I have to handle scenario where username or password is invalid .If for any reason getUserData or nSecurityHelpers.CheckPasswordHash() is false , or any function for that matter returns a boolean of false . The entire function just terminates and doesn't allow me handle the error the way I want and output custom JSON response. I just get this {"status":""}

I am 100% sure this issue is from Gin , but I don't know what to activate or deactivate to allow me handle errors on my own. I have read the docs, but its obvious I am missing something .

Kindly help me please .


Solution

  • I have resolved this, thanks to everyone that tried to help It was a typo error causing the issue "InValidLogin" instead of "InvalidLogin" in another file. It was really subtle