-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
162 lines (127 loc) · 4.28 KB
/
profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package usersvc
import (
"context"
"log"
"strings"
"time"
emailer "github.com/sisukasco/commons/email"
"github.com/sisukasco/commons/http_utils"
"github.com/sisukasco/commons/stringid"
"github.com/sisukasco/commons/utils"
"github.com/sisukasco/henki/pkg/db"
"github.com/cbroglie/mustache"
"github.com/pkg/errors"
)
type EmailUpdateInfo struct {
FirstName string
Link string
}
func (usvc *UserService) sendEmailUpdateEmail(ctx context.Context, userID string,
new_email string) {
user, err := usvc.svc.DB.Q.GetUser(ctx, userID)
if err != nil {
log.Printf("Error while sending email change link: user not found %v", err)
return
}
token := stringid.RandString(22)
for i := 0; i < 100; i++ {
exists, err := usvc.svc.DB.Q.DoesEmailUpdateTokenExist(ctx, token)
if !exists || err == nil {
break
}
token = stringid.RandString(22)
}
err = usvc.svc.DB.Q.InitUpdateUserEmail(ctx,
db.InitUpdateUserEmailParams{ID: user.ID, EmailChange: new_email, EmailChangeToken: token})
if err != nil {
log.Printf("Error while sending email change link: Error updating token code %v", err)
return
}
site := usvc.svc.Konf.String("client.url")
subj := usvc.svc.Konf.String("emails.email-update.subject")
templateUpdateEmail := usvc.svc.Konf.String("emails.email-update.body")
link := site + "/auth/email-update/?token=" + token
e := EmailUpdateInfo{user.FirstName, link}
eb, err := mustache.Render(templateUpdateEmail, &e)
if err != nil {
log.Printf("Error while sending email- mustache rendering error %v", err)
return
}
mailer := usvc.getMailer()
err = mailer.SendEmail(new_email, subj, eb)
if err != nil {
log.Printf("Error while sending email- SendEmail error %v", err)
return
}
log.Println("email update confirmation email sent")
}
func (usvc *UserService) getMailerConfig() *emailer.EmailConfig {
if usvc.svc.Konf.Exists("mailer.ses") {
ss := usvc.svc.Konf.StringMap("mailer.ses")
return emailer.NewSESConfig(usvc.svc.Konf.String("mailer.from"),
ss["region"], ss["access"], ss["secret"])
} else if usvc.svc.Konf.Exists("mailer.smtp") {
ss := usvc.svc.Konf.StringMap("mailer.smtp")
return emailer.NewSMTPConfig(usvc.svc.Konf.String("mailer.from"),
ss["host"], ss["user"], ss["pass"])
} else {
return &emailer.EmailConfig{}
}
}
func (usvc *UserService) getMailer() *emailer.Emailer {
return emailer.NewEmailer(usvc.getMailerConfig())
}
func (usvc *UserService) UpdateProfileField(ctx context.Context, user *db.User,
fieldName string, fieldValue string) error {
var err error
switch fieldName {
case "first_name":
err = usvc.svc.DB.Q.UpdateUserFirstName(ctx,
db.UpdateUserFirstNameParams{user.ID, fieldValue})
case "last_name":
err = usvc.svc.DB.Q.UpdateUserLastName(ctx,
db.UpdateUserLastNameParams{user.ID, fieldValue})
case "email":
email := utils.CleanupString(fieldValue)
if len(email) <= 0 {
return http_utils.UnprocessableEntityError("Email can't be empty")
}
exists, _ := usvc.svc.DB.Q.DoesUserExist(ctx, email)
if exists {
return http_utils.UnprocessableEntityError("A user with the same email address already exists.")
}
usvc.PostEmailUpdateConfirmation(user.ID, email)
default:
return errors.New("Unknown Field name " + fieldName)
}
return err
}
func (usvc *UserService) PostEmailUpdateConfirmation(userID string, email string) {
usvc.wg.Add(1)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
go func() {
defer cancel()
defer usvc.wg.Done()
usvc.sendEmailUpdateEmail(ctx, userID, email)
}()
}
func (usvc *UserService) CompleteEmailUpdate(ctx context.Context, token string) error {
token = strings.TrimSpace(token)
ur, err := usvc.svc.DB.Q.GetUserFromEmailUpdateToken(ctx, token)
if err != nil {
return http_utils.InternalServerError("Please check the token code").WithInternalError(err)
}
if len(ur.EmailChange) <= 0 {
return http_utils.UnprocessableEntityError("Bad token %s", token)
}
diff := time.Now().Sub(ur.EmailChangeSentAt.Time).Hours()
if diff > 48 {
return http_utils.UnprocessableEntityError("This token got expired. Token gets expired after 48 hours")
}
err = usvc.svc.DB.Q.UpdateUserEmail(ctx, db.UpdateUserEmailParams{
ur.ID, ur.EmailChange})
if err != nil {
return http_utils.InternalServerError("Couldn't get User Rec").WithInternalError(err)
}
return nil
}