diff --git a/cmd/api/users.go b/cmd/api/users.go index b77ae2e..abe100f 100644 --- a/cmd/api/users.go +++ b/cmd/api/users.go @@ -3,6 +3,7 @@ package main import ( "errors" "net/http" + "time" "gitea.local.lab/Lbenedar/greenlight/internal/data" "gitea.local.lab/Lbenedar/greenlight/internal/validator" @@ -52,8 +53,18 @@ func (app *application) registerUserHandler(w http.ResponseWriter, r *http.Reque return } + token, err := app.models.Tokens.New(user.ID, 3*24*time.Hour, data.ScopeActivation) + if err != nil { + app.serverErrorResponse(w, r, err) + return + } + app.background(func() { - err = app.mailer.Send(user.Email, "user_welcome.tmpl", user) + data := map[string]any{ + "activationToken": token.Plaintext, + "userID": user.ID, + } + err = app.mailer.Send(user.Email, "user_welcome.tmpl", data) if err != nil { app.logger.PrintError(err, nil) } diff --git a/internal/mailer/templates/user_welcome.tmpl b/internal/mailer/templates/user_welcome.tmpl index 179cb38..683a73a 100644 --- a/internal/mailer/templates/user_welcome.tmpl +++ b/internal/mailer/templates/user_welcome.tmpl @@ -7,6 +7,13 @@ Thanks for signing up for a Greenlight account. We're excited to have you on boa For future reference, your user ID number is {{.ID}}. +Please send a request to the `PUT /v1/users/activated` endpoint with the following JSON +body to activate your account: + +{"token": "{{.activationToken}}"} + +Please note that this is a one-time use token and it will expire in 3 days. + Thanks, The Greenlight Team @@ -26,6 +33,12 @@ The Greenlight Team

Hi,

Thanks for signing up for a Greenlight account. We're excited to have you on board!

For future reference, your user ID number is {{.ID}}.

+

Please send a request to the PUT /v1/users/activated endpoint with the + following JSON body to activate your account:

+

+    {"token": "{{.activationToken}}"}
+    
+

Please note that this is a one-time use token and it will expire in 3 days.

Thanks,

The Greenlight Team