-
Notifications
You must be signed in to change notification settings - Fork 1
/
users_handler.go
53 lines (43 loc) · 1.4 KB
/
users_handler.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/flosch/pongo2"
"github.com/jonahgeorge/letterdrop/mailers"
"github.com/jonahgeorge/letterdrop/models"
repo "github.com/jonahgeorge/letterdrop/repositories"
"github.com/tuvistavie/securerandom"
)
func (app *Application) UsersNewHandler(w http.ResponseWriter, r *http.Request) {
app.Render(w, r, "users/new", pongo2.Context{})
}
func (app *Application) UsersCreateHandler(w http.ResponseWriter, r *http.Request) {
session, _ := app.GetSession(r)
token, _ := securerandom.UrlSafeBase64(10, true)
newUser := &models.User{
Name: r.PostFormValue("name"),
Email: r.PostFormValue("email"),
PasswordDigest: r.PostFormValue("password"),
EmailConfirmationToken: &token,
}
if !app.recaptchaClient.Verify(*r) {
session.AddFlash("Invalid ReCaptcha")
session.Save(r, w)
app.Render(w, r, "users/new", pongo2.Context{"user": newUser})
return
}
user, err := repo.NewUsersRepository(app.db).Create(newUser)
if err != nil {
log.Println(err)
session.AddFlash("Woah, something bad happened.")
session.Save(r, w)
app.Render(w, r, "users/new", pongo2.Context{"user": newUser})
return
}
mailers.SendEmailConfirmation(app.emailClient, app.hostName, user)
session.Values["userId"] = user.Id
session.AddFlash(fmt.Sprintf("Welcome, %s!", user.Name))
session.Save(r, w)
http.Redirect(w, r, "/", 302)
}