forked from ebhomengo/niki
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package adminservice
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/config"
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
validator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type AuthGenerator interface {
|
|
CreateAccessToken(admin entity.Authenticable) (string, error)
|
|
CreateRefreshToken(admin entity.Authenticable) (string, error)
|
|
}
|
|
|
|
type Repository interface {
|
|
AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error)
|
|
GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error)
|
|
GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error)
|
|
}
|
|
|
|
type Service struct {
|
|
repo Repository
|
|
auth AuthGenerator
|
|
vld validator.Validator
|
|
}
|
|
|
|
func New(repo Repository, auth AuthGenerator, vld validator.Validator) Service {
|
|
return Service{
|
|
repo: repo,
|
|
auth: auth,
|
|
vld: vld,
|
|
}
|
|
}
|
|
|
|
func GenerateHash(password *string) error {
|
|
hashedPassword, bErr := bcrypt.GenerateFromPassword([]byte(*password), config.BcryptCost)
|
|
if bErr != nil {
|
|
return fmt.Errorf("bcrypt error: %w", bErr)
|
|
}
|
|
*password = string(hashedPassword)
|
|
|
|
return nil
|
|
}
|
|
|
|
func CompareHash(hashedPassword, password string) error {
|
|
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
|
}
|