/
user.handler.go
133 lines (110 loc) · 3.45 KB
/
user.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
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
package handler
import (
"database/sql"
"fmt"
"log/slog"
"net/http"
"strings"
"github.com/crowmw/risiti/model"
"github.com/crowmw/risiti/service"
"github.com/crowmw/risiti/view/signin"
"github.com/crowmw/risiti/view/signup"
"github.com/microcosm-cc/bluemonday"
"golang.org/x/crypto/bcrypt"
"gopkg.in/validator.v2"
)
type UserHandler struct {
UserService service.IUserService
AuthService service.AuthService
}
func NewUserHandler(us service.IUserService, jwt service.AuthService) *UserHandler {
return &UserHandler{
UserService: us,
AuthService: jwt,
}
}
func (h *UserHandler) GetSignin(w http.ResponseWriter, r *http.Request) {
RenderView(w, r, signin.Show("", ""), "/signin")
}
func (h *UserHandler) GetSignup(w http.ResponseWriter, r *http.Request) {
// Check is any user exists in system
anyUserExists, err := h.UserService.AnyExists()
if err != nil {
OnError(w, err, "Internal Server Error", http.StatusInternalServerError)
return
}
if !anyUserExists {
RenderView(w, r, signup.Show("", ""), "/signup")
return
}
if err := h.AuthService.Authorize(r); err != nil {
RenderView(w, r, signin.Show("", ""), "/signin")
return
}
RenderView(w, r, signup.Show("", ""), "/signup")
}
func (h *UserHandler) PostUser(w http.ResponseWriter, r *http.Request) {
user := model.User{}
// sanitize
s := bluemonday.UGCPolicy()
user.Email, user.Password = s.Sanitize(strings.TrimSpace(r.FormValue("email"))), s.Sanitize(strings.TrimSpace(r.FormValue("password")))
// validate
if err := validator.Validate(user); err != nil {
RenderView(w, r, signup.Show(user.Email, fmt.Sprint(err)), "/signup")
return
}
passwordConfirm := r.FormValue("confirm")
// Check passwords is the same
if user.Password != passwordConfirm {
RenderView(w, r, signup.Show(user.Email, "Passwords are not the same."), "/signup")
return
}
// Check user is already exists
if _, err := h.UserService.Read(user.Email); err != sql.ErrNoRows {
RenderView(w, r, signup.Show(user.Email, "User "+user.Email+" already exists. Try signin."), "/signup")
return
}
// Create new user
newUser, err := h.UserService.Create(user)
if err != nil {
RenderView(w, r, signup.Show(user.Email, "Something went wrong while creating user."), "/signup")
slog.Error(fmt.Sprint(err))
return
}
// Signin new user
err = h.AuthService.SignIn(&w, &newUser)
if err != nil {
RenderView(w, r, signup.Show(user.Email, "Something went wrong while signing in new user."), "/signup")
return
}
w.Header().Add("HX-Redirect", "/")
}
func (h *UserHandler) PostSignin(w http.ResponseWriter, r *http.Request) {
// sanitize
s := bluemonday.UGCPolicy()
email := s.Sanitize(strings.TrimSpace(r.FormValue("email")))
password := strings.TrimSpace(r.FormValue("password"))
// Check user is already exists
existedUser, err := h.UserService.Read(email)
if err != nil {
RenderView(w, r, signin.Show(email, "Wrong username or password."), "/signin")
return
}
if err = bcrypt.CompareHashAndPassword(
[]byte(existedUser.Password),
[]byte(password),
); err != nil {
RenderView(w, r, signin.Show(email, "Wrong username or password."), "/signin")
return
}
err = h.AuthService.SignIn(&w, &existedUser)
if err != nil {
RenderView(w, r, signin.Show(email, "Something went wrong while signing in user."), "/signin")
return
}
w.Header().Add("HX-Redirect", "/")
}
func (h *UserHandler) GetSignout(w http.ResponseWriter, r *http.Request) {
h.AuthService.SignOut(&w)
w.Header().Add("HX-Redirect", "/")
}