initial v0.8 pre-release

This commit is contained in:
Gani Georgiev
2022-10-30 10:28:14 +02:00
parent 9cbb2e750e
commit 90dba45d7c
388 changed files with 21580 additions and 13603 deletions
+23 -22
View File
@@ -7,25 +7,26 @@ import (
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/mails/templates"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
"github.com/pocketbase/pocketbase/tokens"
)
// SendUserPasswordReset sends a password reset request email to the specified user.
func SendUserPasswordReset(app core.App, user *models.User) error {
token, tokenErr := tokens.NewUserResetPasswordToken(app, user)
// SendRecordPasswordReset sends a password reset request email to the specified user.
func SendRecordPasswordReset(app core.App, authRecord *models.Record) error {
token, tokenErr := tokens.NewRecordResetPasswordToken(app, authRecord)
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
event := &core.MailerUserEvent{
event := &core.MailerRecordEvent{
MailClient: mailClient,
User: user,
Record: authRecord,
Meta: map[string]any{"token": token},
}
sendErr := app.OnMailerBeforeUserResetPasswordSend().Trigger(event, func(e *core.MailerUserEvent) error {
sendErr := app.OnMailerBeforeRecordResetPasswordSend().Trigger(event, func(e *core.MailerRecordEvent) error {
settings := app.Settings()
subject, body, err := resolveEmailTemplate(app, token, settings.Meta.ResetPasswordTemplate)
@@ -38,7 +39,7 @@ func SendUserPasswordReset(app core.App, user *models.User) error {
Name: settings.Meta.SenderName,
Address: settings.Meta.SenderAddress,
},
mail.Address{Address: e.User.Email},
mail.Address{Address: e.Record.GetString(schema.FieldNameEmail)},
subject,
body,
nil,
@@ -46,28 +47,28 @@ func SendUserPasswordReset(app core.App, user *models.User) error {
})
if sendErr == nil {
app.OnMailerAfterUserResetPasswordSend().Trigger(event)
app.OnMailerAfterRecordResetPasswordSend().Trigger(event)
}
return sendErr
}
// SendUserVerification sends a verification request email to the specified user.
func SendUserVerification(app core.App, user *models.User) error {
token, tokenErr := tokens.NewUserVerifyToken(app, user)
// SendRecordVerification sends a verification request email to the specified user.
func SendRecordVerification(app core.App, authRecord *models.Record) error {
token, tokenErr := tokens.NewRecordVerifyToken(app, authRecord)
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
event := &core.MailerUserEvent{
event := &core.MailerRecordEvent{
MailClient: mailClient,
User: user,
Record: authRecord,
Meta: map[string]any{"token": token},
}
sendErr := app.OnMailerBeforeUserVerificationSend().Trigger(event, func(e *core.MailerUserEvent) error {
sendErr := app.OnMailerBeforeRecordVerificationSend().Trigger(event, func(e *core.MailerRecordEvent) error {
settings := app.Settings()
subject, body, err := resolveEmailTemplate(app, token, settings.Meta.VerificationTemplate)
@@ -80,7 +81,7 @@ func SendUserVerification(app core.App, user *models.User) error {
Name: settings.Meta.SenderName,
Address: settings.Meta.SenderAddress,
},
mail.Address{Address: e.User.Email},
mail.Address{Address: e.Record.GetString(schema.FieldNameEmail)},
subject,
body,
nil,
@@ -88,31 +89,31 @@ func SendUserVerification(app core.App, user *models.User) error {
})
if sendErr == nil {
app.OnMailerAfterUserVerificationSend().Trigger(event)
app.OnMailerAfterRecordVerificationSend().Trigger(event)
}
return sendErr
}
// SendUserChangeEmail sends a change email confirmation email to the specified user.
func SendUserChangeEmail(app core.App, user *models.User, newEmail string) error {
token, tokenErr := tokens.NewUserChangeEmailToken(app, user, newEmail)
func SendRecordChangeEmail(app core.App, record *models.Record, newEmail string) error {
token, tokenErr := tokens.NewRecordChangeEmailToken(app, record, newEmail)
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
event := &core.MailerUserEvent{
event := &core.MailerRecordEvent{
MailClient: mailClient,
User: user,
Record: record,
Meta: map[string]any{
"token": token,
"newEmail": newEmail,
},
}
sendErr := app.OnMailerBeforeUserChangeEmailSend().Trigger(event, func(e *core.MailerUserEvent) error {
sendErr := app.OnMailerBeforeRecordChangeEmailSend().Trigger(event, func(e *core.MailerRecordEvent) error {
settings := app.Settings()
subject, body, err := resolveEmailTemplate(app, token, settings.Meta.ConfirmEmailChangeTemplate)
@@ -133,7 +134,7 @@ func SendUserChangeEmail(app core.App, user *models.User, newEmail string) error
})
if sendErr == nil {
app.OnMailerAfterUserChangeEmailSend().Trigger(event)
app.OnMailerAfterRecordChangeEmailSend().Trigger(event)
}
return sendErr
+12 -12
View File
@@ -8,16 +8,16 @@ import (
"github.com/pocketbase/pocketbase/tests"
)
func TestSendUserPasswordReset(t *testing.T) {
func TestSendRecordPasswordReset(t *testing.T) {
testApp, _ := tests.NewTestApp()
defer testApp.Cleanup()
// ensure that action url normalization will be applied
testApp.Settings().Meta.AppUrl = "http://localhost:8090////"
user, _ := testApp.Dao().FindUserByEmail("test@example.com")
user, _ := testApp.Dao().FindFirstRecordByData("users", "email", "test@example.com")
err := mails.SendUserPasswordReset(testApp, user)
err := mails.SendRecordPasswordReset(testApp, user)
if err != nil {
t.Fatal(err)
}
@@ -27,7 +27,7 @@ func TestSendUserPasswordReset(t *testing.T) {
}
expectedParts := []string{
"http://localhost:8090/_/#/users/confirm-password-reset/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
"http://localhost:8090/_/#/auth/confirm-password-reset/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
}
for _, part := range expectedParts {
if !strings.Contains(testApp.TestMailer.LastHtmlBody, part) {
@@ -36,13 +36,13 @@ func TestSendUserPasswordReset(t *testing.T) {
}
}
func TestSendUserVerification(t *testing.T) {
func TestSendRecordVerification(t *testing.T) {
testApp, _ := tests.NewTestApp()
defer testApp.Cleanup()
user, _ := testApp.Dao().FindUserByEmail("test@example.com")
user, _ := testApp.Dao().FindFirstRecordByData("users", "email", "test@example.com")
err := mails.SendUserVerification(testApp, user)
err := mails.SendRecordVerification(testApp, user)
if err != nil {
t.Fatal(err)
}
@@ -52,7 +52,7 @@ func TestSendUserVerification(t *testing.T) {
}
expectedParts := []string{
"http://localhost:8090/_/#/users/confirm-verification/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
"http://localhost:8090/_/#/auth/confirm-verification/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
}
for _, part := range expectedParts {
if !strings.Contains(testApp.TestMailer.LastHtmlBody, part) {
@@ -61,13 +61,13 @@ func TestSendUserVerification(t *testing.T) {
}
}
func TestSendUserChangeEmail(t *testing.T) {
func TestSendRecordChangeEmail(t *testing.T) {
testApp, _ := tests.NewTestApp()
defer testApp.Cleanup()
user, _ := testApp.Dao().FindUserByEmail("test@example.com")
user, _ := testApp.Dao().FindFirstRecordByData("users", "email", "test@example.com")
err := mails.SendUserChangeEmail(testApp, user, "new_test@example.com")
err := mails.SendRecordChangeEmail(testApp, user, "new_test@example.com")
if err != nil {
t.Fatal(err)
}
@@ -77,7 +77,7 @@ func TestSendUserChangeEmail(t *testing.T) {
}
expectedParts := []string{
"http://localhost:8090/_/#/users/confirm-email-change/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
"http://localhost:8090/_/#/auth/confirm-email-change/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.",
}
for _, part := range expectedParts {
if !strings.Contains(testApp.TestMailer.LastHtmlBody, part) {