working on auth.
mailer, basic setup with html template and a dev treansport
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Info(msg string, args ...any) {
|
||||
a, b := getArgs(args)
|
||||
slog.Info(fmt.Sprintf(msg, a...), b...)
|
||||
}
|
||||
|
||||
func Warn(msg string, args ...any) {
|
||||
a, b := getArgs(args)
|
||||
slog.Warn(fmt.Sprintf(msg, a...), b...)
|
||||
}
|
||||
|
||||
func Error(err error, args ...any) {
|
||||
a, b := getArgs(args)
|
||||
slog.Error(fmt.Sprintf(err.Error(), a...), b...)
|
||||
// TODO: save error log for later scrutiny
|
||||
}
|
||||
|
||||
// Fatal error will exit with os.Exit(1)
|
||||
func Fatal(msg string, args ...any) {
|
||||
a, b := getArgs(args)
|
||||
slog.Error(fmt.Sprintf(msg, a...), b...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func getArgs(args []any) ([]any, []any) {
|
||||
var a []any
|
||||
var b []any
|
||||
for _, arg := range args {
|
||||
switch arg.(type) {
|
||||
case slog.Attr:
|
||||
b = append(b, arg)
|
||||
default:
|
||||
a = append(a, arg)
|
||||
}
|
||||
}
|
||||
return a, b
|
||||
}
|
97
pkg/user/create.go
Normal file
97
pkg/user/create.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/mail"
|
||||
"strings"
|
||||
|
||||
"gitserver.in/patialtech/rano/db"
|
||||
"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"
|
||||
)
|
||||
|
||||
type CreateInput struct {
|
||||
Email string `validate:"email"`
|
||||
Phone string
|
||||
Pwd string `validate:"required"`
|
||||
ConfirmPwd string `validate:"required"`
|
||||
FirstName string `validate:"required"`
|
||||
MiddleName string
|
||||
LastName string
|
||||
RoleID uint8 `validate:"required"`
|
||||
}
|
||||
|
||||
var (
|
||||
ErrCreateInpNil = errors.New("user: create input is nil")
|
||||
ErrWrongConfirmPwd = errors.New("user: confirm password does not match")
|
||||
)
|
||||
|
||||
// Create user record in DB
|
||||
//
|
||||
// will return created userID on success
|
||||
func Create(ctx context.Context, inp *CreateInput) (int64, error) {
|
||||
// check for nil inp
|
||||
if inp == nil {
|
||||
return 0, ErrCreateInpNil
|
||||
}
|
||||
|
||||
// validate
|
||||
if err := validate.Struct(inp); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// compare pwd and comparePwd
|
||||
if inp.Pwd != inp.ConfirmPwd {
|
||||
return 0, ErrWrongConfirmPwd
|
||||
}
|
||||
|
||||
h, salt, err := crypto.PasswordHash(inp.Pwd)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// save record to DB
|
||||
client := db.Client()
|
||||
u, err := client.User.Create().
|
||||
SetEmail(inp.Email).
|
||||
SetPwdHash(h).
|
||||
SetPwdSalt(salt).
|
||||
SetFirstName(inp.FirstName).
|
||||
SetMiddleName(inp.MiddleName).
|
||||
SetLastName(inp.LastName).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
logger.Error(err, slog.String("ref", "user: create error"))
|
||||
return 0, errors.New("failed to create user")
|
||||
}
|
||||
|
||||
// email
|
||||
err = mailer.Send(
|
||||
[]mail.Address{
|
||||
{Name: inp.FullName(), Address: inp.Email},
|
||||
},
|
||||
&message.Welcome{
|
||||
Name: inp.FullName(),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
logger.Error(err, slog.String("ref", "user: send welcome email"))
|
||||
}
|
||||
|
||||
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)
|
||||
return strings.Join(strings.Fields(name), " ")
|
||||
}
|
36
pkg/user/create_test.go
Normal file
36
pkg/user/create_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
t.Run("check nil", func(t *testing.T) {
|
||||
if _, err := Create(context.Background(), nil); err == nil {
|
||||
t.Error("nil check error expected")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("trigger validation errors", func(t *testing.T) {
|
||||
if _, err := Create(context.Background(), &CreateInput{}); err == nil {
|
||||
t.Error("validation errors are expected")
|
||||
} else {
|
||||
t.Log(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("create", func(t *testing.T) {
|
||||
if _, err := Create(context.Background(), &CreateInput{
|
||||
Email: "aa@aa.com",
|
||||
Pwd: "pwd123",
|
||||
ConfirmPwd: "pwd123",
|
||||
FirstName: "Ankit",
|
||||
MiddleName: "Singh",
|
||||
LastName: "Patial",
|
||||
RoleID: 1,
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user