mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
fix user login and register
This commit is contained in:
@@ -51,6 +51,14 @@ func (rc *UserController) PostLogin(c *gin.Context) {
|
|||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if body.Email == "" {
|
||||||
|
c.JSON(400, "Email cannot be empty!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if body.Password == "" {
|
||||||
|
c.JSON(400, "Password cannot be empty!")
|
||||||
|
return
|
||||||
|
}
|
||||||
returnedUser, exceptionReturn := rc.service.Login(c, body)
|
returnedUser, exceptionReturn := rc.service.Login(c, body)
|
||||||
|
|
||||||
if exceptionReturn.Message != "" {
|
if exceptionReturn.Message != "" {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import "wallet-api/pkg/model"
|
|||||||
type UserFilter struct {
|
type UserFilter struct {
|
||||||
model.Params
|
model.Params
|
||||||
BaseFilter
|
BaseFilter
|
||||||
|
Email string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserFilter(params model.Params) *UserFilter {
|
func NewUserFilter(params model.Params) *UserFilter {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ Gets row from transaction table by id.
|
|||||||
func (us *UserRepository) Get(ctx context.Context, flt *filter.UserFilter, tx *pg.Tx) (*model.User, error) {
|
func (us *UserRepository) Get(ctx context.Context, flt *filter.UserFilter, tx *pg.Tx) (*model.User, error) {
|
||||||
wm := new(model.User)
|
wm := new(model.User)
|
||||||
wm.Id = flt.Id
|
wm.Id = flt.Id
|
||||||
|
wm.Email = flt.Email
|
||||||
|
|
||||||
commit := false
|
commit := false
|
||||||
if tx == nil {
|
if tx == nil {
|
||||||
@@ -52,7 +53,16 @@ func (us *UserRepository) Get(ctx context.Context, flt *filter.UserFilter, tx *p
|
|||||||
}
|
}
|
||||||
|
|
||||||
qry := tx.Model(wm)
|
qry := tx.Model(wm)
|
||||||
err := common.GenerateEmbed(qry, flt.Embed).WherePK().Select()
|
|
||||||
|
var query = common.GenerateEmbed(qry, flt.Embed)
|
||||||
|
if wm.Id != "" {
|
||||||
|
query.WherePK()
|
||||||
|
}
|
||||||
|
if wm.Email != "" {
|
||||||
|
query.Where("? = ?", pg.Ident("email"), wm.Email)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := query.Select()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -116,7 +126,7 @@ Inserts new row to users table.
|
|||||||
*model.User: User object from database
|
*model.User: User object from database
|
||||||
*model.Exception
|
*model.Exception
|
||||||
*/
|
*/
|
||||||
func (us *UserRepository) Check(ctx context.Context, tx *pg.Tx, checkBody *model.User) (*model.User, error) {
|
func (us *UserRepository) Check(ctx context.Context, tx *pg.Tx, checkBody *model.User) (bool, error) {
|
||||||
check := new(model.User)
|
check := new(model.User)
|
||||||
|
|
||||||
commit := false
|
commit := false
|
||||||
@@ -127,14 +137,14 @@ func (us *UserRepository) Check(ctx context.Context, tx *pg.Tx, checkBody *model
|
|||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
}
|
}
|
||||||
|
|
||||||
err := tx.Model(check).Where("? = ?", pg.Ident("username"), checkBody.Username).WhereOr("? = ?", pg.Ident("email"), checkBody.Email).Select()
|
exists, err := tx.Model(check).Where("? = ?", pg.Ident("username"), checkBody.Username).WhereOr("? = ?", pg.Ident("email"), checkBody.Email).Exists()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return false, err
|
||||||
}
|
}
|
||||||
if commit {
|
if commit {
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
}
|
}
|
||||||
return check, nil
|
return exists, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func (us *UserService) Create(ctx context.Context, registerBody *model.User) (*m
|
|||||||
return nil, exceptionReturn
|
return nil, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
if check.Username != "" || check.Email != "" {
|
if check {
|
||||||
exceptionReturn.Message = "User already exists"
|
exceptionReturn.Message = "User already exists"
|
||||||
exceptionReturn.ErrorCode = "400101"
|
exceptionReturn.ErrorCode = "400101"
|
||||||
exceptionReturn.StatusCode = 400
|
exceptionReturn.StatusCode = 400
|
||||||
@@ -94,10 +94,13 @@ func (us *UserService) Login(ctx context.Context, body *model.Login) (*model.Tok
|
|||||||
loginBody.Email = body.Email
|
loginBody.Email = body.Email
|
||||||
loginBody.Username = body.Email
|
loginBody.Username = body.Email
|
||||||
|
|
||||||
|
flt := filter.NewUserFilter(model.Params{})
|
||||||
|
flt.Email = body.Email
|
||||||
|
|
||||||
tx, _ := us.repository.CreateTx(ctx)
|
tx, _ := us.repository.CreateTx(ctx)
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
check, err := us.repository.Check(ctx, tx, loginBody)
|
check, err := us.repository.Get(ctx, flt, tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exceptionReturn.Message = "Error checking user"
|
exceptionReturn.Message = "Error checking user"
|
||||||
exceptionReturn.ErrorCode = "400139"
|
exceptionReturn.ErrorCode = "400139"
|
||||||
|
|||||||
Reference in New Issue
Block a user