Skip to content

Commit

Permalink
Fixed nil pointer on database
Browse files Browse the repository at this point in the history
  • Loading branch information
Ju-Wiluis William Rodriguez Hernandez committed May 29, 2024
1 parent 22b6559 commit c70d0db
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ func open() error {
return nil
}

func closeDB() {
if db != nil {
db.Close()
}
}

func ValidateUser(oid, username, password string) (bool, error) {
defer db.Close()
if err := open(); err != nil {
fmt.Printf("Error initializing database connection: %s", err.Error())
return false, err
}
defer closeDB() // Cierra la conexión solo si se abrió correctamente

var storedPassword string
err := db.QueryRow("SELECT password FROM credentials WHERE oid=$1, username=$2", oid, username).Scan(&storedPassword)
err := db.QueryRow("SELECT password FROM credentials WHERE oid=$1 AND username=$2", oid, username).Scan(&storedPassword)
if err != nil {
println("Error al consultar usuario: ", err.Error())
if err == sql.ErrNoRows {
Expand All @@ -63,11 +69,11 @@ func ValidateUser(oid, username, password string) (bool, error) {
}

func InsertUser(oid, username, password string) error {
defer db.Close()
if err := open(); err != nil {
fmt.Errorf("No se pudo establecer conexion con la base de datos")
return err
}
defer closeDB() // Cierra la conexión solo si se abrió correctamente

// Encrypt the password
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
Expand Down

0 comments on commit c70d0db

Please sign in to comment.