sign in
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"main/models"
|
||||
"main/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@@ -19,17 +23,68 @@ func SessionsGet(w http.ResponseWriter, r *http.Request) {
|
||||
listview.WriteJSON(w)
|
||||
}
|
||||
|
||||
func GetForm(r *http.Request) (form []interface{}) {
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
if err = json.Unmarshal(body, &form); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 創建會話
|
||||
func SessionsPost(w http.ResponseWriter, r *http.Request) {
|
||||
var session models.Session
|
||||
var form struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
if err = json.Unmarshal(body, &form); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 使用Email獲取用戶
|
||||
user, err := models.GetUserByEmail(form.Email)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("404 - User Not Found"))
|
||||
return
|
||||
}
|
||||
|
||||
// 使用密碼驗證登錄
|
||||
if !user.CheckPassword(form.Password) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - Unauthorized"))
|
||||
return
|
||||
}
|
||||
|
||||
// 創建會話(生成一個不重複的 uuid 作爲 sid)
|
||||
session := &models.Session{ID: uuid.New().String(), UserID: user.ID}
|
||||
session.Create()
|
||||
|
||||
// 寫入Cookie
|
||||
cookie := http.Cookie{Name: "session_id", Value: session.ID, Path: "/", HttpOnly: true}
|
||||
http.SetCookie(w, &cookie)
|
||||
|
||||
// 返回信息
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(session))
|
||||
}
|
||||
|
||||
// 獲取會話
|
||||
func SessionsItemGet(w http.ResponseWriter, r *http.Request) {
|
||||
session := models.Session{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
|
||||
session := models.Session{ID: mux.Vars(r)["id"]}
|
||||
session.Get()
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(session))
|
||||
@@ -37,7 +92,7 @@ func SessionsItemGet(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 更新會話
|
||||
func SessionsItemPatch(w http.ResponseWriter, r *http.Request) {
|
||||
session := models.Session{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
|
||||
session := models.Session{ID: mux.Vars(r)["id"]}
|
||||
session.Get()
|
||||
session.Update()
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
@@ -46,7 +101,7 @@ func SessionsItemPatch(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 刪除會話
|
||||
func SessionsItemDelete(w http.ResponseWriter, r *http.Request) {
|
||||
session := models.Session{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
|
||||
session := models.Session{ID: mux.Vars(r)["id"]}
|
||||
session.Get()
|
||||
session.Delete()
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
Reference in New Issue
Block a user