memos/api/user.go

150 lines
3.2 KiB
Go
Raw Normal View History

2021-12-08 23:43:14 +08:00
package api
import (
"encoding/json"
2021-12-09 22:02:57 +08:00
"memos/api/e"
2021-12-08 23:43:14 +08:00
"memos/store"
"net/http"
"github.com/gorilla/mux"
)
func handleGetMyUserInfo(w http.ResponseWriter, r *http.Request) {
2021-12-10 13:41:17 +08:00
userId, _ := GetUserIdInSession(r)
2021-12-08 23:43:14 +08:00
user, err := store.GetUserById(userId)
if err != nil {
2021-12-15 10:55:17 +08:00
e.ErrorHandler(w, "USER_NOT_FOUND", err.Error())
2021-12-08 23:43:14 +08:00
return
}
2021-12-09 22:02:57 +08:00
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: user,
})
2021-12-08 23:43:14 +08:00
}
func handleUpdateMyUserInfo(w http.ResponseWriter, r *http.Request) {
2021-12-10 13:41:17 +08:00
userId, _ := GetUserIdInSession(r)
2021-12-08 23:43:14 +08:00
2022-01-09 11:44:12 +08:00
updateUserPatch := store.UpdateUserPatch{}
err := json.NewDecoder(r.Body).Decode(&updateUserPatch)
2021-12-09 22:02:57 +08:00
if err != nil {
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Bad request")
return
}
2022-01-09 11:44:12 +08:00
if updateUserPatch.Username != nil {
usernameUsable, _ := store.CheckUsernameUsable(*updateUserPatch.Username)
2021-12-12 14:00:25 +08:00
if !usernameUsable {
json.NewEncoder(w).Encode(Response{
Succeed: false,
Message: "Username is existed",
Data: nil,
})
return
}
}
2022-01-09 11:44:12 +08:00
user, err := store.UpdateUser(userId, &updateUserPatch)
2021-12-09 22:02:57 +08:00
if err != nil {
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
return
}
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: user,
})
}
2022-01-09 11:44:12 +08:00
func handleResetUserOpenId(w http.ResponseWriter, r *http.Request) {
2021-12-14 20:40:24 +08:00
userId, _ := GetUserIdInSession(r)
2022-01-09 11:44:12 +08:00
openId, err := store.ResetUserOpenId(userId)
2021-12-14 20:40:24 +08:00
if err != nil {
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
return
}
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: openId,
})
}
2021-12-09 22:02:57 +08:00
func handleCheckUsername(w http.ResponseWriter, r *http.Request) {
2021-12-15 10:55:17 +08:00
type CheckUsernameDataBody struct {
Username string
}
checkUsername := CheckUsernameDataBody{}
2021-12-09 22:02:57 +08:00
err := json.NewDecoder(r.Body).Decode(&checkUsername)
if err != nil {
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Bad request")
return
}
usable, err := store.CheckUsernameUsable(checkUsername.Username)
if err != nil {
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
return
}
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: usable,
})
}
func handleValidPassword(w http.ResponseWriter, r *http.Request) {
2021-12-15 10:55:17 +08:00
type ValidPasswordDataBody struct {
Password string
}
2021-12-10 13:41:17 +08:00
userId, _ := GetUserIdInSession(r)
2021-12-15 10:55:17 +08:00
validPassword := ValidPasswordDataBody{}
2021-12-09 22:02:57 +08:00
err := json.NewDecoder(r.Body).Decode(&validPassword)
2021-12-08 23:43:14 +08:00
if err != nil {
2021-12-09 22:02:57 +08:00
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Bad request")
2021-12-08 23:43:14 +08:00
return
}
2021-12-09 22:02:57 +08:00
valid, err := store.CheckPasswordValid(userId, validPassword.Password)
2021-12-08 23:43:14 +08:00
if err != nil {
2021-12-09 22:02:57 +08:00
e.ErrorHandler(w, "DATABASE_ERROR", err.Error())
2021-12-08 23:43:14 +08:00
return
}
2021-12-09 22:02:57 +08:00
json.NewEncoder(w).Encode(Response{
Succeed: true,
Message: "",
Data: valid,
})
2021-12-08 23:43:14 +08:00
}
func RegisterUserRoutes(r *mux.Router) {
userRouter := r.PathPrefix("/api/user").Subrouter()
2021-12-12 21:49:46 +08:00
userRouter.Use(JSONResponseMiddleWare)
2021-12-08 23:43:14 +08:00
userRouter.Use(AuthCheckerMiddleWare)
userRouter.HandleFunc("/me", handleGetMyUserInfo).Methods("GET")
userRouter.HandleFunc("/me", handleUpdateMyUserInfo).Methods("PATCH")
2022-01-09 11:44:12 +08:00
userRouter.HandleFunc("/open_id/new", handleResetUserOpenId).Methods("POST")
2021-12-09 22:02:57 +08:00
userRouter.HandleFunc("/checkusername", handleCheckUsername).Methods("POST")
userRouter.HandleFunc("/validpassword", handleValidPassword).Methods("POST")
2021-12-08 23:43:14 +08:00
}