add tysc handlers to kontor-gin
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
package service
|
||||
|
||||
import "kontor-api-gin/internal/repository"
|
||||
import (
|
||||
"kontor-api-gin/internal/helpers"
|
||||
"kontor-api-gin/internal/repository"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type AuthService struct {
|
||||
repository *repository.ProfileRepository
|
||||
@@ -10,3 +15,33 @@ func NewAuthService(repository *repository.ProfileRepository, secret []byte) *Au
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user