forked from nulpunt/nulpunt
/
http_registerAccount.go
63 lines (55 loc) · 1.53 KB
/
http_registerAccount.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
package main
import (
"encoding/json"
"log"
"net/http"
)
func registerAccountHandlerFunc(w http.ResponseWriter, r *http.Request) {
type inDataType struct {
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
Color string `json:"color"`
Tags []string `json:"tags"`
}
type outDataType struct {
Success bool `json:"success"`
Error string `json:"error"`
}
// defer sending output
outData := &outDataType{}
defer func() {
err := json.NewEncoder(w).Encode(outData)
if err != nil {
log.Printf("could not encode data for registerAccoutn request. %s\n", err)
}
}()
inData := &inDataType{}
err := json.NewDecoder(r.Body).Decode(inData)
r.Body.Close()
if err != nil {
log.Printf("could not decode body for registerAccount request. %s\n", err)
return
}
err = registerNewAccount(inData.Username, inData.Email, inData.Password, inData.Color)
if err != nil {
if err == errAccountUsernameNotUnique {
outData.Error = "Username is already taken."
return
}
log.Printf("error creating an account: %s\n", err)
outData.Error = "A server error occured."
return
}
// Now create a Profile with the selected Tags
profile := newProfile()
profile.Username = inData.Username
profile.Tags = inData.Tags
err = upsertProfile(profile)
if err != nil {
log.Printf("error creating a profile for account (%s): %s\n", inData.Username, err)
// don't signal an error, as we have created an account succesfully, above.
}
// all done
outData.Success = true
}