-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandleusers.go
More file actions
142 lines (115 loc) · 3.38 KB
/
handleusers.go
File metadata and controls
142 lines (115 loc) · 3.38 KB
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
package main
import (
"encoding/json"
"net/http"
"time"
"github.com/frozendolphin/Chirpy/internal/auth"
"github.com/frozendolphin/Chirpy/internal/database"
"github.com/google/uuid"
)
type usersInfo struct {
Id uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Email string `json:"email"`
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
IsChiryRed bool `json:"is_chirpy_red"`
}
func (cfg *apiConfig) createUsers(w http.ResponseWriter, r *http.Request) {
type mail struct {
Email string `json:"email"`
Password string `json:"password"`
}
params := mail{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(¶ms)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters", err)
return
}
hashedp, err := auth.HashPassword(params.Password)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't hash the password", err)
return
}
createUser_params := database.CreateUserParams {
Email: params.Email,
HashedPassword: hashedp,
}
user, err := cfg.db.CreateUser(r.Context(), createUser_params)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't create user", err)
return
}
res := usersInfo {
Id: user.ID,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
Email: user.Email,
IsChiryRed: user.IsChirpyRed,
}
respondWithJSON(w, http.StatusCreated, res)
}
func (cfg *apiConfig) resetHits(w http.ResponseWriter, req *http.Request) {
if cfg.platform != "dev" {
respondWithError(w, http.StatusForbidden, "403 Forbidden: Access Denied", nil)
return
}
err := cfg.db.DeleteAllUsers(req.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Request to delete to database Failed", err)
return
}
w.WriteHeader(http.StatusOK)
}
func (cfg *apiConfig) changeEmailPass(w http.ResponseWriter, r *http.Request) {
type requirementreq struct {
Email string `json:"email"`
Password string `json:"password"`
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find access token", err)
return
}
requirement := requirementreq{}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&requirement)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode the request data", err)
return
}
if requirement.Email == "" || requirement.Password == "" {
respondWithError(w, http.StatusBadRequest, "Email or Password not found", err)
return
}
user_id, err := auth.ValidateJWT(token, cfg.secret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "jwt validation failed", err)
return
}
hashpass, err := auth.HashPassword(requirement.Password)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't hash the password", err)
return
}
updateuserparams := database.UpdateUserParams {
Email: requirement.Email,
HashedPassword: hashpass,
ID: user_id,
}
user, err := cfg.db.UpdateUser(r.Context(), updateuserparams)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "couldn't update the database", err)
return
}
res := usersInfo {
Id: user.ID,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
Email: user.Email,
IsChiryRed: user.IsChirpyRed,
}
respondWithJSON(w, http.StatusOK, res)
}