account auto
This commit is contained in:
1
go.mod
1
go.mod
@@ -7,7 +7,6 @@ require (
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/gorilla/mux v1.8.0
|
||||
github.com/gorilla/websocket v1.5.0
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
github.com/mattn/go-sqlite3 v1.14.16
|
||||
github.com/russross/blackfriday v1.6.0
|
||||
)
|
||||
|
5
go.sum
5
go.sum
@@ -1,4 +1,3 @@
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
@@ -7,10 +6,6 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
|
||||
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
|
||||
|
51
models/account.go
Normal file
51
models/account.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
SessionID string `json:"session_id"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
func AccountRead(w http.ResponseWriter, r *http.Request, cb func(account *Account)) {
|
||||
// 從Cookie中獲取session_id
|
||||
cookie, err := r.Cookie("session_id")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - 未登錄"))
|
||||
return
|
||||
}
|
||||
|
||||
// 獲取當前session
|
||||
session := Session{ID: cookie.Value}
|
||||
if err := session.Get(); err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - 會話已過期"))
|
||||
return
|
||||
}
|
||||
|
||||
// 獲取當前用戶
|
||||
user := User{ID: session.UserID}
|
||||
if err := user.Get(); err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("401 - 用戶不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
var account Account
|
||||
account.ID = user.ID
|
||||
account.Name = user.Name
|
||||
account.Email = user.Email
|
||||
account.SessionID = session.ID
|
||||
account.CreatedAt = user.CreatedAt
|
||||
account.UpdatedAt = user.UpdatedAt
|
||||
|
||||
cb(&account)
|
||||
|
||||
}
|
@@ -43,3 +43,25 @@ func AccountGet(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(account))
|
||||
}
|
||||
|
||||
// 獲取當前賬戶, 並將其傳入回調函數
|
||||
func get_account(w http.ResponseWriter, r *http.Request, callback func(*models.User)) (err error) {
|
||||
// 獲取Cookie
|
||||
cookie, err := r.Cookie("session_id")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 獲取會話
|
||||
session := models.Session{ID: cookie.Value}
|
||||
session.Get()
|
||||
|
||||
// 獲取用戶
|
||||
user := models.User{ID: session.UserID}
|
||||
user.Get()
|
||||
|
||||
callback(&user)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"main/models"
|
||||
@@ -26,6 +27,11 @@ func ModelsGet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func ModelsPost(w http.ResponseWriter, r *http.Request) {
|
||||
// 取得用戶
|
||||
models.AccountRead(w, r, func(account *models.Account) {
|
||||
fmt.Println(account)
|
||||
// TODO: 判斷權限(是否可以創建)
|
||||
// 創建模型
|
||||
var model models.Model
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -40,6 +46,7 @@ func ModelsPost(w http.ResponseWriter, r *http.Request) {
|
||||
model.Create()
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(model))
|
||||
})
|
||||
}
|
||||
|
||||
func ModelItemGet(w http.ResponseWriter, r *http.Request) {
|
||||
|
@@ -119,7 +119,7 @@ func SessionsItemDelete(w http.ResponseWriter, r *http.Request) {
|
||||
user := models.User{ID: session.UserID}
|
||||
user.Get()
|
||||
|
||||
sessionx := models.Session{ID: mux.Vars(r)["id"]}
|
||||
sessionx := models.Session{ID: mux.Vars(r)["session_id"]}
|
||||
sessionx.Get()
|
||||
|
||||
if user.ID != sessionx.UserID {
|
||||
|
Reference in New Issue
Block a user