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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserSignUp struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleUserSignUp(w http.ResponseWriter, r *http.Request) {
|
2021-12-09 22:02:57 +08:00
|
|
|
userSignup := UserSignUp{}
|
2021-12-08 23:43:14 +08:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&userSignup)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := store.CreateNewUser(userSignup.Username, userSignup.Password, "", "")
|
|
|
|
|
|
|
|
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
|
|
|
userIdCookie := &http.Cookie{
|
|
|
|
Name: "user_id",
|
|
|
|
Value: user.Id,
|
|
|
|
Path: "/",
|
|
|
|
MaxAge: 3600 * 24 * 30,
|
|
|
|
}
|
|
|
|
http.SetCookie(w, userIdCookie)
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(Response{
|
|
|
|
Succeed: true,
|
|
|
|
Message: "",
|
|
|
|
Data: user,
|
|
|
|
})
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserSignin struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleUserSignIn(w http.ResponseWriter, r *http.Request) {
|
2021-12-09 22:02:57 +08:00
|
|
|
userSignin := UserSignin{}
|
2021-12-08 23:43:14 +08:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&userSignin)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := store.GetUserByUsernameAndPassword(userSignin.Username, userSignin.Password)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
userIdCookie := &http.Cookie{
|
|
|
|
Name: "user_id",
|
|
|
|
Value: user.Id,
|
2021-12-09 22:02:57 +08:00
|
|
|
Path: "/",
|
2021-12-08 23:43:14 +08:00
|
|
|
MaxAge: 3600 * 24 * 30,
|
|
|
|
}
|
|
|
|
http.SetCookie(w, userIdCookie)
|
|
|
|
|
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 handleUserSignOut(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userIdCookie := &http.Cookie{
|
|
|
|
Name: "user_id",
|
|
|
|
Value: "",
|
2021-12-09 22:02:57 +08:00
|
|
|
Path: "/",
|
2021-12-08 23:43:14 +08:00
|
|
|
MaxAge: 0,
|
|
|
|
}
|
|
|
|
http.SetCookie(w, userIdCookie)
|
2021-12-09 22:02:57 +08:00
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(Response{
|
|
|
|
Succeed: true,
|
|
|
|
Message: "",
|
|
|
|
Data: nil,
|
|
|
|
})
|
2021-12-08 23:43:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterAuthRoutes(r *mux.Router) {
|
|
|
|
authRouter := r.PathPrefix("/api/auth").Subrouter()
|
|
|
|
|
|
|
|
authRouter.HandleFunc("/signup", handleUserSignUp).Methods("POST")
|
|
|
|
authRouter.HandleFunc("/signin", handleUserSignIn).Methods("POST")
|
|
|
|
authRouter.HandleFunc("/signout", handleUserSignOut).Methods("POST")
|
|
|
|
}
|