From a2282fbf77b8c4004f0a97195bed3ce5b586091a Mon Sep 17 00:00:00 2001 From: Kunal Varkekar Date: Wed, 18 Jan 2023 16:39:07 -0500 Subject: [PATCH] Fixed bug in AddUser function in the loginDatabase. Also added signup functionality and the function for it in the loginController --- controllers/loginController.go | 31 +++++++++++++++++++++++++------ database/loginDatabase.go | 15 ++++++++------- main.go | 2 +- models/userModel.go | 2 +- static/credentials.js | 2 +- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/controllers/loginController.go b/controllers/loginController.go index 64c5e1b..72adbce 100644 --- a/controllers/loginController.go +++ b/controllers/loginController.go @@ -1,16 +1,35 @@ package controller import ( - "TODO/models" - "github.com/gofiber/fiber/v2" + + "TODO/database" + "TODO/models" + "fmt" ) // This function will be called through the JS and handle any signup requirements // the c variable here contains all the required credentials -func Signup(c *fiber.Ctx) error { +func Signup(ctx *fiber.Ctx) error { var creds models.User - return c.Status(fiber.StatusOK).JSON(fiber.Map{ - "success": true, - }) + // First we need to parse the variable ctx to receive the credentials + err := ctx.BodyParser(&creds) + if err != nil { + fmt.Println("Error with parsing credentials") + } + + // Once we have the required data, we need to make sure the user isn't a duplicate + err = database.AddUser(creds) + if err != nil { + // In this case, we know the user is a duplicate, so we returen an error message + return ctx.Status(fiber.StatusOK).JSON(fiber.Map{ + "success": false, + "message": "User already exists. Please login or use a different email address.", + }) + } else { + return ctx.Status(fiber.StatusOK).JSON(fiber.Map{ + "success": true, + "message": "Account created.", + }) + } } diff --git a/database/loginDatabase.go b/database/loginDatabase.go index e0cd6d5..f84be24 100644 --- a/database/loginDatabase.go +++ b/database/loginDatabase.go @@ -44,25 +44,26 @@ func ConnectToDB() { // The password will be hashed for security reasons func AddUser(userInfo models.User) error { // Get the pointer for the model - creds := &userInfo + //creds := &userInfo // First we check for any errors. If there are no errors when retrieving the user // from the database, it means that there exists an entry with that email already. // To prevent duplicate entries, we check for this and return the error - err := DB.Take(creds).Error + var tempUser models.User + err := DB.Where("email = ?", userInfo.Email).First(&tempUser).Error if err == nil { - return errors.New("There is already an account with this email. Please login instead.") + return errors.New("there is already an account with this email. please login instead") } // As the email does not exist in the database, we first hash it before adding it - // The 8 represents the cost of hashing. 8 is chosen arbitrarily // We also salt the password for extra security - hashedPass, err := bcrypt.GenerateFromPassword([]byte(userInfo.Password), 8) + hashedPass, err := bcrypt.GenerateFromPassword([]byte(userInfo.Password), bcrypt.DefaultCost) if err == nil { // If no errors, we can add the user info to the database userInfo.Password = string(hashedPass) - err := DB.Create(creds) - if err == nil { + err := DB.Create(&userInfo) + if err.Error == nil { return nil } + fmt.Println(err.Error) return err.Error } return err diff --git a/main.go b/main.go index 64bb9a8..7dd03f4 100644 --- a/main.go +++ b/main.go @@ -48,5 +48,5 @@ func main() { }) }) - app.Listen(":8080") + app.Listen("127.0.0.1:8080") } diff --git a/models/userModel.go b/models/userModel.go index ae1ebd7..a68e61e 100644 --- a/models/userModel.go +++ b/models/userModel.go @@ -7,7 +7,7 @@ import ( // The email is already validated through HTML type User struct { gorm.Model - ID uint `json:"id"` + ID uint `json:"id" sql:"AUTO_INCREMENT" gorm:"primaryKey"` Email string `json:"email` Password string `json:"password"` Tasks []Task `json:"tasks"` diff --git a/static/credentials.js b/static/credentials.js index 8e8c8f0..80d0576 100644 --- a/static/credentials.js +++ b/static/credentials.js @@ -46,7 +46,7 @@ function signupButton() { window.location.href = "/tasks"; } else { - window.alert("Error with signing up. Please try again.") + window.alert(result.message) } }) }