-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollers.go
More file actions
142 lines (119 loc) · 3.17 KB
/
Copy pathcontrollers.go
File metadata and controls
142 lines (119 loc) · 3.17 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"
"log"
"net/http"
"time"
"github.com/labstack/echo/v4"
)
func Signup(c echo.Context) error {
var user User
err := json.NewDecoder(c.Request().Body).Decode(&user)
if err != nil {
c.String(400, "Invalid request body")
}
// check if phone number and password are valid
if len(user.PhoneNumber) != 11 {
return c.String(400, "Invalid phone number. Phone number must be 11 digits")
}
if len(user.Password) < 8 || len(user.Password) > 64 {
return c.String(400, "Invalid password. Password must be between 8 and 64 characters")
}
// check if user already exists
exists, err := redisClient.KeyExists(user.PhoneNumber)
if err != nil {
log.Println(err)
return c.String(500, "Failed to create user")
}
if exists {
return c.String(409, "Phone number already exists")
}
// set user role
user.Role = "user"
// create user
_, err = redisClient.CreateUser(user)
if err != nil {
log.Println(err)
return c.String(500, "Failed to create user")
}
// create JWT
jwt, err := CreateJWT(user, config.JWTSecret)
if err != nil {
log.Println(err)
return c.String(500, "Failed to create jwt")
}
c.SetCookie(&http.Cookie{Name: "jwt", Value: jwt, Path: "/",
// Secure: true,
HttpOnly: true,
Expires: time.Now().Add(time.Hour * 24),
Domain: config.Domain,
})
return c.NoContent(201)
}
func Signin(c echo.Context) error {
var user User
err := json.NewDecoder(c.Request().Body).Decode(&user)
if err != nil {
c.String(400, "Invalid request body")
}
// check if phone number and password are valid
if len(user.PhoneNumber) != 11 {
return c.String(400, "Invalid phone number. Phone number must be 11 digits")
}
if len(user.Password) < 8 || len(user.Password) > 64 {
return c.String(400, "Invalid password. Password must be between 8 and 64 characters")
}
// find user
u, err := redisClient.FindUser(user.PhoneNumber)
if err != nil {
if err.Error() == "user not found" {
return c.String(400, "Wrong phone number or password")
} else {
log.Println(err)
return c.String(500, "Failed to find user")
}
}
// check password
if !CheckPasswordHash(user.Password, u.Password) {
return c.String(400, "Wrong phone number or password")
}
// create JWT
jwt, err := CreateJWT(*u, config.JWTSecret)
if err != nil {
log.Println(err)
return c.String(500, "Failed to create jwt")
}
// set JWT cookie
c.SetCookie(&http.Cookie{Name: "jwt", Value: jwt, Path: "/",
// Secure: true,
HttpOnly: true,
Expires: time.Now().Add(time.Hour * 24),
Domain: config.Domain,
})
return c.NoContent(200)
}
func Signout(c echo.Context) error {
// set JWT cookie
c.SetCookie(&http.Cookie{Name: "jwt", Value: "nil", Path: "/",
// Secure: true,
HttpOnly: true,
MaxAge: -1,
Domain: config.Domain,
})
return c.NoContent(200)
}
func Account(c echo.Context) error {
phoneNumber := c.Get("user").(*User).PhoneNumber
// find user
u, err := redisClient.FindUser(phoneNumber)
if err != nil {
if err.Error() == "user not found" {
return c.String(400, "Wrong phone number or password")
} else {
log.Println(err)
return c.String(500, "Failed to find user")
}
}
u.Password = ""
return c.JSON(200, u)
}