-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes_api_auth_.go
178 lines (131 loc) · 3.65 KB
/
routes_api_auth_.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"net/http"
"time"
"todo-app/data"
"github.com/julienschmidt/httprouter"
)
type LoginData struct {
Email string `json:"email"`
Password string `json:"password"`
}
// POST -> signup
func signup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
user := data.User{}
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
respond(w, message(false, "Invalid request"), http.StatusBadRequest)
return
}
// check if email doesn't already exists
emailExists, err := data.UserEmailExists(user.Email)
if err != nil {
respond(w, message(false, err.Error()), http.StatusBadRequest)
return
}
if emailExists {
respond(w, message(false, "A user with this email already exists"), http.StatusBadRequest)
return
}
// check if password is atleast 6 characters.. then
if len(user.Password) < 6 {
respond(w, message(false, "Password must be atleast 6 characters"), http.StatusBadRequest)
return
}
// hashed password and replace user password with the hashed password
hashedPassword, err := data.GeneratePassword(user.Password)
if err != nil {
// log error
panic(err)
}
user.Password = hashedPassword
err = user.Create()
if err != nil {
//log error
panic(err)
}
user.Password = ""
resp := message(true, "Successful")
resp["data"] = user
respond(w, resp, http.StatusCreated)
}
// POST -> Login
func login(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
loginData := LoginData{}
err := json.NewDecoder(r.Body).Decode(&loginData)
if err != nil {
respond(w, message(false, "Invalid request"), http.StatusBadRequest)
return
}
user, err := data.UserByEmail(loginData.Email)
if err != nil {
respond(w, message(false, "Invalid email or password"), http.StatusBadRequest)
return
}
err = data.ComparePasswords(user.Password, loginData.Password)
if err != nil {
respond(w, message(false, "Invalid email or password"), http.StatusBadRequest)
return
}
// create a new random session token
sessionToken := createUUID()
_, err = cache.Do("SET", sessionToken, user.Email)
if err != nil {
panic(err)
// respond(w, message(false, err.Error()), http.StatusInternalServerError)
// return
}
cookie := http.Cookie{
Name: "session_token",
Value: sessionToken,
HttpOnly: true,
Expires: time.Now().Add(72 * time.Hour),
Path: "/",
}
http.SetCookie(w, &cookie)
resp := message(true, "Successful")
resp["data"] = user
respond(w, resp, http.StatusOK)
}
func refreshToken(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ctx := r.Context()
userEmail := ctx.Value(userEmailKey)
sessionToken := ctx.Value(sessionTokenKey)
newSessionToken := createUUID()
_, err := cache.Do("SET", newSessionToken, userEmail)
if err != nil {
respond(w, message(false, err.Error()), http.StatusInternalServerError)
return
}
// Delete the older session token
_, err = cache.Do("DEL", sessionToken)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
http.SetCookie(w, &http.Cookie{
Name: "session_token",
Value: newSessionToken,
Expires: time.Now().Add(72 * time.Hour),
})
}
func logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ctx := r.Context()
sessionToken := ctx.Value(sessionTokenKey)
// Delete the session from cache
_, err := cache.Do("DEL", sessionToken)
if err != nil {
respond(w, message(false, err.Error()), http.StatusInternalServerError)
return
}
cookie := http.Cookie{
Name: "session_token",
Value: "",
HttpOnly: true,
Expires: time.Now(),
Path: "/",
}
http.SetCookie(w, &cookie)
respond(w, message(true, "logout successs"), http.StatusOK)
}