Creating the auth and user database functions

This commit is contained in:
vinchent 2024-07-19 09:44:27 +02:00
parent 3e70a5727f
commit d32fd432d2
3 changed files with 85 additions and 0 deletions

View File

@ -2,8 +2,11 @@ package dbrepo
import (
"context"
"errors"
"go-udemy-web-1/internal/models"
"time"
"golang.org/x/crypto/bcrypt"
)
func (m *postgresDBRepo) AllUsers() bool {
@ -146,3 +149,65 @@ func (m *postgresDBRepo) GetRoomById(id int) (models.Room, error) {
}
return room, nil
}
// GetUserByID gets a user by id
func (m *postgresDBRepo) GetUserByID(id int) (models.User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `select id, first_name, last_name, email, password, access_level, created_at, updated_at
from users where id = $1`
row := m.DB.QueryRowContext(ctx, query, id)
var u models.User
err := row.Scan(
&u.ID,
&u.FirstName,
&u.LastName,
&u.Email,
&u.Password,
&u.AccessLevel,
&u.CreatedAt,
&u.UpdatedAt,
)
if err != nil {
return models.User{}, err
}
return u, nil
}
// UpdateUser updates a user in the database
func (m *postgresDBRepo) UpdateUser(u models.User) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `update users set first_name = $1, last_name = $2, email = $3, access_level = $4, updated_at = $5`
_, err := m.DB.ExecContext(ctx, query, u.FirstName, u.LastName, u.Email, u.AccessLevel, time.Now())
if err != nil {
return err
}
return nil
}
// Authenticate authenticates a user
func (m *postgresDBRepo) Authenticate(email, testPassword string) (int, string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var id int
var hashedPassword string
row := m.DB.QueryRowContext(ctx, "select id, password from users where email = $1", email)
err := row.Scan(&id, &hashedPassword)
if err != nil {
return id, "", err
}
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(testPassword))
if err == bcrypt.ErrMismatchedHashAndPassword {
return 0, "", errors.New("incorrect password")
} else if err != nil {
return 0, "", err
}
return id, hashedPassword, nil
}

View File

@ -64,3 +64,20 @@ func (m *testDBRepo) GetRoomById(id int) (models.Room, error) {
}
return room, nil
}
// GetUserByID gets a user by id
func (m *testDBRepo) GetUserByID(id int) (models.User, error) {
var u models.User
return u, nil
}
// UpdateUser updates a user in the database
func (m *testDBRepo) UpdateUser(u models.User) error {
return nil
}
// Authenticate authenticates a user
func (m *testDBRepo) Authenticate(email, testPassword string) (int, string, error) {
return 1, "", nil
}

View File

@ -13,4 +13,7 @@ type DatabaseRepo interface {
SearchAvailabilityByDatesByRoomID(start, end time.Time, roomID int) (bool, error)
SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error)
GetRoomById(id int) (models.Room, error)
GetUserByID(id int) (models.User, error)
UpdateUser(u models.User) error
Authenticate(email, testPassword string) (int, string, error)
}