From d8f261382db7124a4448765b8c1e99960a666c2e Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Tue, 7 Jan 2020 04:25:45 +0800 Subject: [PATCH] =?UTF-8?q?TODO:=20=E6=9D=83=E9=99=90=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 26 +++++++++++++++++++++----- user.go | 15 +++++++++++++++ web/src/App.js | 2 +- web/src/login.js | 2 +- 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 user.go diff --git a/main.go b/main.go index e9fa68c..6e7084a 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "log" "net/http" + "time" "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/cookie" @@ -22,12 +23,19 @@ func auth(ctx *gin.Context) { session := sessions.Default(ctx) - if token := session.Get(SessionUser); token == nil { + if user := session.Get(SessionUser); user == nil { + session.Clear() session.Save() ctx.JSON(http.StatusUnauthorized, gin.H{"message": "需要登录"}) return + } else if user.(*User).Expired < time.Now().Unix() { + session.Clear() + session.Save() + + ctx.JSON(http.StatusUnauthorized, gin.H{"message": "账号过期"}) + return } } @@ -35,10 +43,10 @@ func auth(ctx *gin.Context) { } func login(ctx *gin.Context) { - user := ctx.PostForm("user") + userName := ctx.PostForm("user") session := sessions.Default(ctx) - if user == "" { + if userName == "" { if tokenUser := session.Get(SessionUser); tokenUser != nil { ctx.JSON(http.StatusOK, gin.H{"user": tokenUser}) @@ -47,10 +55,17 @@ func login(ctx *gin.Context) { } - if realPassword, ok := GlobalConfig.GetUser(user); ok { + if realPassword, ok := GlobalConfig.GetUser(userName); ok { pwd := ctx.PostForm("pwd") if realPassword == pwd { + + user := &User{Name: userName, + Expired: time.Now().Unix() + 15, + ConfigPath: "", + Config: nil, + } + session.Set(SessionUser, user) session.Save() ctx.JSON(http.StatusOK, gin.H{"message": "登录成功"}) @@ -73,6 +88,7 @@ func logout(ctx *gin.Context) { session := sessions.Default(ctx) session.Clear() session.Save() + ctx.JSON(http.StatusOK, gin.H{"message": "退出登录成功"}) } func userConfig(ctx *gin.Context) { @@ -84,7 +100,7 @@ func userConfig(ctx *gin.Context) { ctx.JSON(http.StatusUnauthorized, gin.H{"error": "权限错误"}) return } - ctx.JSON(http.StatusOK, gin.H{"message": "获取配置成功"}) + ctx.JSON(http.StatusOK, gin.H{"message": "获取配置成功", "user": user.(*User).Name}) } func main() { diff --git a/user.go b/user.go new file mode 100644 index 0000000..665258c --- /dev/null +++ b/user.go @@ -0,0 +1,15 @@ +package main + +import "encoding/gob" + +func init() { + gob.Register(&User{}) +} + +// User 用户结构 +type User struct { + Expired int64 + Name string + ConfigPath string + Config interface{} +} diff --git a/web/src/App.js b/web/src/App.js index e2848ff..959a6af 100755 --- a/web/src/App.js +++ b/web/src/App.js @@ -15,7 +15,7 @@ axios.interceptors.response.use((response) => { return response }, (err) => { if(err.response.status === 401){ - ReactDOM.render(, document.getElementById('root')); + ReactDOM.render(, document.getElementById('root')); } return Promise.reject(err) }) diff --git a/web/src/login.js b/web/src/login.js index 8b64ab1..d80f2ed 100644 --- a/web/src/login.js +++ b/web/src/login.js @@ -17,7 +17,7 @@ class Login extends React.Component { axios.post("/api/login", new FormData()).then(loginInfo => { axios.post("/api/user/config", new FormData()).then( value => { console.log(value); - ReactDom.render(, document.getElementById('root')) + ReactDom.render(, document.getElementById('root')) } ) return