feature: verify tokens
This commit is contained in:
@@ -4,15 +4,16 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/mail"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitserver.in/patialtech/rano/config"
|
||||
"gitserver.in/patialtech/rano/db"
|
||||
"gitserver.in/patialtech/rano/db/ent/user"
|
||||
"gitserver.in/patialtech/rano/mailer"
|
||||
"gitserver.in/patialtech/rano/mailer/message"
|
||||
"gitserver.in/patialtech/rano/util/crypto"
|
||||
"gitserver.in/patialtech/rano/util/logger"
|
||||
"gitserver.in/patialtech/rano/util/validate"
|
||||
)
|
||||
|
||||
@@ -36,17 +37,17 @@ var (
|
||||
//
|
||||
// will return created userID on success
|
||||
func Create(ctx context.Context, inp *CreateInput) (int64, error) {
|
||||
// check for nil inp
|
||||
// Check for nil input.
|
||||
if inp == nil {
|
||||
return 0, ErrCreateInpNil
|
||||
}
|
||||
|
||||
// validate
|
||||
// Validate struct.
|
||||
if err := validate.Struct(inp); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// compare pwd and comparePwd
|
||||
// Compare pwd and comparePwd.
|
||||
if inp.Pwd != inp.ConfirmPwd {
|
||||
return 0, ErrWrongConfirmPwd
|
||||
}
|
||||
@@ -56,42 +57,73 @@ func Create(ctx context.Context, inp *CreateInput) (int64, error) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// save record to DB
|
||||
client := db.Client()
|
||||
u, err := client.User.Create().
|
||||
// Begin a transaction.
|
||||
tx, err := db.Client().BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Save User to DB.
|
||||
u, err := tx.User.Create().
|
||||
SetEmail(inp.Email).
|
||||
SetPwdHash(h).
|
||||
SetPwdSalt(salt).
|
||||
SetFirstName(inp.FirstName).
|
||||
SetMiddleName(inp.MiddleName).
|
||||
SetLastName(inp.LastName).
|
||||
SetStatus(user.StatusActive).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
logger.Error(err, slog.String("ref", "user: create error"))
|
||||
return 0, errors.New("failed to create user")
|
||||
}
|
||||
|
||||
// email
|
||||
// Get a new email-verification token
|
||||
tokenDuration := time.Hour * 6
|
||||
token, err := newTokenToVerifyEmail(u.ID, tokenDuration)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Save token to DB
|
||||
err = tx.VerifyToken.Create().
|
||||
SetToken(token).
|
||||
SetExpiresAt(time.Now().Add(tokenDuration).UTC()).
|
||||
SetPurpose("VerifyEmail").
|
||||
SetUserID(u.ID).Exec(ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return 0, err
|
||||
}
|
||||
|
||||
name := fullName(inp.FirstName, inp.MiddleName, inp.LastName)
|
||||
// Send a welcome email with a link to verigy email-address.
|
||||
err = mailer.Send(
|
||||
[]mail.Address{
|
||||
{Name: inp.FullName(), Address: inp.Email},
|
||||
{Name: name, Address: inp.Email},
|
||||
},
|
||||
&message.Welcome{
|
||||
Name: inp.FullName(),
|
||||
Name: name,
|
||||
VerifyURL: config.VerifyEmailURL(token),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
logger.Error(err, slog.String("ref", "user: send welcome email"))
|
||||
_ = tx.Rollback()
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Commit transaction
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// ALL Done!
|
||||
// Created a new user in system.
|
||||
return u.ID, nil
|
||||
}
|
||||
|
||||
func (inp *CreateInput) FullName() string {
|
||||
if inp == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
name := fmt.Sprintf("%s %s %s", inp.FirstName, inp.MiddleName, inp.LastName)
|
||||
func fullName(fName, mName, lName string) string {
|
||||
name := fmt.Sprintf("%s %s %s", fName, mName, lName)
|
||||
return strings.Join(strings.Fields(name), " ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user