From 2f1f559383eccce08f082be5b7208f112ba04f5d Mon Sep 17 00:00:00 2001 From: venbaittech Date: Thu, 26 Dec 2024 14:56:16 +0530 Subject: [PATCH 1/2] password mail user --- main.go | 111 ++++++++++++++++++++++++++++++++++++++++++++ pb_data/data.db-shm | Bin 32768 -> 0 bytes pb_data/data.db-wal | 0 pb_data/logs.db-shm | Bin 32768 -> 0 bytes pb_data/logs.db-wal | 0 5 files changed, 111 insertions(+) delete mode 100644 pb_data/data.db-shm delete mode 100644 pb_data/data.db-wal delete mode 100644 pb_data/logs.db-shm delete mode 100644 pb_data/logs.db-wal diff --git a/main.go b/main.go index 4912afc..1cee8c0 100644 --- a/main.go +++ b/main.go @@ -111,6 +111,117 @@ func main() { // app := pocketbase.New() app = pocketbase.New() + app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error { + if e.Collection.Name == "users" { + // Fetch the original record by ID + originalRecord, err := app.Dao().FindRecordById("users", e.Record.GetString("id")) + if err != nil { + return fmt.Errorf("failed to fetch original record: %v", err) + } + + // Compare the original `passwordHash` with the updated `passwordHash` + originalPasswordHash := originalRecord.GetString("passwordHash") + updatedPasswordHash := e.Record.GetString("passwordHash") + + if originalPasswordHash != updatedPasswordHash { + fmt.Println("Password changed for user:", e.Record.GetString("username")) + + // Fetch user details for the email + name := e.Record.GetString("username") + email := e.Record.GetString("email") + + if name == "" || email == "" { + return fmt.Errorf("user details missing: name or email") + } + + // Send an email notification + subject := "Password Change Notification" + body := fmt.Sprintf(` +

Hello %s,

+

Your password has been successfully changed. If this was not you, please contact support immediately.

+

Best regards,
Your Team

+ `, name) + + // Create the email message + message := &mailer.Message{ + From: mail.Address{ + Name: "FCSC", // Sender name + Address: app.Settings().Meta.SenderAddress, // Sender email address from app settings + }, + To: []mail.Address{ + { + Name: name, + Address: email, + }, + }, + Subject: subject, + HTML: body, // Use HTML body for formatted content + } + + // Send the email + err := app.NewMailClient().Send(message) + if err != nil { + log.Printf("Failed to send password change email to user: %v", err) + return err + } + } + } + + return nil + }) + + // Handle password resets explicitly + + app.OnRecordAfterAuthWithPasswordRequest().Add(func(e *core.RecordAuthWithPasswordEvent) error { + if e.Collection.Name == "users" { + fmt.Println("Password reset detected for user:", e.Record.GetString("username")) + + // Optionally, log the password reset time or other actions + resetTime := time.Now().Format(time.RFC3339) + fmt.Println("Password reset time:", resetTime) + + // Fetch user details for the email + name := e.Record.GetString("username") + email := e.Record.GetString("email") + + if name == "" || email == "" { + return fmt.Errorf("user details missing: name or email") + } + + // Send an email notification + subject := "Password Reset Notification" + body := fmt.Sprintf(` +

Hello %s,

+

Your password has been successfully reset on %s. If this was not you, please contact support immediately.

+

Best regards,
Your Team

+ `, name, resetTime) + + // Create the email message + message := &mailer.Message{ + From: mail.Address{ + Name: "FCSC", // Sender name + Address: app.Settings().Meta.SenderAddress, // Sender email address from app settings + }, + To: []mail.Address{ + { + Name: name, + Address: email, + }, + }, + Subject: subject, + HTML: body, // Use HTML body for formatted content + } + + // Send the email + if err := app.NewMailClient().Send(message); err != nil { + log.Printf("Failed to send password reset email to user: %v", err) + return err + } + } + + return nil + }) + //send mail while status changed from app admin app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error { if e.Collection.Name == "users" { diff --git a/pb_data/data.db-shm b/pb_data/data.db-shm deleted file mode 100644 index fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeIuAr62r3 Date: Thu, 2 Jan 2025 13:40:44 +0530 Subject: [PATCH 2/2] remove password mail --- go.mod | 2 +- main.go | 111 -------------------------------------------------------- 2 files changed, 1 insertion(+), 112 deletions(-) diff --git a/go.mod b/go.mod index ef55587..691f9bc 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.3 require ( github.com/labstack/echo/v5 v5.0.0-20230722203903-ec5b858dab61 + github.com/pocketbase/dbx v1.10.1 github.com/pocketbase/pocketbase v0.22.23 ) @@ -49,7 +50,6 @@ require ( github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect github.com/ncruces/go-strftime v0.1.9 // indirect - github.com/pocketbase/dbx v1.10.1 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/spf13/cast v1.7.0 // indirect github.com/spf13/cobra v1.8.1 // indirect diff --git a/main.go b/main.go index b9a192e..64840dc 100644 --- a/main.go +++ b/main.go @@ -153,117 +153,6 @@ func main() { app = pocketbase.New() - app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error { - if e.Collection.Name == "users" { - // Fetch the original record by ID - originalRecord, err := app.Dao().FindRecordById("users", e.Record.GetString("id")) - if err != nil { - return fmt.Errorf("failed to fetch original record: %v", err) - } - - // Compare the original `passwordHash` with the updated `passwordHash` - originalPasswordHash := originalRecord.GetString("passwordHash") - updatedPasswordHash := e.Record.GetString("passwordHash") - - if originalPasswordHash != updatedPasswordHash { - fmt.Println("Password changed for user:", e.Record.GetString("username")) - - // Fetch user details for the email - name := e.Record.GetString("username") - email := e.Record.GetString("email") - - if name == "" || email == "" { - return fmt.Errorf("user details missing: name or email") - } - - // Send an email notification - subject := "Password Change Notification" - body := fmt.Sprintf(` -

Hello %s,

-

Your password has been successfully changed. If this was not you, please contact support immediately.

-

Best regards,
Your Team

- `, name) - - // Create the email message - message := &mailer.Message{ - From: mail.Address{ - Name: "FCSC", // Sender name - Address: app.Settings().Meta.SenderAddress, // Sender email address from app settings - }, - To: []mail.Address{ - { - Name: name, - Address: email, - }, - }, - Subject: subject, - HTML: body, // Use HTML body for formatted content - } - - // Send the email - err := app.NewMailClient().Send(message) - if err != nil { - log.Printf("Failed to send password change email to user: %v", err) - return err - } - } - } - - return nil - }) - - // Handle password resets explicitly - - app.OnRecordAfterAuthWithPasswordRequest().Add(func(e *core.RecordAuthWithPasswordEvent) error { - if e.Collection.Name == "users" { - fmt.Println("Password reset detected for user:", e.Record.GetString("username")) - - // Optionally, log the password reset time or other actions - resetTime := time.Now().Format(time.RFC3339) - fmt.Println("Password reset time:", resetTime) - - // Fetch user details for the email - name := e.Record.GetString("username") - email := e.Record.GetString("email") - - if name == "" || email == "" { - return fmt.Errorf("user details missing: name or email") - } - - // Send an email notification - subject := "Password Reset Notification" - body := fmt.Sprintf(` -

Hello %s,

-

Your password has been successfully reset on %s. If this was not you, please contact support immediately.

-

Best regards,
Your Team

- `, name, resetTime) - - // Create the email message - message := &mailer.Message{ - From: mail.Address{ - Name: "FCSC", // Sender name - Address: app.Settings().Meta.SenderAddress, // Sender email address from app settings - }, - To: []mail.Address{ - { - Name: name, - Address: email, - }, - }, - Subject: subject, - HTML: body, // Use HTML body for formatted content - } - - // Send the email - if err := app.NewMailClient().Send(message); err != nil { - log.Printf("Failed to send password reset email to user: %v", err) - return err - } - } - - return nil - }) - //send mail while status changed from app admin app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error { if e.Collection.Name == "users" {