-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6832fc3
commit 5c0c229
Showing
5 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
db "github.com/cukhoaimon/SimpleBank/db/sqlc" | ||
"github.com/cukhoaimon/SimpleBank/utils" | ||
"github.com/gin-gonic/gin" | ||
"github.com/lib/pq" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type createUserRequest struct { | ||
Username string `json:"username" binding:"required,alphanum"` | ||
Password string `json:"password" binding:"required,min=8"` | ||
FullName string `json:"full_name" binding:"required"` | ||
Email string `json:"email" binding:"required,email"` | ||
} | ||
|
||
type createUserResponse struct { | ||
Username string `json:"username"` | ||
FullName string `json:"full_name"` | ||
Email string `json:"email"` | ||
PasswordChangedAt time.Time `json:"password_changed_at"` | ||
CreatedAt time.Time `json:"created_at"` | ||
} | ||
|
||
func (server *Server) createUser(ctx *gin.Context) { | ||
var req createUserRequest | ||
|
||
if err := ctx.ShouldBindJSON(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
hashedPassword, err := utils.HashPassword(req.Password) | ||
|
||
arg := db.CreateUserParams{ | ||
Username: req.Username, | ||
HashedPassword: hashedPassword, | ||
FullName: req.FullName, | ||
Email: req.Email, | ||
} | ||
|
||
user, err := server.store.CreateUser(ctx, arg) | ||
if err != nil { | ||
var pqErr *pq.Error | ||
if errors.As(err, &pqErr) { | ||
switch pqErr.Code.Name() { | ||
case "unique_violation": | ||
ctx.JSON(http.StatusForbidden, errorResponse(err)) | ||
return | ||
} | ||
} | ||
|
||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
rsp := createUserResponse{ | ||
Username: user.Username, | ||
FullName: user.FullName, | ||
Email: user.Email, | ||
PasswordChangedAt: user.PasswordChangedAt, | ||
CreatedAt: user.CreatedAt, | ||
} | ||
|
||
ctx.JSON(http.StatusCreated, rsp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
// HashPassword returns the password hashed by bcrypt | ||
func HashPassword(password string) (string, error) { | ||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return "", fmt.Errorf("fail to hash password: %w", err) | ||
} | ||
|
||
return string(hashedPassword), nil | ||
} | ||
|
||
// CheckPassword check if the password is match or not | ||
func CheckPassword(password, hashedPassword string) error { | ||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestPassword(t *testing.T) { | ||
password := RandomString(10) | ||
hashedPassword, err := HashPassword(password) | ||
|
||
require.Nil(t, err) | ||
|
||
type args struct { | ||
password string | ||
hashedPassword string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Same password", | ||
args: args{ | ||
password: password, | ||
hashedPassword: hashedPassword, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Different password", | ||
args: args{ | ||
password: RandomString(10), | ||
hashedPassword: hashedPassword, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := CheckPassword(tt.args.password, tt.args.hashedPassword); (err != nil) != tt.wantErr { | ||
t.Errorf("CheckPassword() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |