mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-04 00:06:22 +00:00
37 lines
1005 B
Go
37 lines
1005 B
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/snowykami/neo-blog/internal/dto"
|
|
"github.com/snowykami/neo-blog/internal/repo"
|
|
"github.com/snowykami/neo-blog/pkg/constant"
|
|
"github.com/snowykami/neo-blog/pkg/resps"
|
|
"github.com/snowykami/neo-blog/pkg/utils"
|
|
)
|
|
|
|
type UserService interface {
|
|
UserLogin(dto *dto.UserLoginReq) (*dto.UserLoginResp, error)
|
|
// TODO impl other user-related methods
|
|
}
|
|
|
|
type userService struct{}
|
|
|
|
func NewUserService() UserService {
|
|
return &userService{}
|
|
}
|
|
|
|
func (s *userService) UserLogin(dto *dto.UserLoginReq) (*dto.UserLoginResp, error) {
|
|
user, err := repo.User.GetByUsernameOrEmail(dto.Username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if user == nil {
|
|
return nil, errors.New(resps.ErrNotFound)
|
|
}
|
|
if utils.Password.VerifyPassword(dto.Password, user.Password, utils.Env.Get(constant.EnvVarPasswordSalt, "default_salt")) {
|
|
return nil, nil // TODO: Generate JWT token and return it in the response
|
|
} else {
|
|
return nil, errors.New(resps.ErrInvalidCredentials)
|
|
}
|
|
}
|