refactor: use dependency injection

This commit is contained in:
Stavros
2025-01-21 18:41:06 +02:00
parent 2988b5f22f
commit 5e73d06fcc
6 changed files with 113 additions and 42 deletions

View File

@@ -6,8 +6,18 @@ import (
"golang.org/x/crypto/bcrypt"
)
func FindUser(userList types.UserList, username string) (*types.User) {
for _, user := range userList.Users {
func NewAuth(userList types.Users) *Auth {
return &Auth{
Users: userList,
}
}
type Auth struct {
Users types.Users
}
func (auth *Auth) GetUser(username string) *types.User {
for _, user := range auth.Users {
if user.Username == username {
return &user
}
@@ -15,7 +25,7 @@ func FindUser(userList types.UserList, username string) (*types.User) {
return nil
}
func CheckPassword(user types.User, password string) bool {
func (auth *Auth) CheckPassword(user types.User, password string) bool {
hashedPasswordErr := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
return hashedPasswordErr == nil
}