feat: CR login, register, subscribe
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cachingsvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
cachingdomain "github.com/ardeman/project-legalgo-go/internal/domain/caching"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type impl struct {
|
||||
redisClient *redis.Client
|
||||
}
|
||||
|
||||
type implIntf interface {
|
||||
Set(context.Context, cachingdomain.CacheSpec) error
|
||||
}
|
||||
|
||||
func New() implIntf {
|
||||
return &impl{}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cachingsvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
cachingdomain "github.com/ardeman/project-legalgo-go/internal/domain/caching"
|
||||
"github.com/ardeman/project-legalgo-go/internal/utilities/response"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (i *impl) Set(ctx context.Context, spec cachingdomain.CacheSpec) error {
|
||||
redisClient := i.redisClient
|
||||
|
||||
if redisClient == nil {
|
||||
logrus.WithContext(ctx).Errorf("redis not found")
|
||||
|
||||
return response.ErrRedisConnNotFound
|
||||
}
|
||||
|
||||
dataBytes, err := json.Marshal(spec.Data)
|
||||
if err != nil {
|
||||
return response.ErrMarshal
|
||||
}
|
||||
|
||||
if err := redisClient.Set(ctx, spec.Key, string(dataBytes), spec.TTL); err != nil {
|
||||
return response.ErrRedisSet
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -2,11 +2,15 @@ package services
|
||||
|
||||
import (
|
||||
serviceauth "github.com/ardeman/project-legalgo-go/internal/services/auth"
|
||||
subscribesvc "github.com/ardeman/project-legalgo-go/internal/services/subscribe"
|
||||
subscribeplansvc "github.com/ardeman/project-legalgo-go/internal/services/subscribe_plan"
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
var Module = fx.Module("services",
|
||||
fx.Provide(
|
||||
serviceauth.New,
|
||||
subscribeplansvc.New,
|
||||
subscribesvc.New,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package subscribesvc
|
||||
|
||||
func (s *SubsSvc) Create(subsPlanId string) (string, error) {
|
||||
subsId, err := s.subsRepo.Create(subsPlanId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return subsId, nil
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package subscribesvc
|
||||
|
||||
import subscriberepository "github.com/ardeman/project-legalgo-go/internal/accessor/subscribe"
|
||||
|
||||
type SubsSvc struct {
|
||||
subsRepo subscriberepository.SubsIntf
|
||||
}
|
||||
|
||||
type SubsIntf interface {
|
||||
Create(string) (string, error)
|
||||
}
|
||||
|
||||
func New(subsRepo subscriberepository.SubsIntf) SubsIntf {
|
||||
return &SubsSvc{subsRepo}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package subscribeplansvc
|
||||
|
||||
func (sb *SubsPlanSvc) CreatePlan(code string) error {
|
||||
return sb.subsAccs.Create(code)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package subscribeplansvc
|
||||
|
||||
import subscribeplanrepository "github.com/ardeman/project-legalgo-go/internal/accessor/subscribeplan"
|
||||
|
||||
type SubsPlanSvc struct {
|
||||
subsAccs subscribeplanrepository.SubsPlanIntf
|
||||
}
|
||||
|
||||
type SubsPlanIntf interface {
|
||||
CreatePlan(string) error
|
||||
}
|
||||
|
||||
func New(
|
||||
subsAccs subscribeplanrepository.SubsPlanIntf,
|
||||
) SubsPlanIntf {
|
||||
return &SubsPlanSvc{subsAccs}
|
||||
}
|
||||
Reference in New Issue
Block a user