fix: update user plan and rearrange structur project
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
package authsvc
|
||||
|
||||
import (
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
)
|
||||
|
||||
func (as *impl) GetUserProfile(email string) (*userdomain.UserProfile, error) {
|
||||
user, err := as.userRepo.GetUserProfile(email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package authsvc
|
||||
import (
|
||||
staffrepository "legalgo-BE-go/internal/accessor/staff"
|
||||
subscriberepository "legalgo-BE-go/internal/accessor/subscribe"
|
||||
subscribeplanrepository "legalgo-BE-go/internal/accessor/subscribeplan"
|
||||
subscribeplanrepository "legalgo-BE-go/internal/accessor/subscribe_plan"
|
||||
userrepository "legalgo-BE-go/internal/accessor/user"
|
||||
staffdomain "legalgo-BE-go/internal/domain/staff"
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
type impl struct {
|
||||
staffRepo staffrepository.Staff
|
||||
userRepo userrepository.User
|
||||
subsRepo subscriberepository.SubsIntf
|
||||
subsPlanRepo subscribeplanrepository.SubsPlanIntf
|
||||
subsRepo subscriberepository.Subscribe
|
||||
subsPlanRepo subscribeplanrepository.SubscribePlan
|
||||
}
|
||||
|
||||
type Auth interface {
|
||||
@@ -22,17 +22,13 @@ type Auth interface {
|
||||
GetStaffProfile(string) (*staffdomain.StaffProfile, error)
|
||||
GetUsers() ([]userdomain.UserProfile, error)
|
||||
UpdateStaff(staffdomain.Staff) error
|
||||
|
||||
LoginAsUser(userdomain.UserLogin) (string, error)
|
||||
RegisterUser(userdomain.UserRegister) (string, error)
|
||||
GetUserProfile(string) (*userdomain.UserProfile, error)
|
||||
}
|
||||
|
||||
func New(
|
||||
staffRepo staffrepository.Staff,
|
||||
userRepo userrepository.User,
|
||||
subsRepo subscriberepository.SubsIntf,
|
||||
subsPlanRepo subscribeplanrepository.SubsPlanIntf,
|
||||
subsRepo subscriberepository.Subscribe,
|
||||
subsPlanRepo subscribeplanrepository.SubscribePlan,
|
||||
) Auth {
|
||||
return &impl{
|
||||
staffRepo: staffRepo,
|
||||
|
||||
@@ -16,7 +16,7 @@ func (sv *impl) LoginAsStaff(spec staffdomain.StaffLogin) (string, error) {
|
||||
return "", errors.New(err.Error())
|
||||
}
|
||||
|
||||
matchPassword := ComparePassword(staff.Password, spec.Password)
|
||||
matchPassword := utils.ComparePassword(staff.Password, spec.Password)
|
||||
if !matchPassword {
|
||||
return "", errors.New("wrong password")
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package authsvc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (a *impl) LoginAsUser(spec userdomain.UserLogin) (string, error) {
|
||||
user, err := a.userRepo.GetUserByEmail(spec.Email)
|
||||
if err != nil {
|
||||
return "", errors.New(err.Error())
|
||||
}
|
||||
|
||||
matchPassword := ComparePassword(user.Password, spec.Password)
|
||||
if !matchPassword {
|
||||
return "", errors.New("wrong password")
|
||||
}
|
||||
|
||||
authToken := authdomain.AuthToken{
|
||||
Email: user.Email,
|
||||
SessionID: uuid.NewString(),
|
||||
Role: "user",
|
||||
}
|
||||
|
||||
token, err := utils.GenerateToken(authToken)
|
||||
if err != nil {
|
||||
return "", errors.New(err.Error())
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
@@ -15,7 +15,7 @@ func (a *impl) RegisterStaff(spec staffdomain.StaffRegister) (string, error) {
|
||||
if err == nil {
|
||||
return "", errors.New("this email address is already in use")
|
||||
}
|
||||
hashedPwd, err := HashPassword(spec.Password)
|
||||
hashedPwd, err := utils.HashPassword(spec.Password)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
package authsvc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
authdomain "legalgo-BE-go/internal/domain/auth"
|
||||
userdomain "legalgo-BE-go/internal/domain/user"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (a *impl) RegisterUser(spec userdomain.UserRegister) (string, error) {
|
||||
_, err := a.userRepo.GetUserByEmail(spec.Email)
|
||||
|
||||
if err == nil {
|
||||
return "", errors.New("this email address is already in use")
|
||||
}
|
||||
|
||||
if spec.SubscribePlanID == "" {
|
||||
subsPlan, err := a.subsPlanRepo.GetDefault()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
spec.SubscribePlanID = subsPlan.ID
|
||||
}
|
||||
|
||||
_, err = a.subsPlanRepo.GetByID(spec.SubscribePlanID)
|
||||
if err != nil {
|
||||
return "", errors.New(err.Error())
|
||||
}
|
||||
|
||||
subsId, err := a.subsRepo.Create(spec.SubscribePlanID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
hashedPwd, err := HashPassword(spec.Password)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
user := userdomain.User{
|
||||
ID: uuid.NewString(),
|
||||
Email: spec.Email,
|
||||
SubscribeID: subsId,
|
||||
Password: hashedPwd,
|
||||
Phone: spec.Phone,
|
||||
}
|
||||
|
||||
err = a.userRepo.CreateUser(user)
|
||||
if err != nil {
|
||||
return "", errors.New(err.Error())
|
||||
}
|
||||
|
||||
authToken := authdomain.AuthToken{
|
||||
Email: user.Email,
|
||||
SessionID: uuid.NewString(),
|
||||
Role: "user",
|
||||
}
|
||||
|
||||
token, err := utils.GenerateToken(authToken)
|
||||
if err != nil {
|
||||
return "", errors.New(err.Error())
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package authsvc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func VerifyToken(signature string) jwt.Keyfunc {
|
||||
return func(token *jwt.Token) (any, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
|
||||
return []byte(signature), nil
|
||||
}
|
||||
}
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
// Hashing the password with a cost of 14 (default)
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(hash), nil
|
||||
}
|
||||
|
||||
func ComparePassword(storedPassword, inputPassword string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(storedPassword), []byte(inputPassword))
|
||||
return err == nil
|
||||
}
|
||||
Reference in New Issue
Block a user