49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
|
|
package auth
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"furtuna-be/internal/entity"
|
||
|
|
|
||
|
|
"go.uber.org/zap"
|
||
|
|
|
||
|
|
"furtuna-be/internal/common/errors"
|
||
|
|
"furtuna-be/internal/common/logger"
|
||
|
|
"furtuna-be/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
type AuthServiceImpl struct {
|
||
|
|
authRepo repository.Auth
|
||
|
|
crypto repository.Crypto
|
||
|
|
}
|
||
|
|
|
||
|
|
func New(authRepo repository.Auth, crypto repository.Crypto) *AuthServiceImpl {
|
||
|
|
return &AuthServiceImpl{
|
||
|
|
authRepo: authRepo,
|
||
|
|
crypto: crypto,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (u *AuthServiceImpl) AuthenticateUser(ctx context.Context, email, password string) (*entity.AuthenticateUser, error) {
|
||
|
|
user, err := u.authRepo.CheckExistsUserAccount(ctx, email)
|
||
|
|
if err != nil {
|
||
|
|
logger.ContextLogger(ctx).Error("error when get user", zap.Error(err))
|
||
|
|
return nil, errors.ErrorInternalServer
|
||
|
|
}
|
||
|
|
|
||
|
|
if user == nil {
|
||
|
|
return nil, errors.ErrorUserIsNotFound
|
||
|
|
}
|
||
|
|
|
||
|
|
if ok := u.crypto.CompareHashAndPassword(user.Password, password); !ok {
|
||
|
|
return nil, errors.ErrorUserInvalidLogin
|
||
|
|
}
|
||
|
|
|
||
|
|
signedToken, err := u.crypto.GenerateJWT(user.ToUser())
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return user.ToUserAuthenticate(signedToken), nil
|
||
|
|
}
|