-
Notifications
You must be signed in to change notification settings - Fork 1
/
signup.go
145 lines (122 loc) · 3.77 KB
/
signup.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package auth
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
)
// TODO(!): figure out how to properly cancel the email send when time out
func sendWelcomeMail(ctx *AuthContext, email string) error {
if val, err := ctx.Settings.Get("auth_send_welcome_email"); val != "true" {
return nil
} else if err != nil {
return err
}
mailSettings, err := ctx.Settings.GetMulti([]string{
"auth_full_path",
"auth_welcome_email_subject",
"auth_welcome_email_message",
"auth_email_from",
})
if err != nil {
return err
}
return ctx.Notifications.SendMail(ctx, mailSettings["auth_welcome_email_subject"],
mailSettings["auth_welcome_email_message"],
mailSettings["auth_email_from"], email)
}
func sendActivateMail(ctx *AuthContext, id, email, code string) error {
if val, err := ctx.Settings.Get("auth_send_activate_email"); val != "true" {
return nil
} else if err != nil {
return err
}
mailSettings, err := ctx.Settings.GetMulti([]string{
"auth_full_path",
"auth_activate_page",
"auth_activate_email_subject",
"auth_activate_email_message",
"auth_email_from",
})
if err != nil {
return err
}
activeURL := fmt.Sprintf("%s/users/%s/activate?code=%s", mailSettings["auth_full_path"], id, code)
return ctx.Notifications.SendMail(ctx, mailSettings["auth_activate_email_subject"],
fmt.Sprintf(mailSettings["auth_activate_email_message"], activeURL),
mailSettings["auth_email_from"], email)
}
// SignUp handle registration action.
// Details: http://kidstuff.github.io/swagger/#!/default/signup_post
func SignUp(authCtx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
credential := struct {
Email string
Pwd string
PwdRepeat string
}{}
err := json.NewDecoder(req.Body).Decode(&credential)
if err != nil {
return http.StatusBadRequest, err
}
if credential.Pwd != credential.PwdRepeat {
return http.StatusBadRequest, ErrPwdMismatch
}
app := true
if val, err := authCtx.Settings.
Get("auth_approve_new_user"); err != nil || val != "true" {
app = false
}
u, err := authCtx.Auth.AddUser(credential.Email, credential.PwdRepeat, app)
if err != nil {
return http.StatusPreconditionFailed, err
}
status := 200
if app {
err = sendWelcomeMail(authCtx, *u.Email)
if err != nil {
authCtx.Logs.Errorf("Wellcome mail failed: %s", err)
}
} else {
err = sendActivateMail(authCtx, *u.Id, *u.Email, u.ConfirmCodes["activate"])
if err != nil {
authCtx.Logs.Errorf("Active mail failed: %s", err)
status = http.StatusAccepted
}
}
json.NewEncoder(rw).Encode(u)
return status, nil
}
// Activate handle user activation action. If success change user Approved to true.
// Details: http://kidstuff.github.io/swagger/#!/default/users_user_id_active_get
func Activate(authCtx *AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
vars := mux.Vars(req)
sid := vars["user_id"]
code := req.FormValue("code")
if len(sid) == 0 || len(code) == 0 {
return http.StatusBadRequest, ErrInvalidId
}
u, err := authCtx.Auth.FindUser(sid)
if err != nil {
return http.StatusInternalServerError, err
}
if ok := u.ValidConfirmCode("activate", code, false, true); !ok {
return http.StatusPreconditionFailed, ErrInvalidActiveCode
}
t := true
err = authCtx.Auth.UpdateUserDetail(*u.Id, nil, &t, nil, nil, nil, nil)
if err != nil {
return http.StatusInternalServerError, err
}
err = sendWelcomeMail(authCtx, *u.Email)
if err != nil {
authCtx.Logs.Errorf("Wellcome mail failed: %s", err)
}
activate_redirect, err := authCtx.Settings.Get("auth_activate_redirect")
if err != nil {
authCtx.Logs.Errorf("Error when fetching 'auth_activate_redirect' settings")
return http.StatusNoContent, nil
} else {
http.Redirect(rw, req, activate_redirect, http.StatusSeeOther)
}
return http.StatusOK, nil
}