fixes, add email msg send, login changed to email

This commit is contained in:
Sergey Chubaryan 2024-08-05 06:10:34 +03:00
parent 7e3d9ec155
commit 233c5cb057
7 changed files with 63 additions and 9 deletions

3
.vscode/launch.json vendored
View File

@ -9,7 +9,8 @@
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "auto", "mode": "auto",
"program": "${workspaceFolder}" "program": "${workspaceFolder}",
"args": ["-c", "./config_example/config.yaml", "-o", "./log.txt"]
} }
] ]
} }

View File

@ -1,6 +1,6 @@
create table users ( create table users (
id int generated always as identity, id int generated always as identity,
login text unique not null, email text unique not null,
secret text not null, secret text not null,
name text not null, name text not null,

2
go.mod
View File

@ -49,5 +49,7 @@ require (
golang.org/x/sys v0.22.0 // indirect golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
) )

4
go.sum
View File

@ -122,11 +122,15 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -67,7 +67,7 @@ func main() {
logger.Fatal().Err(err).Msg("failed parsing postgres connection string") logger.Fatal().Err(err).Msg("failed parsing postgres connection string")
} }
sqlDb := stdlib.OpenDB(connConf) sqlDb = stdlib.OpenDB(connConf)
if err := sqlDb.Ping(); err != nil { if err := sqlDb.Ping(); err != nil {
logger.Fatal().Err(err).Msg("failed pinging postgres db") logger.Fatal().Err(err).Msg("failed pinging postgres db")
} }
@ -77,6 +77,8 @@ func main() {
passwordUtil := utils.NewPasswordUtil() passwordUtil := utils.NewPasswordUtil()
userRepo := repo.NewUserRepo(sqlDb) userRepo := repo.NewUserRepo(sqlDb)
userCache := repo.NewCacheInmem[string, models.UserDTO](60 * 60) userCache := repo.NewCacheInmem[string, models.UserDTO](60 * 60)
emailRepo := repo.NewEmailRepo()
actionTokenRepo := repo.NewActionTokenRepo(sqlDb)
userService := services.NewUserService( userService := services.NewUserService(
services.UserServiceDeps{ services.UserServiceDeps{
@ -84,6 +86,8 @@ func main() {
Password: passwordUtil, Password: passwordUtil,
UserRepo: userRepo, UserRepo: userRepo,
UserCache: userCache, UserCache: userCache,
EmailRepo: emailRepo,
ActionTokenRepo: actionTokenRepo,
}, },
) )

View File

@ -1,5 +1,48 @@
package repo package repo
import (
"strings"
"gopkg.in/gomail.v2"
)
const MSG_TEXT = `
<html>
<head>
</head>
<body>
<p>This message was sent because you forgot a password</p>
<p>To change a password, use <a href="{{Link}}"/>this</a> link</p>
</body>
</html>
`
type EmailRepo interface { type EmailRepo interface {
SendEmailForgotPassword(email, token string) SendEmailForgotPassword(email, token string)
} }
func NewEmailRepo() EmailRepo {
return &emailRepo{}
}
type emailRepo struct {
// mail *gomail.Dialer
}
func (e *emailRepo) SendEmailForgotPassword(email, token string) {
link := "https://nucrea.ru?token=" + token
msgText := strings.ReplaceAll(MSG_TEXT, "{{Link}}", link)
m := gomail.NewMessage()
m.SetHeader("From", "email")
m.SetHeader("To", email)
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", msgText)
d := gomail.NewDialer("smtp.yandex.ru", 587, "login", "password")
// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}

View File

@ -107,7 +107,7 @@ func (u *userService) AuthenticateUser(ctx context.Context, email, password stri
} }
func (u *userService) HelpPasswordForgot(ctx context.Context, userId string) error { func (u *userService) HelpPasswordForgot(ctx context.Context, userId string) error {
user, err := u.deps.UserRepo.GetUserById(ctx, userId) user, err := u.getUserById(ctx, userId)
if err != nil { if err != nil {
return err return err
} }
@ -129,7 +129,7 @@ func (u *userService) HelpPasswordForgot(ctx context.Context, userId string) err
} }
func (u *userService) ChangePasswordForgot(ctx context.Context, userId, newPassword, accessCode string) error { func (u *userService) ChangePasswordForgot(ctx context.Context, userId, newPassword, accessCode string) error {
user, err := u.deps.UserRepo.GetUserById(ctx, userId) user, err := u.getUserById(ctx, userId)
if err != nil { if err != nil {
return err return err
} }