-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
127 lines (111 loc) · 3.46 KB
/
user.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
package main
import (
"encoding/json"
"log"
"net/http"
"time"
"example.com/chirpy/internal/auth"
"example.com/chirpy/internal/database"
"github.com/google/uuid"
)
const accessTokenExpiresIn time.Duration = time.Hour
const refreshTokenExpiresIn time.Duration = 60 * 24 * time.Hour
type user struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Email string `json:"email"`
IsChirpyRed bool `json:"is_chirpy_red"`
}
func (cfg *apiConfig) handlerCreateUser(w http.ResponseWriter, r *http.Request) {
type parameters struct {
Password string `json:"password"`
Email string `json:"email"`
}
type response struct {
user
}
decoder := json.NewDecoder(r.Body)
var params parameters
if err := decoder.Decode(¶ms); err != nil {
log.Printf("Error parsing JSON parameters: %s", err)
respondWithError(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
return
}
hashedPassword, err := auth.HashPassword(params.Password)
if err != nil {
log.Printf("Error hashing the password: %s", err)
respondWithError(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
return
}
newUser, err := cfg.dbQueries.CreateUser(r.Context(), database.CreateUserParams{
HashedPassword: hashedPassword,
Email: params.Email,
})
if err != nil {
log.Printf("Error creating user: %s", err)
respondWithError(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
return
}
respondWithJson(w, http.StatusCreated, response{
user: user{
ID: newUser.ID,
CreatedAt: newUser.CreatedAt,
UpdatedAt: newUser.UpdatedAt,
Email: newUser.Email,
IsChirpyRed: newUser.IsChirpyRed,
},
})
}
func (cfg *apiConfig) handlerUpdateUser(w http.ResponseWriter, r *http.Request) {
type parameters struct {
Password string `json:"password"`
Email string `json:"email"`
}
type response struct {
user
}
bearer, err := auth.GetBearerToken(r.Header)
if err != nil {
log.Printf("Unable to fetch bearer token: %s", err)
respondWithError(w, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
return
}
decoder := json.NewDecoder(r.Body)
var params parameters
if err := decoder.Decode(¶ms); err != nil {
log.Printf("Error parsing JSON parameters: %s", err)
respondWithError(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
return
}
userId, err := auth.ValidateJWT(bearer, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
return
}
hashedPassword, err := auth.HashPassword(params.Password)
if err != nil {
log.Printf("Unable to hash password: %s", err)
respondWithError(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
return
}
dbUser, err := cfg.dbQueries.UpdateUser(r.Context(), database.UpdateUserParams{
ID: userId,
Email: params.Email,
HashedPassword: hashedPassword,
})
if err != nil {
log.Printf("Unable to update user: %s", err)
respondWithError(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
return
}
respondWithJson(w, http.StatusOK, response{
user: user{
ID: dbUser.ID,
CreatedAt: dbUser.CreatedAt,
UpdatedAt: dbUser.UpdatedAt,
Email: dbUser.Email,
IsChirpyRed: dbUser.IsChirpyRed,
},
})
}