feat: CR login, register, subscribe

This commit is contained in:
ericprd
2025-02-24 16:48:20 +08:00
parent ae5b2cb975
commit cb3d06fa02
48 changed files with 1027 additions and 59 deletions
+24 -10
View File
@@ -1,19 +1,33 @@
package serviceauth
package authsvc
import staffrepository "github.com/ardeman/project-legalgo-go/internal/accessor/staff"
import (
staffrepository "github.com/ardeman/project-legalgo-go/internal/accessor/staff"
subscriberepository "github.com/ardeman/project-legalgo-go/internal/accessor/subscribe"
userrepository "github.com/ardeman/project-legalgo-go/internal/accessor/user_repository"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
)
type LoginStaffSvc struct {
staffAcs staffrepository.StaffInterface
type AuthSvc struct {
staffRepo staffrepository.StaffIntf
userRepo userrepository.UserIntf
subsRepo subscriberepository.SubsIntf
}
type LoginStaffIntf interface {
LoginAsStaff(string) (string, error)
type AuthIntf interface {
LoginAsStaff(authdomain.LoginReq) (string, error)
LoginAsUser(authdomain.LoginReq) (string, error)
RegisterUser(authdomain.RegisterUserReq) (string, error)
RegisterStaff(authdomain.RegisterStaffReq) (string, error)
}
func New(
staffAcs staffrepository.StaffInterface,
) LoginStaffIntf {
return &LoginStaffSvc{
staffAcs: staffAcs,
staffRepo staffrepository.StaffIntf,
userRepo userrepository.UserIntf,
subsRepo subscriberepository.SubsIntf,
) AuthIntf {
return &AuthSvc{
staffRepo: staffRepo,
userRepo: userRepo,
subsRepo: subsRepo,
}
}
+10 -4
View File
@@ -1,18 +1,24 @@
package serviceauth
package authsvc
import (
"errors"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
)
func (sv *LoginStaffSvc) LoginAsStaff(email string) (string, error) {
staff, err := sv.staffAcs.GetStaff(email)
func (sv *AuthSvc) LoginAsStaff(spec authdomain.LoginReq) (string, error) {
staff, err := sv.staffRepo.GetStaffByEmail(spec.Email)
if err != nil {
return "", errors.New(err.Error())
}
token, err := utils.GenerateToken2(staff.Email)
matchPassword := ComparePassword(staff.Password, spec.Password)
if !matchPassword {
return "", errors.New("wrong password")
}
token, err := utils.GenerateToken(staff.Email)
if err != nil {
return "", errors.New(err.Error())
}
+27
View File
@@ -0,0 +1,27 @@
package authsvc
import (
"errors"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
)
func (sv *AuthSvc) LoginAsUser(spec authdomain.LoginReq) (string, error) {
user, err := sv.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")
}
token, err := utils.GenerateToken(user.Email)
if err != nil {
return "", errors.New(err.Error())
}
return token, nil
}
+33
View File
@@ -0,0 +1,33 @@
package authsvc
import (
"errors"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
"github.com/google/uuid"
)
func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error) {
hashedPwd, err := HashPassword(spec.Password)
if err != nil {
return "", err
}
user := authdomain.Staff{
ID: uuid.New(),
Email: spec.Email,
Password: hashedPwd,
}
_, err = a.staffRepo.Create(&user)
if err != nil {
return "", errors.New(err.Error())
}
token, err := utils.GenerateToken(spec.Email)
if err != nil {
return "", errors.New(err.Error())
}
return token, nil
}
+40
View File
@@ -0,0 +1,40 @@
package authsvc
import (
"errors"
authdomain "github.com/ardeman/project-legalgo-go/internal/domain/auth"
"github.com/ardeman/project-legalgo-go/internal/utilities/utils"
"github.com/google/uuid"
)
func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error) {
subsId, err := a.subsRepo.Create(spec.SubscribePlanID)
if err != nil {
return "", nil
}
hashedPwd, err := HashPassword(spec.Password)
if err != nil {
return "", err
}
user := authdomain.User{
ID: uuid.New(),
Email: spec.Email,
SubscribeID: subsId,
Password: hashedPwd,
Phone: spec.Phone,
}
_, err = a.userRepo.CreateUser(&user)
if err != nil {
return "", errors.New(err.Error())
}
token, err := utils.GenerateToken(spec.Email)
if err != nil {
return "", errors.New(err.Error())
}
return token, nil
}
+32
View File
@@ -0,0 +1,32 @@
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
}