-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_registration.go
88 lines (74 loc) · 2.3 KB
/
handler_registration.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
package main
import (
"context"
"encoding/json"
"errors"
"net/http"
"piot-server/utils"
"time"
"github.com/op/go-logging"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type RegistrationHandler struct {
log *logging.Logger
db *mongo.Database
}
func NewRegistrationHandler(log *logging.Logger, db *mongo.Database) *RegistrationHandler {
return &RegistrationHandler{log: log, db: db}
}
func (h *RegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// check http method, POST is required
if r.Method != http.MethodPost {
WriteErrorResponse(w, errors.New("only POST method is allowed"), http.StatusMethodNotAllowed)
return
}
// decode json from request body
var credentials Credentials
err := json.NewDecoder(r.Body).Decode(&credentials)
if err != nil {
WriteErrorResponse(w, err, 400)
return
}
// check required attributes
if len(credentials.Email) == 0 {
WriteErrorResponse(w, errors.New("email field is empty or not specified"), 400)
return
}
if len(credentials.Password) == 0 {
WriteErrorResponse(w, errors.New("password field is empty or not specified"), 400)
return
}
if !ValidateEmail(credentials.Email) {
WriteErrorResponse(w, errors.New("email field has wrong format"), 400)
return
}
// try to find existing user
var user User
collection := h.db.Collection("users")
err = collection.FindOne(context.TODO(), bson.M{"email": credentials.Email}).Decode(&user)
if err == nil {
WriteErrorResponse(w, errors.New("user identified by this email already exists"), 409)
return
}
// generate hash for given password (we don't store passwords in plain form)
hash, err := utils.GetPasswordHash(credentials.Password)
if err != nil {
WriteErrorResponse(w, errors.New("error while hashing password, try again"), 500)
return
}
user.Email = credentials.Email
user.Password = hash
user.Created = int32(time.Now().Unix())
// user does not exist -> create new one
_, err = collection.InsertOne(context.TODO(), user)
if err != nil {
WriteErrorResponse(w, errors.New("User while creating user, try again"), 500)
return
}
var response ResponseResult
response.Result = "Registration successful"
h.log.Debugf("User is registered: %s", user.Email)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}