feat(api): validate emails correctly
This commit is contained in:
parent
cccf28bc03
commit
57aec575c8
|
|
@ -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 == "" {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Database) UpdatePassword(user User) error {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user