improve checks
This commit is contained in:
@@ -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 {
|
||||
|
Reference in New Issue
Block a user