Skip to content

Commit

Permalink
Merge pull request #26 from bitroll-team/dev
Browse files Browse the repository at this point in the history
chore(version): v0.7.0
  • Loading branch information
Woynert authored Oct 25, 2023
2 parents 3333523 + 42e381c commit a98ae9f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
19 changes: 9 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# [0.7.0](https://github.com/bitroll-team/codefest1-users/compare/v0.6.0...v0.7.0) (2023-10-25)


### Features

* add role to login response ([ba2b9bb](https://github.com/bitroll-team/codefest1-users/commit/ba2b9bb2df4cda0664a56e10fe48552ec4f6da28))



# [0.6.0](https://github.com/bitroll-team/codefest1-users/compare/v0.5.0...v0.6.0) (2023-10-25)


Expand Down Expand Up @@ -39,13 +48,3 @@



# [0.3.0](https://github.com/bitroll-team/codefest1-users/compare/v0.2.0...v0.3.0) (2023-10-25)


### Features

* login ([6eea5bc](https://github.com/bitroll-team/codefest1-users/commit/6eea5bcb87502d9a0bbba36a8efceb948bff2635))
* register ([a052388](https://github.com/bitroll-team/codefest1-users/commit/a05238811a55d17e4af1c27646b85c0767ff853d))



19 changes: 9 additions & 10 deletions controller/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,31 @@ import (
"golang.org/x/crypto/bcrypt"
)

func (ctrl *Controller) Login(req model.ReqLogin) (string, string, error) {
func (ctrl *Controller) Login(req model.ReqLogin) (*model.ResLogin, error) {

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

query := "SELECT uuid,username,password_hash FROM users WHERE email = $1"
query := "SELECT password_hash,uuid,username,role FROM users WHERE email = $1"

row := ctrl.DB.QueryRowContext(
ctx,
query,
req.Email,
)

var userId string
var username string
var password_hash string
var res model.ResLogin
var passwordHash string

if err := row.Scan(&userId, &username, &password_hash); err != nil {
return "", "", err
if err := row.Scan(&passwordHash, &res.UserId, &res.Username, &res.Role); err != nil {
return nil, err
}

// compare

if err := bcrypt.CompareHashAndPassword([]byte(password_hash), []byte(req.Password)); err != nil {
return "", "", err
if err := bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(req.Password)); err != nil {
return nil, err
}

return userId, username, nil
return &res, nil
}
7 changes: 7 additions & 0 deletions model/controller_resp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type ResLogin struct {
UserId string `json:"userid"`
Username string `json:"username"`
Role string `json:"role"`
}
7 changes: 4 additions & 3 deletions router/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *Router) Login(ctx *gin.Context) {
return
}

userId, username, err := r.ctrl.Login(req)
res, err := r.ctrl.Login(req)
if err != nil {
log.Println(err)
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.MsgIntServerErr())
Expand All @@ -35,7 +35,7 @@ func (r *Router) Login(ctx *gin.Context) {

// Create token

token, _, err := CreateToken(TokenInfo{UserID: uuid.MustParse(userId)}, []byte(config.Cfg.Secret))
token, _, err := CreateToken(TokenInfo{UserID: uuid.MustParse(res.UserId)}, []byte(config.Cfg.Secret))
if err != nil {
log.Println(err)
return
Expand All @@ -45,7 +45,8 @@ func (r *Router) Login(ctx *gin.Context) {

ctx.JSON(http.StatusOK, gin.H{
"msg": "User logged in",
"username": username,
"username": res.Username,
"rol": res.Role,
"token": token,
})
}
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.6.0"
"version": "0.7.0"
}

0 comments on commit a98ae9f

Please sign in to comment.