-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_handler.go
105 lines (96 loc) · 3.37 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
package controller
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/google/uuid"
"github.com/itishrishikesh/gofeed/auth"
"github.com/itishrishikesh/gofeed/constants"
"github.com/itishrishikesh/gofeed/internal/database"
"github.com/itishrishikesh/gofeed/models"
"github.com/itishrishikesh/gofeed/utils"
)
func (apiCfg *ApiConfig) CreateUserHandler(writer http.ResponseWriter, request *http.Request) {
type parameters struct {
Username string `json:"username"`
Password string `json:"password"`
}
params := parameters{}
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(¶ms)
if err != nil {
utils.RespondWithError(writer, constants.HTTP_BAD_REQUEST, "Error parsing JSON")
log.Println("E#1PDKJ2 - User passed incorrect JSON")
return
}
user, err := apiCfg.DB.CreateUser(request.Context(), database.CreateUserParams{
ID: uuid.New(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Name: params.Username,
Password: utils.HashPassword(params.Password),
})
if err != nil {
utils.RespondWithError(writer, constants.HTTP_ERROR, "Couldn't create user")
log.Println(fmt.Sprintf("E#1ORI9D - Couldn't create user %v", err))
return
}
utils.RespondWithJSON(writer, constants.HTTP_SUCCESS, models.DatabaseUserToUser(user))
}
func (apiCfg *ApiConfig) GetUserHandler(writer http.ResponseWriter, request *http.Request, user models.User) {
utils.RespondWithJSON(writer, constants.HTTP_SUCCESS, user)
}
func (config *ApiConfig) GetPostsForUserHandler(writer http.ResponseWriter, request *http.Request, user models.User) {
posts, err := config.DB.GetPostsForUser(request.Context(), database.GetPostsForUserParams{
UserID: user.ID,
Limit: 10,
})
if err != nil {
utils.RespondWithError(writer, constants.HTTP_ERROR, "Couldn't get posts")
log.Println(fmt.Sprintf("E#1OWWJW - Couldn't get posts %v", err))
return
}
utils.RespondWithJSON(writer, constants.HTTP_SUCCESS, models.DatabasePostsToPosts(posts))
}
func (config *ApiConfig) TokenHandler(writer http.ResponseWriter, request *http.Request) {
type parameters struct {
Username string `json:"username"`
Password string `json:"password"`
}
params := parameters{}
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(¶ms)
if err != nil {
utils.RespondWithError(writer, constants.HTTP_BAD_REQUEST, "Error parsing JSON")
log.Println("E#1PFLY0 - Something's wrong with token", err)
return
}
user, err := config.DB.GetUserByUsername(context.Background(), params.Username)
log.Println("D#1PFMM0 - Received user is", user)
if err != nil {
utils.RespondWithError(writer, constants.HTTP_UNAUTHORIZED, "Invalid Username or Password")
log.Println("E#1PFM6A - Invalid Username or Password. E:", err)
return
}
if !utils.CompareHashAndPassword(user.Password, params.Password) {
utils.RespondWithError(writer, constants.HTTP_UNAUTHORIZED, "Invalid Username or Password")
log.Println("E#1PHB77 - Invalid Username or Password. E:", err)
return
}
token, err := auth.GenerateJWT(user.Name)
if err != nil {
utils.RespondWithError(writer, constants.HTTP_ERROR, "Error while generating token")
log.Println(fmt.Sprintf("E#1PFKYV - Error while generating JWT Token %v", err))
return
}
type returnToken struct {
Token string `json:"token"`
}
payload := returnToken{
Token: token,
}
utils.RespondWithJSON(writer, constants.HTTP_SUCCESS, payload)
}