user verification added
This commit is contained in:
parent
e1c406fbc6
commit
9059840e77
113
main.go
113
main.go
@ -28,8 +28,11 @@ type EmailConfigurationResponse struct {
|
||||
} `json:"items"`
|
||||
}
|
||||
|
||||
var app *pocketbase.PocketBase
|
||||
|
||||
func main() {
|
||||
app := pocketbase.New()
|
||||
// app := pocketbase.New()
|
||||
app = pocketbase.New()
|
||||
|
||||
app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
|
||||
if e.Collection.Name == "users" {
|
||||
@ -105,6 +108,62 @@ func main() {
|
||||
return nil
|
||||
})
|
||||
|
||||
//Hook to send a verification link to register user
|
||||
app.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error {
|
||||
if e.Record.Collection().Name == "users" {
|
||||
// Retrieve user details
|
||||
name := e.Record.GetString("username")
|
||||
email := e.Record.GetString("email")
|
||||
userID := e.Record.Id // User ID
|
||||
|
||||
// Email subject
|
||||
subject := "Please Verify Your Email Address"
|
||||
|
||||
// Generate a verification URL (example: using user ID or token)
|
||||
verificationURL := fmt.Sprintf("https://pb.venbait.in/verify-email?userId=%s", userID)
|
||||
|
||||
// Email body
|
||||
body := fmt.Sprintf(`
|
||||
<html>
|
||||
<body>
|
||||
<p>Dear %s,</p>
|
||||
<p>Thank you for registering. Please click the link below to verify your email address:</p>
|
||||
<p>
|
||||
<a href="%s" style="padding: 10px 15px; background-color: blue; color: white; text-decoration: none; border-radius: 5px;">Verify Email</a>
|
||||
</p>
|
||||
<p>If you did not register for this account, please ignore this email.</p>
|
||||
</body>
|
||||
</html>
|
||||
`, name, verificationURL)
|
||||
|
||||
// Create the email message
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Name: "FCSC",
|
||||
Address: app.Settings().Meta.SenderAddress,
|
||||
},
|
||||
To: []mail.Address{
|
||||
{
|
||||
Name: name,
|
||||
Address: email,
|
||||
},
|
||||
},
|
||||
Subject: subject,
|
||||
HTML: body,
|
||||
}
|
||||
|
||||
// Send the email
|
||||
err := app.NewMailClient().Send(message)
|
||||
if err != nil {
|
||||
log.Printf("Failed to send verification email to user: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("Verification email sent successfully to the user.")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// Hook to send an email after a user is created
|
||||
app.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error {
|
||||
|
||||
@ -419,6 +478,7 @@ Email: %s</p>
|
||||
|
||||
// Serve static files from the provided public directory (if exists)
|
||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
e.Router.GET("/verify-email", verifyEmailHandler)
|
||||
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public"), false))
|
||||
return nil
|
||||
})
|
||||
@ -486,6 +546,8 @@ Email: %s</p>
|
||||
}
|
||||
}()
|
||||
|
||||
//app.Router.GET("/verify-email", verifyEmailHandler)
|
||||
|
||||
// Start your custom HTTP server on port 8091
|
||||
log.Println("Starting custom HTTP server on :8091")
|
||||
if err := http.ListenAndServe(":8091", nil); err != nil {
|
||||
@ -502,3 +564,52 @@ func generateOTP() (int, error) {
|
||||
}
|
||||
return int(n.Int64()), nil
|
||||
}
|
||||
|
||||
// Email verification handler
|
||||
func verifyEmailHandler(c echo.Context) error {
|
||||
log.Println("Starting verifyEmailHandler")
|
||||
|
||||
// Get the userId from the query parameter
|
||||
userID := c.QueryParam("userId")
|
||||
|
||||
if userID == "" {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "User ID not provided",
|
||||
})
|
||||
}
|
||||
|
||||
// Check if app is properly initialized
|
||||
if app == nil {
|
||||
log.Fatal("PocketBase app is not initialized")
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Internal server error",
|
||||
})
|
||||
}
|
||||
|
||||
// Find the user record in PocketBase by userId
|
||||
record, err := app.Dao().FindRecordById("users", userID)
|
||||
if err != nil {
|
||||
log.Printf("Error finding user: %v", err)
|
||||
return c.JSON(500, map[string]interface{}{
|
||||
"code": 500,
|
||||
"message": "Error finding user.",
|
||||
})
|
||||
}
|
||||
|
||||
// Set the verified status to true
|
||||
record.Set("user_mail_verify", true)
|
||||
log.Printf("Failed to verify user: %v", err)
|
||||
if err := app.Dao().SaveRecord(record); err != nil {
|
||||
return c.JSON(500, map[string]interface{}{
|
||||
"code": 500,
|
||||
"message": "Failed to verify user.",
|
||||
})
|
||||
}
|
||||
|
||||
// Redirect to the login page after successful verification
|
||||
log.Println("Email verification successful")
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
"message": "Email verified successfully!",
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
BIN
pb_data/logs.db
BIN
pb_data/logs.db
Binary file not shown.
Loading…
Reference in New Issue
Block a user