From 3dd66038d3b967938e59d0107e15d4e014437e2f Mon Sep 17 00:00:00 2001 From: Janos Dobronszki Date: Tue, 22 Jun 2021 15:19:22 +0100 Subject: [PATCH 1/2] User fixes v4 (#168) --- user/domain/domain.go | 9 ++++++--- user/examples.json | 14 ++++++++++++++ user/handler/handler.go | 2 +- user/proto/user.pb.go | 21 ++++++++++++++++----- user/proto/user.proto | 3 ++- 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/user/domain/domain.go b/user/domain/domain.go index 86a1e54..3c301ee 100644 --- a/user/domain/domain.go +++ b/user/domain/domain.go @@ -53,13 +53,13 @@ func New(db db.DbService) *Domain { } } -func (domain *Domain) SendEmail(fromName, toAddress, toUsername, subject, textContent, token, redirctUrl string) error { +func (domain *Domain) SendEmail(fromName, toAddress, toUsername, subject, textContent, token, redirctUrl, failureRedirectUrl string) error { if domain.sengridKey == "" { return fmt.Errorf("empty email api key") } from := mail.NewEmail(fromName, "support@m3o.com") to := mail.NewEmail(toUsername, toAddress) - textContent = strings.Replace(textContent, "$micro_verification_link", "https://angry-cori-854281.netlify.app?token="+token+"&redirectUrl="+url.QueryEscape(redirctUrl), -1) + textContent = strings.Replace(textContent, "$micro_verification_link", "https://angry-cori-854281.netlify.app?token="+token+"&redirectUrl="+url.QueryEscape(redirctUrl)+"&failureRedirectUrl="+url.QueryEscape(failureRedirectUrl), -1) message := mail.NewSingleEmail(from, subject, to, textContent, "") client := sendgrid.NewSendClient(domain.sengridKey) response, err := client.Send(message) @@ -100,6 +100,9 @@ func (domain *Domain) DeleteSession(ctx context.Context, id string) error { // ReadToken returns the user id func (domain *Domain) ReadToken(ctx context.Context, tokenId string) (string, error) { + if tokenId == "" { + return "", errors.New("token id empty") + } token := &verificationToken{} rsp, err := domain.db.Read(ctx, &db.ReadRequest{ @@ -110,7 +113,7 @@ func (domain *Domain) ReadToken(ctx context.Context, tokenId string) (string, er return "", err } if len(rsp.Records) == 0 { - return "", errors.New("not found") + return "", errors.New("token not found") } m, _ := rsp.Records[0].MarshalJSON() json.Unmarshal(m, token) diff --git a/user/examples.json b/user/examples.json index 3e560ef..55fcf5b 100644 --- a/user/examples.json +++ b/user/examples.json @@ -39,6 +39,7 @@ } } },{ + "title": "Read account by email", "request": { "email": "joe@example.com" @@ -52,5 +53,18 @@ "updated": "1623677579" } } + }], + "sendVerificationEmail": [{ + "title": "Send verification email", + "request": { + "email": "joe@example.com", + "subject": "Email verification", + "redirectUrl": "https://m3o.com", + "failureRedirectUrl": "https://m3o.com/verification-failed", + "textContent": "Hi there,\n\nPlease verify your email by clicking this link: $micro_verification_link", + "fromName": "Awesome Dot Com" + }, + "response": { + } }] } diff --git a/user/handler/handler.go b/user/handler/handler.go index 17f23bc..e3cbab0 100644 --- a/user/handler/handler.go +++ b/user/handler/handler.go @@ -212,5 +212,5 @@ func (s *User) SendVerificationEmail(ctx context.Context, req *pb.SendVerificati return err } - return s.domain.SendEmail(req.FromName, req.Email, users[0].Username, req.Subject, req.TextContent, token, req.RedirectUrl) + return s.domain.SendEmail(req.FromName, req.Email, users[0].Username, req.Subject, req.TextContent, token, req.RedirectUrl, req.FailureRedirectUrl) } diff --git a/user/proto/user.pb.go b/user/proto/user.pb.go index c4257a2..d87835d 100644 --- a/user/proto/user.pb.go +++ b/user/proto/user.pb.go @@ -1128,11 +1128,12 @@ type SendVerificationEmailRequest struct { // The variable will be replaced with an actual url that will look similar to this: // 'https://user.m3o.com/user/verify?token=a-verification-token&rediretUrl=your-redir-url' // HTML emails are not available currently. - TextContent string `protobuf:"bytes,3,opt,name=textContent,proto3" json:"textContent,omitempty"` - RedirectUrl string `protobuf:"bytes,4,opt,name=redirectUrl,proto3" json:"redirectUrl,omitempty"` + TextContent string `protobuf:"bytes,3,opt,name=textContent,proto3" json:"textContent,omitempty"` + RedirectUrl string `protobuf:"bytes,4,opt,name=redirectUrl,proto3" json:"redirectUrl,omitempty"` + FailureRedirectUrl string `protobuf:"bytes,5,opt,name=failureRedirectUrl,proto3" json:"failureRedirectUrl,omitempty"` // While the from email address can't be changed, // the from name (ie. sender name) can. - FromName string `protobuf:"bytes,5,opt,name=fromName,proto3" json:"fromName,omitempty"` + FromName string `protobuf:"bytes,6,opt,name=fromName,proto3" json:"fromName,omitempty"` } func (x *SendVerificationEmailRequest) Reset() { @@ -1195,6 +1196,13 @@ func (x *SendVerificationEmailRequest) GetRedirectUrl() string { return "" } +func (x *SendVerificationEmailRequest) GetFailureRedirectUrl() string { + if x != nil { + return x.FailureRedirectUrl + } + return "" +} + func (x *SendVerificationEmailRequest) GetFromName() string { if x != nil { return x.FromName @@ -1334,7 +1342,7 @@ var file_proto_user_proto_rawDesc = []byte{ 0x79, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x22, 0x15, 0x0a, 0x13, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x45, 0x6d, 0x61, - 0x69, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0xae, 0x01, 0x0a, 0x1c, 0x53, + 0x69, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0xde, 0x01, 0x0a, 0x1c, 0x53, 0x65, 0x6e, 0x64, 0x56, 0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x65, 0x6d, 0x61, 0x69, @@ -1344,7 +1352,10 @@ var file_proto_user_proto_rawDesc = []byte{ 0x52, 0x0b, 0x74, 0x65, 0x78, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x20, 0x0a, 0x0b, 0x72, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x55, 0x72, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x72, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x55, 0x72, 0x6c, 0x12, - 0x1a, 0x0a, 0x08, 0x66, 0x72, 0x6f, 0x6d, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, + 0x2e, 0x0a, 0x12, 0x66, 0x61, 0x69, 0x6c, 0x75, 0x72, 0x65, 0x52, 0x65, 0x64, 0x69, 0x72, 0x65, + 0x63, 0x74, 0x55, 0x72, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x12, 0x66, 0x61, 0x69, + 0x6c, 0x75, 0x72, 0x65, 0x52, 0x65, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x55, 0x72, 0x6c, 0x12, + 0x1a, 0x0a, 0x08, 0x66, 0x72, 0x6f, 0x6d, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x66, 0x72, 0x6f, 0x6d, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x1f, 0x0a, 0x1d, 0x53, 0x65, 0x6e, 0x64, 0x56, 0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0x86, 0x05, 0x0a, diff --git a/user/proto/user.proto b/user/proto/user.proto index aa437f8..173ee2f 100644 --- a/user/proto/user.proto +++ b/user/proto/user.proto @@ -158,9 +158,10 @@ message SendVerificationEmailRequest{ // HTML emails are not available currently. string textContent = 3; string redirectUrl = 4; + string failureRedirectUrl = 5; // While the from email address can't be changed, // the from name (ie. sender name) can. - string fromName = 5; + string fromName = 6; } message SendVerificationEmailResponse{} \ No newline at end of file From 0952a2ed36ba048db6c482afabb8e75fcc7cd18b Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 22 Jun 2021 18:32:01 +0100 Subject: [PATCH 2/2] Update README.md --- user/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user/README.md b/user/README.md index 83f4266..b45cf57 100644 --- a/user/README.md +++ b/user/README.md @@ -1,6 +1,6 @@ User management and authentication -# Users Service +# User Service -The users service provides user account management and authentication +The user service provides user account management and authentication