improve checks

This commit is contained in:
2023-04-03 08:33:31 +10:00
parent b45e276df5
commit 748f4251e1
9 changed files with 139 additions and 43 deletions

View File

@@ -36,6 +36,19 @@ func Register(c *gin.Context) {
u.UserName = input.Username
u.Password = input.Password
//remove spaces in username
u.UserName = html.EscapeString(strings.TrimSpace(u.UserName))
// Check if user already exists
testUser, _ := models.GetUserByName(u.UserName)
fmt.Printf("Register checking if user already exists : '%v'\n", testUser)
if (models.User{} == testUser) {
fmt.Printf("Register confirmed no existing username\n")
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": "Attempt to register conflicting username"})
return
}
//turn password into hash
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
if err != nil {
@@ -46,9 +59,6 @@ func Register(c *gin.Context) {
}
u.Password = string(hashedPassword)
//remove spaces in username
u.UserName = html.EscapeString(strings.TrimSpace(u.UserName))
_, err = u.SaveUser()
if err != nil {