48 lines
959 B
Go
48 lines
959 B
Go
package service
|
|
|
|
import (
|
|
"kontor-api-gin/internal/helpers"
|
|
"kontor-api-gin/internal/repository"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type AuthService struct {
|
|
repository *repository.ProfileRepository
|
|
}
|
|
|
|
func NewAuthService(repository *repository.ProfileRepository, secret []byte) *AuthService {
|
|
service := &AuthService{repository: repository}
|
|
return service
|
|
}
|
|
|
|
func VerifyPassword(password, hashedPassword string) error {
|
|
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
|
}
|
|
|
|
func (service *AuthService) LoginCheck(email string, password string) (string, error) {
|
|
|
|
var err error
|
|
|
|
profile, err := service.repository.FindProfileByEmail(email)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = VerifyPassword(password, profile.Password)
|
|
|
|
if err != nil && err == bcrypt.ErrMismatchedHashAndPassword {
|
|
return "", err
|
|
}
|
|
|
|
token, err := helpers.GenerateToken(profile.ID)
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|