Skip to content

Commit

Permalink
Merge pull request #35 from Lindharden/fix/register-time
Browse files Browse the repository at this point in the history
Fix/register time
  • Loading branch information
bitknox authored Feb 28, 2023
2 parents b4b683b + 8eb178c commit a565cb1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
18 changes: 9 additions & 9 deletions helpers/authenticationHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ func RegisterUser(db *sqlx.DB, username string, password string, password2 strin
return model.User{}, errors.New("you have to enter a value")
}

if !checkUserPasswords(password, password2) {
if !CheckUserPasswords(password, password2) {
return model.User{}, errors.New("the two passwords do not match")
}

if !checkUserEmail(email) {
if !CheckUserEmail(email) {
return model.User{}, errors.New("you have to enter a valid email address")
}

if checkUsernameExists(db, username) {
if CheckUsernameExists(db, username) {
return model.User{}, errors.New("the username is already taken")
}

pw_hash, err := hashPassword(password)
pw_hash, err := HashPassword(password)
if err != nil {
panic("password hashing failed")
}
Expand All @@ -38,20 +38,20 @@ func RegisterUser(db *sqlx.DB, username string, password string, password2 strin
return model.User{Username: username, UserId: id, Email: email, PwHash: pw_hash}, err
}

func checkUserPasswords(password, password2 string) bool {
func CheckUserPasswords(password, password2 string) bool {
return password == password2
}

func checkUsernameExists(db *sqlx.DB, username string) bool {
func CheckUsernameExists(db *sqlx.DB, username string) bool {
_, err := GetUserId(db, username)
return err == nil
}

func checkUserEmail(email string) bool {
func CheckUserEmail(email string) bool {
return strings.Contains(email, "@")
}

func hashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 10)
return string(bytes), err
}
18 changes: 0 additions & 18 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
"golang.org/x/crypto/bcrypt"
)

func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}

func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
Expand All @@ -31,15 +26,6 @@ func EmptyUserPass(username, password string) bool {
return strings.Trim(username, " ") == "" || strings.Trim(password, " ") == ""
}

func CheckUserPasswords(password, password2 string) bool {
return password == password2
}

func CheckUsernameExists(db *sqlx.DB, username string) bool {
_, err := GetUserId(db, username)
return err == nil
}

func ValidatePassword(db *sqlx.DB, username, password string) bool {
var pw_hash string
err := db.QueryRow("select pw_hash from user where username = ?", username).Scan(&pw_hash)
Expand All @@ -50,10 +36,6 @@ func ValidatePassword(db *sqlx.DB, username, password string) bool {
return CheckPasswordHash(password, pw_hash)
}

func CheckUserEmail(email string) bool {
return strings.Contains(email, "@")
}

func GetTypedDb(c *gin.Context) *sqlx.DB {
db := c.MustGet("db").(*sqlx.DB)
return db
Expand Down

0 comments on commit a565cb1

Please sign in to comment.