From 57aec575c82b91de32a6375a5b65662c67139892 Mon Sep 17 00:00:00 2001 From: leandrofars Date: Tue, 30 Apr 2024 17:09:48 -0300 Subject: [PATCH] feat(api): validate emails correctly --- .../services/controller/internal/api/user.go | 17 ++++++++++++++++- backend/services/controller/internal/db/db.go | 10 ++++++++++ backend/services/controller/internal/db/user.go | 8 +++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/backend/services/controller/internal/api/user.go b/backend/services/controller/internal/api/user.go index 542264a..bb32b37 100644 --- a/backend/services/controller/internal/api/user.go +++ b/backend/services/controller/internal/api/user.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" "net/http" + "net/mail" "github.com/gorilla/mux" "github.com/leandrofars/oktopus/internal/api/auth" @@ -33,7 +34,6 @@ func (a *Api) retrieveUsers(w http.ResponseWriter, r *http.Request) { if err != nil { log.Println(err) } - return } func (a *Api) registerUser(w http.ResponseWriter, r *http.Request) { @@ -70,12 +70,27 @@ func (a *Api) registerUser(w http.ResponseWriter, r *http.Request) { return } + if user.Email == "" || user.Password == "" || !valid(user.Email) { + w.WriteHeader(http.StatusBadRequest) + return + } + if err := a.db.RegisterUser(user); err != nil { + if err == db.ErrorUserExists { + w.WriteHeader(http.StatusConflict) + w.Write([]byte("User with this email already exists")) + return + } w.WriteHeader(http.StatusInternalServerError) return } } +func valid(email string) bool { + _, err := mail.ParseAddress(email) + return err == nil +} + func (a *Api) deleteUser(w http.ResponseWriter, r *http.Request) { tokenString := r.Header.Get("Authorization") if tokenString == "" { diff --git a/backend/services/controller/internal/db/db.go b/backend/services/controller/internal/db/db.go index 820a925..c3365b2 100644 --- a/backend/services/controller/internal/db/db.go +++ b/backend/services/controller/internal/db/db.go @@ -4,6 +4,7 @@ import ( "context" "log" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -33,6 +34,15 @@ func NewDatabase(ctx context.Context, mongoUri string) Database { log.Println("Connected to MongoDB-->", mongoUri) db.users = client.Database("account-mngr").Collection("users") + indexField := bson.M{"email": 1} + _, err = db.users.Indexes().CreateOne(ctx, mongo.IndexModel{ + Keys: indexField, + Options: options.Index().SetUnique(true), + }) + if err != nil { + log.Fatalln(err) + } + db.ctx = ctx return db diff --git a/backend/services/controller/internal/db/user.go b/backend/services/controller/internal/db/user.go index af0950f..e7e2cfc 100644 --- a/backend/services/controller/internal/db/user.go +++ b/backend/services/controller/internal/db/user.go @@ -1,6 +1,7 @@ package db import ( + "errors" "log" "go.mongodb.org/mongo-driver/bson" @@ -13,8 +14,11 @@ type User struct { Name string `json:"name"` Password string `json:"password,omitempty"` Level int `json:"level"` + Phone string `json:"phone"` } +var ErrorUserExists = errors.New("User already exists") + func (d *Database) RegisterUser(user User) error { err := d.users.FindOne(d.ctx, bson.D{{"email", user.Email}}).Err() if err != nil { @@ -23,8 +27,10 @@ func (d *Database) RegisterUser(user User) error { return err } log.Println(err) + return err + } else { + return ErrorUserExists } - return err } func (d *Database) UpdatePassword(user User) error {