mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-02 00:28:35 +00:00
email improvements
This commit is contained in:
parent
6fd8457e5a
commit
29e30bfaba
47 changed files with 3710 additions and 95 deletions
|
@ -4,6 +4,7 @@ package services
|
|||
import (
|
||||
"github.com/hay-kot/homebox/backend/internal/core/currencies"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/mailer"
|
||||
)
|
||||
|
||||
type AllServices struct {
|
||||
|
@ -33,7 +34,7 @@ func WithCurrencies(v []currencies.Currency) func(*options) {
|
|||
}
|
||||
}
|
||||
|
||||
func New(repos *repo.AllRepos, opts ...OptionsFunc) *AllServices {
|
||||
func New(repos *repo.AllRepos, baseurl string, sender *mailer.Mailer, opts ...OptionsFunc) *AllServices {
|
||||
if repos == nil {
|
||||
panic("repos cannot be nil")
|
||||
}
|
||||
|
@ -55,7 +56,11 @@ func New(repos *repo.AllRepos, opts ...OptionsFunc) *AllServices {
|
|||
}
|
||||
|
||||
return &AllServices{
|
||||
User: &UserService{repos},
|
||||
User: &UserService{
|
||||
repos: repos,
|
||||
mailer: sender,
|
||||
baseurl: baseurl,
|
||||
},
|
||||
Group: &GroupService{repos},
|
||||
Items: &ItemService{
|
||||
repo: repos,
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/faker"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/mailer"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
|
@ -67,7 +68,7 @@ func TestMain(m *testing.M) {
|
|||
currencies.CollectDefaults(),
|
||||
)
|
||||
|
||||
tSvc = New(tRepos, WithCurrencies(defaults))
|
||||
tSvc = New(tRepos, "", &mailer.Mailer{}, WithCurrencies(defaults))
|
||||
defer func() { _ = client.Close() }()
|
||||
|
||||
bootstrap()
|
||||
|
|
|
@ -3,12 +3,15 @@ package services
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/easyemails"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/ent/authroles"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/hasher"
|
||||
"github.com/hay-kot/homebox/backend/pkgs/mailer"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
|
@ -20,7 +23,9 @@ var (
|
|||
)
|
||||
|
||||
type UserService struct {
|
||||
repos *repo.AllRepos
|
||||
repos *repo.AllRepos
|
||||
mailer *mailer.Mailer
|
||||
baseurl string
|
||||
}
|
||||
|
||||
type (
|
||||
|
@ -39,6 +44,9 @@ type (
|
|||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
PasswordResetRequest struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
)
|
||||
|
||||
// RegisterUser creates a new user and group in the data with the provided data. It also bootstraps the user's group
|
||||
|
@ -246,3 +254,50 @@ func (svc *UserService) ChangePassword(ctx Context, current string, new string)
|
|||
|
||||
return true
|
||||
}
|
||||
|
||||
func (svc *UserService) RequestPasswordReset(ctx context.Context, req PasswordResetRequest) error {
|
||||
usr, err := svc.repos.Users.GetOneEmail(ctx, req.Email)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Failed to get user for email reset")
|
||||
return err
|
||||
}
|
||||
|
||||
token := hasher.GenerateToken()
|
||||
err = svc.repos.Users.PasswordResetCreate(ctx, usr.ID, token.Hash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resetURL, err := url.JoinPath(svc.baseurl, "reset-password/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resetURL = resetURL + "?token=" + token.Raw
|
||||
|
||||
bldr := easyemails.NewBuilder().Add(
|
||||
easyemails.WithParagraph(
|
||||
easyemails.WithText("You have requested a password reset. Please click the link below to reset your password."),
|
||||
),
|
||||
easyemails.WithButton("Reset Password", resetURL),
|
||||
easyemails.WithParagraph(
|
||||
easyemails.WithText("[Github](https://github.com/hay-kot/homebox) · [Docs](https://hay-kot.github.io/homebox/)").
|
||||
Centered(),
|
||||
).
|
||||
FontSize(12),
|
||||
)
|
||||
|
||||
msg := mailer.NewMessageBuilder().
|
||||
SetBody(bldr.Render()).
|
||||
SetSubject("Password Reset").
|
||||
SetTo(usr.Name, usr.Email).
|
||||
Build()
|
||||
|
||||
err = svc.mailer.Send(msg)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Failed to send password reset email")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue