feat: hash the user password

This commit is contained in:
Muyao CHEN
2024-10-06 22:07:51 +02:00
parent ba8570857d
commit b7697bc89b
2 changed files with 9 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import (
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/repo"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
"golang.org/x/crypto/bcrypt"
)
var UserExisted = &errno.Errno{
@ -57,6 +58,13 @@ func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
}
func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User, error) {
// Hash the password
encrypted, err := bcrypt.GenerateFromPassword([]byte(u.Password), 12)
if err != nil {
log.ErrorLog("encrypt password error", "err", err)
return nil, errno.InternalServerErr
}
u.Password = string(encrypted)
data, err := uuc.dbRepo.Transaction(
ctx,
func(txCtx context.Context, tx interface{}) (interface{}, error) {