Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/register time #35

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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