Basic JWT is now working #12

Merged
jtom38 merged 12 commits from features/jwt/maybe into main 2024-03-29 14:51:01 -07:00
2 changed files with 22 additions and 3 deletions
Showing only changes of commit f78d78d061 - Show all commits

View File

@ -2,6 +2,7 @@ package repositories
import ( import (
"database/sql" "database/sql"
"errors"
"fmt" "fmt"
"go-cook/api/models" "go-cook/api/models"
"time" "time"
@ -12,8 +13,17 @@ import (
const ( const (
TableName string = "users" TableName string = "users"
ErrUserNotFound string = "requested user was not found"
) )
type IUserTable interface {
GetByName(name string) (models.UserModel, error)
Create(name, password string) (int64, error)
Update(id int, entity models.UserModel) error
UpdatePassword(name, password string) error
CheckUserHash(name, password string) error
}
// Creates a new instance of UserRepository with the bound sql // Creates a new instance of UserRepository with the bound sql
func NewUserRepository(conn *sql.DB) UserRepository { func NewUserRepository(conn *sql.DB) UserRepository {
return UserRepository{ return UserRepository{
@ -37,10 +47,15 @@ func (ur UserRepository) GetByName(name string) (models.UserModel, error) {
return models.UserModel{}, err return models.UserModel{}, err
} }
return ur.processRows(rows)[0], nil data := ur.processRows(rows)
if (len(data) == 0) {
return models.UserModel{}, errors.New(ErrUserNotFound)
}
return data[0], nil
} }
func (ur UserRepository) NewUser(name, password string) (int64, error) { func (ur UserRepository) Create(name, password string) (int64, error) {
passwordBytes := []byte(password) passwordBytes := []byte(password)
hash, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost) hash, err := bcrypt.GenerateFromPassword(passwordBytes, bcrypt.DefaultCost)
if err != nil { if err != nil {
@ -62,6 +77,10 @@ func (ur UserRepository) NewUser(name, password string) (int64, error) {
return 1, nil return 1, nil
} }
func (ur UserRepository) Update(id int, entity models.UserModel) error {
return errors.New("not implemented")
}
func (ur UserRepository) UpdatePassword(name, password string) error { func (ur UserRepository) UpdatePassword(name, password string) error {
_, err := ur.GetByName(name) _, err := ur.GetByName(name)
if err != nil { if err != nil {

View File

@ -20,7 +20,7 @@ func TestCanCreateNewUser(t *testing.T) {
defer db.Close() defer db.Close()
repo := repositories.NewUserRepository(db) repo := repositories.NewUserRepository(db)
updated, err := repo.NewUser("testing", "NotSecure") updated, err := repo.Create("testing", "NotSecure")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
t.FailNow() t.FailNow()