e87b7df979
Update module name and all imports from habit-tracker-api to apocapoc-api. This reflects the project's new branding as part of the apocapoc ecosystem (apocapoc-api, apocapoc-web, apocapoc-android).
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package queries
|
|
|
|
import (
|
|
"context"
|
|
|
|
"apocapoc-api/internal/domain/repositories"
|
|
"apocapoc-api/internal/shared/errors"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type LoginUserQuery struct {
|
|
Email string
|
|
Password string
|
|
}
|
|
|
|
type LoginUserResult struct {
|
|
UserID string
|
|
Email string
|
|
Timezone string
|
|
}
|
|
|
|
type LoginUserHandler struct {
|
|
userRepo repositories.UserRepository
|
|
}
|
|
|
|
func NewLoginUserHandler(userRepo repositories.UserRepository) *LoginUserHandler {
|
|
return &LoginUserHandler{userRepo: userRepo}
|
|
}
|
|
|
|
func (h *LoginUserHandler) Handle(ctx context.Context, query LoginUserQuery) (*LoginUserResult, error) {
|
|
if query.Email == "" || query.Password == "" {
|
|
return nil, errors.ErrInvalidInput
|
|
}
|
|
|
|
user, err := h.userRepo.FindByEmail(ctx, query.Email)
|
|
if err != nil {
|
|
return nil, errors.ErrNotFound
|
|
}
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(query.Password)); err != nil {
|
|
return nil, errors.ErrNotFound
|
|
}
|
|
|
|
return &LoginUserResult{
|
|
UserID: user.ID,
|
|
Email: user.Email,
|
|
Timezone: user.Timezone,
|
|
}, nil
|
|
}
|