/
register.go
54 lines (46 loc) · 1.41 KB
/
register.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
package web
import (
"net/http"
"github.com/spring2go/gravitee/oauth/roles"
)
func (s *Service) registerForm(w http.ResponseWriter, r *http.Request) {
// Get the session service from the request context
sessionService, err := getSessionService(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Render the template
errMsg, _ := sessionService.GetFlashMessage()
renderTemplate(w, "register.html", map[string]interface{}{
"error": errMsg,
"queryString": getQueryString(r.URL.Query()),
})
}
func (s *Service) register(w http.ResponseWriter, r *http.Request) {
// Get the session service from the request context
sessionService, err := getSessionService(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Check that the submitted email hasn't been registered already
if s.oauthService.UserExists(r.Form.Get("email")) {
sessionService.SetFlashMessage("Email taken")
http.Redirect(w, r, r.RequestURI, http.StatusFound)
return
}
// Create a user
_, err = s.oauthService.CreateUser(
roles.User, // role ID
r.Form.Get("email"), // username
r.Form.Get("password"), // password
)
if err != nil {
sessionService.SetFlashMessage(err.Error())
http.Redirect(w, r, r.RequestURI, http.StatusFound)
return
}
// Redirect to the login page
redirectWithQueryString("/web/login", r.URL.Query(), w, r)
}