implement reset cache
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/service/ses"
|
||||
"github.com/aws/aws-sdk-go-v2/service/ses/types"
|
||||
)
|
||||
|
||||
func ResetUserEmail(userEmail UserEmail, customerId CustomerId) error {
|
||||
// Load AWS configuration
|
||||
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"))
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("unable to load SDK config, %v", err))
|
||||
}
|
||||
|
||||
nkodeResetJwt, err := ResetNKodeToken(userEmail, customerId)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("unable to load SDK config, %v", err))
|
||||
}
|
||||
// Create an SES client
|
||||
sesClient := ses.NewFromConfig(cfg)
|
||||
|
||||
// Define sender and recipient
|
||||
sender := "no-reply@nkode.tech"
|
||||
|
||||
// Define email subject and body
|
||||
subject := "nKode Reset"
|
||||
htmlBody := fmt.Sprintf("<h1>Hello!</h1><p>Click the link to reset your nKode.</p><a href=\"%s?token=%s\">Reset nKode</a>", FrontendHost, nkodeResetJwt)
|
||||
|
||||
// Construct the email message
|
||||
input := &ses.SendEmailInput{
|
||||
Destination: &types.Destination{
|
||||
ToAddresses: []string{string(userEmail)},
|
||||
},
|
||||
Message: &types.Message{
|
||||
Body: &types.Body{
|
||||
Html: &types.Content{
|
||||
Data: aws.String(htmlBody),
|
||||
},
|
||||
},
|
||||
Subject: &types.Content{
|
||||
Data: aws.String(subject),
|
||||
},
|
||||
},
|
||||
Source: aws.String(sender),
|
||||
}
|
||||
|
||||
// Send the email
|
||||
resp, err := sesClient.SendEmail(context.TODO(), input)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("failed to send email, %v", err))
|
||||
}
|
||||
|
||||
// Output the message ID of the sent email
|
||||
fmt.Printf("UserEmail sent successfully, Message ID: %s\n", *resp.MessageId)
|
||||
return nil
|
||||
}
|
||||
@@ -84,9 +84,14 @@ func (s *SESClient) SendEmail(email Email) error {
|
||||
Source: aws.String(email.Sender),
|
||||
}
|
||||
|
||||
if err = s.ResetCache.Add(email.Recipient, nil, defaultExpiration); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Send the email
|
||||
resp, err := sesClient.SendEmail(context.TODO(), input)
|
||||
if err != nil {
|
||||
s.ResetCache.Delete(email.Recipient)
|
||||
return fmt.Errorf("failed to send email, %v", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -428,6 +428,7 @@ func (h *NKodeHandler) ResetNKode(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
}
|
||||
log.Print("Resetting email")
|
||||
var resetNKodePost ResetNKodePost
|
||||
err := decodeJson(w, r, &resetNKodePost)
|
||||
if err != nil {
|
||||
|
||||
2
main.go
2
main.go
@@ -18,6 +18,8 @@ func main() {
|
||||
defer db.CloseDb()
|
||||
sesClient := core.NewSESClient()
|
||||
emailQueue := core.NewEmailQueue(emailQueueBufferSize, maxEmailsPerSecond, &sesClient)
|
||||
emailQueue.Start()
|
||||
defer emailQueue.Stop()
|
||||
nkodeApi := core.NewNKodeAPI(db, emailQueue)
|
||||
AddDefaultCustomer(nkodeApi)
|
||||
handler := core.NKodeHandler{Api: nkodeApi}
|
||||
|
||||
@@ -73,6 +73,7 @@ func TestApi(t *testing.T) {
|
||||
UserEmail: username,
|
||||
KeySelection: loginKeySelection,
|
||||
}
|
||||
|
||||
var jwtTokens core.AuthenticationTokens
|
||||
testApiPost(t, base+core.Login, loginBody, &jwtTokens)
|
||||
refreshClaims, err := core.ParseRefreshToken(jwtTokens.RefreshToken)
|
||||
|
||||
Reference in New Issue
Block a user