Skip to content

Commit

Permalink
Fixed bug in AddUser function in the loginDatabase. Also added signup…
Browse files Browse the repository at this point in the history
… functionality and the function for it in the loginController
  • Loading branch information
kuki1029 committed Jan 18, 2023
1 parent 27fd7af commit a2282fb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
31 changes: 25 additions & 6 deletions controllers/loginController.go
Original file line number Diff line number Diff line change
@@ -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.",
})
}
}
15 changes: 8 additions & 7 deletions database/loginDatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ func main() {
})
})

app.Listen(":8080")
app.Listen("127.0.0.1:8080")
}
2 changes: 1 addition & 1 deletion models/userModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion static/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function signupButton() {
window.location.href = "/tasks";
}
else {
window.alert("Error with signing up. Please try again.")
window.alert(result.message)
}
})
}
Expand Down

0 comments on commit a2282fb

Please sign in to comment.