2024-01-19 16:56:11 +00:00
|
|
|
package adminservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-01-22 14:41:55 +00:00
|
|
|
|
2024-01-19 16:56:11 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/config"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthGenerator interface {
|
2024-01-25 11:43:39 +00:00
|
|
|
CreateAccessToken(benefactor entity.Authenticable) (string, error)
|
|
|
|
CreateRefreshToken(benefactor entity.Authenticable) (string, error)
|
2024-01-19 16:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Repository interface {
|
|
|
|
AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error)
|
|
|
|
GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error)
|
2024-02-20 20:34:51 +00:00
|
|
|
GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error)
|
2024-06-10 14:49:13 +00:00
|
|
|
GetAllAgent(ctx context.Context) ([]entity.Admin, error)
|
2024-07-02 20:17:40 +00:00
|
|
|
AgentExistByID(ctx context.Context, agentID uint) (bool, error)
|
2024-01-19 16:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
repo Repository
|
|
|
|
auth AuthGenerator
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo Repository, auth AuthGenerator) Service {
|
|
|
|
return Service{
|
|
|
|
repo: repo,
|
|
|
|
auth: auth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-01-22 14:41:55 +00:00
|
|
|
|
2024-01-19 16:56:11 +00:00
|
|
|
func CompareHash(hashedPassword, password string) error {
|
|
|
|
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
|
|
|
}
|