fix email change issues (#973)

* fix email change

* isolated email update

* update email UI immediately

---------

Co-authored-by: Christian Beutel <>
This commit is contained in:
slothful-vassal
2026-05-10 08:38:55 +02:00
committed by GitHub
parent d2ac49470a
commit edad99c7b4
8 changed files with 140 additions and 29 deletions

View File

@@ -0,0 +1,62 @@
package routes
import (
"errors"
"net/http"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/mails"
"github.com/pocketbase/pocketbase/tools/routine"
)
func UserEmailChange(e *core.RequestEvent) error {
if e.Auth == nil {
return apis.NewUnauthorizedError("Authentication required", nil)
}
var data struct {
Email string `json:"email"`
Password string `json:"password"`
}
if err := e.BindBody(&data); err != nil {
return apis.NewBadRequestError("Failed to read request data", err)
}
if data.Email == "" {
return apis.NewBadRequestError("Email is required", nil)
}
if data.Password == "" {
return apis.NewBadRequestError("Current password is required", nil)
}
if !e.Auth.ValidatePassword(data.Password) {
return apis.NewBadRequestError("Invalid password", nil)
}
e.Auth.Set("email", data.Email)
e.Auth.Set("verified", false)
if err := e.App.Save(e.Auth); err != nil {
var verr validation.Errors
if errors.As(err, &verr) {
return apis.NewBadRequestError("Validation failed", verr)
}
return err
}
app := e.App
routine.FireAndForget(func() {
if err := mails.SendRecordVerification(app, e.Auth); err != nil {
app.Logger().Error("Failed to send verification email", "error", err)
}
})
token, err := e.Auth.NewAuthToken()
if err != nil {
return err
}
return e.JSON(http.StatusOK, map[string]any{
"token": token,
"record": e.Auth,
})
}