mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-21 15:05:01 +00:00
User service changes part 3 (#163)
This commit is contained in:
@@ -5,12 +5,19 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_struct "github.com/golang/protobuf/ptypes/struct"
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/config"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
db "github.com/micro/services/db/proto"
|
||||
user "github.com/micro/services/user/proto"
|
||||
|
||||
"github.com/sendgrid/sendgrid-go"
|
||||
"github.com/sendgrid/sendgrid-go/helpers/mail"
|
||||
)
|
||||
|
||||
type pw struct {
|
||||
@@ -19,14 +26,46 @@ type pw struct {
|
||||
Salt string `json:"salt"`
|
||||
}
|
||||
|
||||
type verificationToken struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"userId"`
|
||||
}
|
||||
|
||||
type Domain struct {
|
||||
db db.DbService
|
||||
db db.DbService
|
||||
sengridKey string
|
||||
}
|
||||
|
||||
func New(db db.DbService) *Domain {
|
||||
return &Domain{
|
||||
db: db,
|
||||
var key string
|
||||
cfg, err := config.Get("micro.user.sendgrid.api_key")
|
||||
if err == nil {
|
||||
key = cfg.String("")
|
||||
}
|
||||
if len(key) == 0 {
|
||||
logger.Info("No email key found")
|
||||
} else {
|
||||
logger.Info("Email key found")
|
||||
}
|
||||
return &Domain{
|
||||
sengridKey: key,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (domain *Domain) SendEmail(fromName, toAddress, toUsername, subject, textContent, token, redirctUrl 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)
|
||||
message := mail.NewSingleEmail(from, subject, to, textContent, "")
|
||||
client := sendgrid.NewSendClient(domain.sengridKey)
|
||||
response, err := client.Send(message)
|
||||
logger.Info(response)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (domain *Domain) CreateSession(ctx context.Context, sess *user.Session) error {
|
||||
@@ -59,6 +98,44 @@ func (domain *Domain) DeleteSession(ctx context.Context, id string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// ReadToken returns the user id
|
||||
func (domain *Domain) ReadToken(ctx context.Context, tokenId string) (string, error) {
|
||||
token := &verificationToken{}
|
||||
|
||||
rsp, err := domain.db.Read(ctx, &db.ReadRequest{
|
||||
Table: "tokens",
|
||||
Query: fmt.Sprintf("id == '%v'", tokenId),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(rsp.Records) == 0 {
|
||||
return "", errors.New("not found")
|
||||
}
|
||||
m, _ := rsp.Records[0].MarshalJSON()
|
||||
json.Unmarshal(m, token)
|
||||
return token.UserID, nil
|
||||
}
|
||||
|
||||
// CreateToken returns the created and saved token
|
||||
func (domain *Domain) CreateToken(ctx context.Context, userId string) (string, error) {
|
||||
s := &_struct.Struct{}
|
||||
tokenId := uuid.New().String()
|
||||
jso, _ := json.Marshal(verificationToken{
|
||||
ID: tokenId,
|
||||
UserID: userId,
|
||||
})
|
||||
err := s.UnmarshalJSON(jso)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = domain.db.Create(ctx, &db.CreateRequest{
|
||||
Table: "tokens",
|
||||
Record: s,
|
||||
})
|
||||
return tokenId, err
|
||||
}
|
||||
|
||||
func (domain *Domain) ReadSession(ctx context.Context, id string) (*user.Session, error) {
|
||||
sess := &user.Session{}
|
||||
if len(id) == 0 {
|
||||
@@ -143,12 +220,12 @@ func (domain *Domain) Update(ctx context.Context, user *user.Account) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (domain *Domain) Read(ctx context.Context, id string) (*user.Account, error) {
|
||||
func (domain *Domain) Read(ctx context.Context, userId string) (*user.Account, error) {
|
||||
user := &user.Account{}
|
||||
if len(id) == 0 {
|
||||
if len(userId) == 0 {
|
||||
return nil, fmt.Errorf("no id provided")
|
||||
}
|
||||
q := fmt.Sprintf("id == '%v'", id)
|
||||
q := fmt.Sprintf("id == '%v'", userId)
|
||||
logger.Infof("Running query: %v", q)
|
||||
rsp, err := domain.db.Read(ctx, &db.ReadRequest{
|
||||
Table: "users",
|
||||
@@ -211,21 +288,12 @@ func (domain *Domain) UpdatePassword(ctx context.Context, id string, salt string
|
||||
return err
|
||||
}
|
||||
|
||||
func (domain *Domain) SaltAndPassword(ctx context.Context, username, email string) (string, string, error) {
|
||||
var query string
|
||||
if len(username) > 0 {
|
||||
query = fmt.Sprintf("username == '%v'", username)
|
||||
} else if len(email) > 0 {
|
||||
query = fmt.Sprintf("email == '%v'", email)
|
||||
} else {
|
||||
return "", "", errors.New("username and email cannot be blank")
|
||||
}
|
||||
|
||||
func (domain *Domain) SaltAndPassword(ctx context.Context, userId string) (string, string, error) {
|
||||
password := &pw{}
|
||||
|
||||
rsp, err := domain.db.Read(ctx, &db.ReadRequest{
|
||||
Table: "passwords",
|
||||
Query: query,
|
||||
Query: fmt.Sprintf("id == '%v'", userId),
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
|
||||
Reference in New Issue
Block a user