add: Login system
This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"GoMembership/internal/constants"
|
||||
"GoMembership/internal/models"
|
||||
"GoMembership/internal/repositories"
|
||||
"GoMembership/internal/utils"
|
||||
"GoMembership/pkg/logger"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserServiceInterface interface {
|
||||
RegisterUser(user *models.User) (int64, string, error)
|
||||
FindUserByEmail(email string) (*models.User, error)
|
||||
GetUserByEmail(email string) (*models.User, error)
|
||||
GetUsers(where map[string]interface{}) (*[]models.User, error)
|
||||
// AuthenticateUser(email, password string) (*models.User, error)A
|
||||
VerifyUser(token *string) (*models.User, error)
|
||||
}
|
||||
|
||||
@@ -28,16 +29,12 @@ type UserService struct {
|
||||
}
|
||||
|
||||
func (service *UserService) RegisterUser(user *models.User) (int64, string, error) {
|
||||
/* salt := make([]byte, 16)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return -1, err
|
||||
}
|
||||
user.Salt = base64.StdEncoding.EncodeToString(salt)
|
||||
*/
|
||||
if err := validateRegistrationData(user); err != nil {
|
||||
return http.StatusNotAcceptable, "", err
|
||||
}
|
||||
|
||||
setPassword(user.Password, user)
|
||||
|
||||
user.Status = constants.UnverifiedStatus
|
||||
user.CreatedAt = time.Now()
|
||||
user.UpdatedAt = time.Now()
|
||||
@@ -67,8 +64,50 @@ func (service *UserService) RegisterUser(user *models.User) (int64, string, erro
|
||||
return id, token, nil
|
||||
}
|
||||
|
||||
func (service *UserService) FindUserByEmail(email string) (*models.User, error) {
|
||||
return service.Repo.FindUserByEmail(email)
|
||||
func (service *UserService) Update(user *models.User) (int64, string, error) {
|
||||
if err := validateRegistrationData(user); err != nil {
|
||||
return http.StatusNotAcceptable, "", err
|
||||
}
|
||||
|
||||
if user.Password == "" && user.RoleID != constants.Roles.Member {
|
||||
return http.StatusNotAcceptable, "", fmt.Errorf("No password provided")
|
||||
}
|
||||
hash, err := utils.HashPassword(user.Password)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, "", err
|
||||
}
|
||||
user.Password = hash
|
||||
|
||||
user.Status = constants.UnverifiedStatus
|
||||
user.CreatedAt = time.Now()
|
||||
user.UpdatedAt = time.Now()
|
||||
|
||||
id, err := service.Repo.CreateUser(user)
|
||||
|
||||
if err != nil && strings.Contains(err.Error(), "UNIQUE constraint failed") {
|
||||
return http.StatusConflict, "", err
|
||||
} else if err != nil {
|
||||
return http.StatusInternalServerError, "", err
|
||||
}
|
||||
|
||||
user.ID = id
|
||||
|
||||
token, err := utils.GenerateVerificationToken()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, "", err
|
||||
}
|
||||
|
||||
logger.Info.Printf("TOKEN: %v", token)
|
||||
|
||||
_, err = service.Repo.SetVerificationToken(user, &token)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, "", err
|
||||
}
|
||||
|
||||
return id, token, nil
|
||||
}
|
||||
func (service *UserService) GetUserByEmail(email string) (*models.User, error) {
|
||||
return service.Repo.GetUserByEmail(email)
|
||||
}
|
||||
|
||||
func (service *UserService) GetUsers(where map[string]interface{}) (*[]models.User, error) {
|
||||
@@ -90,39 +129,15 @@ func validateRegistrationData(user *models.User) error {
|
||||
validate.RegisterValidation("iban", utils.IBANValidator)
|
||||
validate.RegisterValidation("subscriptionModel", utils.SubscriptionModelValidator)
|
||||
validate.RegisterValidation("membershipField", utils.ValidateRequiredMembershipField)
|
||||
|
||||
return validate.Struct(user)
|
||||
}
|
||||
|
||||
/* func HashPassword(password string, salt string) (string, error) {
|
||||
saltedPassword := password + salt
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(saltedPassword), bcrypt.DefaultCost)
|
||||
func setPassword(plaintextPassword string, u *models.User) error {
|
||||
hash, err := argon2id.CreateHash(plaintextPassword, argon2id.DefaultParams)
|
||||
if err != nil {
|
||||
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(hashedPassword), nil
|
||||
} */
|
||||
|
||||
/* func (s *UserService) AuthenticateUser(email, password string) (*models.User, error) {
|
||||
user, err := s.repo.FindUserByEmail(email)
|
||||
if err != nil {
|
||||
return nil, errors.ErrUserNotFound
|
||||
}
|
||||
|
||||
if !verifyPassword(password, user.Password, user.Salt) {
|
||||
return nil, errors.ErrInvalidCredentials
|
||||
}
|
||||
|
||||
return user, nil
|
||||
u.Password = hash
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
/* func verifyPassword(password string, storedPassword string, salt string) bool {
|
||||
|
||||
saltedPassword := password + salt
|
||||
decodedStoredPassword, err := base64.StdEncoding.DecodeString(storedPassword)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
err = bcrypt.CompareHashAndPassword([]byte(decodedStoredPassword), []byte(saltedPassword))
|
||||
return err == nil
|
||||
} */
|
||||
|
||||
Reference in New Issue
Block a user