-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
108 lines (99 loc) · 2.69 KB
/
users.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
package controllers
import (
"fmt"
"net/http"
"github.com/baldr9/wdwg/lenslocked/models"
)
type Users struct {
Templates struct {
New Template
SignIn Template
}
UserService *models.UserService
SessionService *models.SessionService
}
func (u Users) New(w http.ResponseWriter, r *http.Request) {
var data struct {
Email string
}
data.Email = r.FormValue("email")
u.Templates.New.Execute(w, r, data)
}
func (u Users) Create(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
password := r.FormValue("password")
user, err := u.UserService.Create(email, password)
if err != nil {
fmt.Println(err)
http.Error(w, "Something went wrong.", http.StatusInternalServerError)
return
}
session, err := u.SessionService.Create(user.ID)
if err != nil {
fmt.Println(err)
// TODO: Long term, we should show a warning about not being able to sign the user in.
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
setCookie(w, CookieSession, session.Token)
http.Redirect(w, r, "/users/me", http.StatusFound)
}
func (u Users) SignIn(w http.ResponseWriter, r *http.Request) {
var data struct {
Email string
}
data.Email = r.FormValue("email")
u.Templates.SignIn.Execute(w, r, data)
}
func (u Users) ProcessSignIn(w http.ResponseWriter, r *http.Request) {
var data struct {
Email string
Password string
}
data.Email = r.FormValue("email")
data.Password = r.FormValue("password")
user, err := u.UserService.Authenticate(data.Email, data.Password)
if err != nil {
fmt.Println(err)
http.Error(w, "Something went wrong.", http.StatusInternalServerError)
return
}
session, err := u.SessionService.Create(user.ID)
if err != nil {
fmt.Println(err)
http.Error(w, "Something went wrong.", http.StatusInternalServerError)
return
}
setCookie(w, CookieSession, session.Token)
http.Redirect(w, r, "/users/me", http.StatusFound)
}
func (u Users) ProcessSignOut(w http.ResponseWriter, r *http.Request) {
token, err := readCookie(r, CookieSession)
if err != nil {
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
err = u.SessionService.Delete(token)
if err != nil {
fmt.Println(err)
http.Error(w, "Something went wrong.", http.StatusInternalServerError)
return
}
deleteCookie(w, CookieSession)
http.Redirect(w, r, "/signin", http.StatusFound)
}
func (u Users) CurrentUser(w http.ResponseWriter, r *http.Request) {
token, err := readCookie(r, CookieSession)
if err != nil {
fmt.Println(err)
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
user, err := u.SessionService.User(token)
if err != nil {
fmt.Println(err)
http.Redirect(w, r, "/signin", http.StatusFound)
return
}
fmt.Fprintf(w, "Current user: %s\n", user.Email)
}