mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
fixes on structuring
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
"wallet-api/pkg/utl/configs"
|
||||
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -16,16 +17,16 @@ type UsersService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserModel, models.ExceptionModel) {
|
||||
var checkModel models.UserModel
|
||||
var exceptionReturn models.ExceptionModel
|
||||
func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserModel, *models.ExceptionModel) {
|
||||
check := new(models.UserModel)
|
||||
exceptionReturn := new(models.ExceptionModel)
|
||||
|
||||
us.Db.Model(&checkModel).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||
if checkModel.Username != "" || checkModel.Email != "" {
|
||||
us.Db.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||
if check.Username != "" || check.Email != "" {
|
||||
exceptionReturn.Message = "User already exists"
|
||||
exceptionReturn.ErrorCode = "400101"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return &checkModel, exceptionReturn
|
||||
return check, exceptionReturn
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
||||
@@ -43,27 +44,27 @@ func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserMode
|
||||
return registerBody, exceptionReturn
|
||||
}
|
||||
|
||||
func (us *UsersService) Login(loginBody *models.LoginModel) (models.TokenModel, models.ExceptionModel) {
|
||||
var checkModel models.UserModel
|
||||
var exceptionReturn models.ExceptionModel
|
||||
var tokenPayload models.TokenModel
|
||||
func (us *UsersService) Login(loginBody *models.LoginModel) (*models.TokenModel, *models.ExceptionModel) {
|
||||
check := new(models.UserModel)
|
||||
exceptionReturn := new(models.ExceptionModel)
|
||||
tokenPayload := new(models.TokenModel)
|
||||
|
||||
us.Db.Model(&checkModel).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||
if checkModel.Email == "" {
|
||||
us.Db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||
if check.Email == "" {
|
||||
exceptionReturn.Message = "Email not found"
|
||||
exceptionReturn.ErrorCode = "400103"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
if bcrypt.CompareHashAndPassword([]byte(checkModel.Password), []byte(loginBody.Password)) != nil {
|
||||
if bcrypt.CompareHashAndPassword([]byte(check.Password), []byte(loginBody.Password)) != nil {
|
||||
exceptionReturn.Message = "Incorrect password"
|
||||
exceptionReturn.ErrorCode = "400104"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
token, err := CreateToken(checkModel)
|
||||
token, err := CreateToken(check)
|
||||
common.CheckError(err)
|
||||
|
||||
tokenPayload.Token = token
|
||||
@@ -71,15 +72,15 @@ func (us *UsersService) Login(loginBody *models.LoginModel) (models.TokenModel,
|
||||
return tokenPayload, exceptionReturn
|
||||
}
|
||||
|
||||
func CreateToken(user models.UserModel) (string, error) {
|
||||
func CreateToken(user *models.UserModel) (string, error) {
|
||||
atClaims := jwt.MapClaims{}
|
||||
atClaims["authorized"] = true
|
||||
atClaims["id"] = user.Id
|
||||
atClaims["exp"] = time.Now().Add(time.Minute * 15).Unix()
|
||||
atClaims["exp"] = time.Now().Add(time.Minute).Unix()
|
||||
|
||||
secret := os.Getenv("ACCESS_SECRET")
|
||||
if secret == "" {
|
||||
secret = "Dond3sta"
|
||||
secret = configs.Secret
|
||||
}
|
||||
|
||||
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
||||
|
||||
Reference in New Issue
Block a user