-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign_handler.go
140 lines (118 loc) ยท 3.28 KB
/
sign_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
134
135
136
137
138
139
140
package handler
import (
"encoding/json"
"github.com/alexedwards/scs/v2"
"github.com/laqiiz/airac/jwt"
"github.com/laqiiz/airac/model"
"gopkg.in/go-playground/validator.v9"
"log"
"net/http"
)
type SignHandler struct {
r model.UserRepository
session *scs.SessionManager
}
func NewSignHandler(session *scs.SessionManager, r model.UserRepository) SignHandler {
return SignHandler{
r: r,
session: session,
}
}
type SignUp struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"min=8,max=100"`
}
type SignIn struct {
Email string `json:"email"`
Password string `json:"password"`
}
func (h *SignHandler) SignUp(w http.ResponseWriter, r *http.Request) {
var signUp SignUp
if err := json.NewDecoder(r.Body).Decode(&signUp); err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
log.Println(err)
return
}
// validate at only sign-up because do not save database for illegal variable
if err := validator.New().Struct(signUp); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
log.Println(err)
return
}
ctx := r.Context()
_, err := h.r.GetByEmail(ctx, signUp.Email)
if err != nil && err != model.NotFound {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
log.Println(err)
return
}
if err == nil {
body := model.ProblemError{
Title: "mail addr already exists",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusConflict)
_ = json.NewEncoder(w).Encode(body)
return
}
up, err := model.NewSignUp(signUp.Email, signUp.Password)
if err != nil {
panic(err) //TODO Temporary impl
}
if err := h.r.Insert(ctx, up); err != nil {
body := model.ProblemError{
Title: "insert error: " + err.Error(),
}
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(body)
return
}
h.session.Put(ctx, "user_id", up.ID)
w.WriteHeader(http.StatusCreated)
w.Header().Set("content-type", "application/json")
_ = json.NewEncoder(w).Encode(up)
}
func (h *SignHandler) SignIn(w http.ResponseWriter, r *http.Request) {
var signIn SignIn
if err := json.NewDecoder(r.Body).Decode(&signIn); err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
log.Println(err)
return
}
ctx := r.Context()
userInfo, err := h.r.GetByEmail(ctx, signIn.Email)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
if err == model.NotFound {
body := model.ProblemError{
Title: "mail addr is not found",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_ = json.NewEncoder(w).Encode(body)
return
}
h.session.Put(ctx, "user_id", userInfo.ID)
jwtToken, err := jwt.GenerateToken(userInfo.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
// Cookie
http.SetCookie(w, &http.Cookie{ //TODO ไปใฎใชใใทใงใณใๆค่จ
Name: "jwtToken",
Value: jwtToken,
})
w.WriteHeader(http.StatusOK)
w.Header().Set("content-type", "application/json")
_ = json.NewEncoder(w).Encode(userInfo)
}