Skip to content

Commit

Permalink
Merge pull request #17 from bitroll-team/feat/return-username
Browse files Browse the repository at this point in the history
feat: Return username
  • Loading branch information
Woynert authored Oct 25, 2023
2 parents 5274d24 + a2457ea commit b25d5d0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
13 changes: 7 additions & 6 deletions controller/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"golang.org/x/crypto/bcrypt"
)

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

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

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

row := ctrl.DB.QueryRowContext(
ctx,
Expand All @@ -22,17 +22,18 @@ func (ctrl *Controller) Login(req model.ReqLogin) (string, error) {
)

var userId string
var username string
var password_hash string

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

// compare

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

return userId, nil
return userId, username, nil
}
5 changes: 3 additions & 2 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ func (ctrl *Controller) Register(req model.ReqRegister) error {
defer cancel()

query := `
INSERT INTO users (username, email, full_name, password_hash)
VALUES ($1, $2, $3, $4)
INSERT INTO users (role, username, email, full_name, password_hash)
VALUES ($1, $2, $3, $4, $5)
`

_, err = ctrl.DB.ExecContext(
ctx,
query,
"STUDENT",
req.Username,
req.Email,
req.Fullname,
Expand Down
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, err := r.ctrl.Login(req)
userId, username, err := r.ctrl.Login(req)
if err != nil {
log.Println(err)
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.MsgIntServerErr())
Expand All @@ -44,7 +44,8 @@ func (r *Router) Login(ctx *gin.Context) {
// send

ctx.JSON(http.StatusOK, gin.H{
"msg": "User logged in",
"token": token,
"msg": "User logged in",
"username": username,
"token": token,
})
}

0 comments on commit b25d5d0

Please sign in to comment.