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-10 13:41:17 +08:00
|
|
|
session, _ := SessionStore.Get(r, "session")
|
|
|
|
|
|
|
|
session.Values["user_id"] = user.Id
|
|
|
|
session.Save(r, w)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-10 13:41:17 +08:00
|
|
|
session, _ := SessionStore.Get(r, "session")
|
|
|
|
|
|
|
|
session.Values["user_id"] = user.Id
|
|
|
|
session.Save(r, w)
|
2021-12-08 23:43:14 +08:00
|
|
|
|
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) {
|
2021-12-10 13:41:17 +08:00
|
|
|
session, _ := SessionStore.Get(r, "session")
|
|
|
|
|
|
|
|
session.Values["user_id"] = ""
|
|
|
|
session.Save(r, w)
|
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")
|
|
|
|
}
|