add login
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-11-21 16:02:36 +07:00
parent f2011b953c
commit 866009cf09
3 changed files with 34 additions and 5 deletions
File diff suppressed because one or more lines are too long
+18
View File
@@ -24,6 +24,12 @@ func (e *UsernameAlreadyExistsErr) Error() string {
return "username already exists error"
}
type UserNotFoundErr struct{}
func (e *UserNotFoundErr) Error() string {
return "user not found error"
}
type UserService struct {
db *pgxpool.Pool
}
@@ -54,6 +60,18 @@ func (s *UserService) AddUser(ctx context.Context, user *UserEntity) (*UserEntit
return user, nil
}
func (s *UserService) Login(ctx context.Context, user *UserEntity) (*UserEntity, error) {
query := `SELECT id FROM users WHERE username = @username AND password = @password`
args := pgx.NamedArgs{
"username": user.Username,
"password": hashPassword(user.Username, user.Password, "crab"),
}
if err := s.db.QueryRow(ctx, query, args).Scan(&user.Id); err != nil {
return nil, &UserNotFoundErr{}
}
return user, nil
}
func hashPassword(username string, password string, salt string) string {
return getMD5Hash(username + password + salt)
}