TODO: config load
This commit is contained in:
parent
6617b483cf
commit
250a40f83e
55
main.go
55
main.go
|
@ -10,50 +10,77 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
// SessionKey Session主Key
|
||||
SessionKey = "token"
|
||||
// SessionUser 用户登录的Session标签
|
||||
SessionUser = "token"
|
||||
SessionUser = "user"
|
||||
)
|
||||
|
||||
func auth(ctx *gin.Context) {
|
||||
|
||||
if ctx.Request.RequestURI != "/api/login" {
|
||||
|
||||
session := sessions.Default(ctx)
|
||||
|
||||
if token := session.Get(SessionUser); token == nil {
|
||||
ctx.Redirect(http.StatusNotModified, "/api/login")
|
||||
session.Clear()
|
||||
session.Save()
|
||||
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"message": "需要登录"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
func login(ctx *gin.Context) {
|
||||
ctx.Request.ParseForm()
|
||||
user := ctx.PostForm("user")
|
||||
session := sessions.Default(ctx)
|
||||
|
||||
if user == "" {
|
||||
|
||||
if tokenUser := session.Get(SessionUser); tokenUser != nil {
|
||||
ctx.JSON(http.StatusOK, gin.H{"user": tokenUser})
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if realPassword, ok := GlobalConfig.GetUser(user); ok {
|
||||
|
||||
pwd := ctx.PostForm("pwd")
|
||||
if realPassword == pwd {
|
||||
session := sessions.Default(ctx)
|
||||
session.Set(SessionUser, user)
|
||||
session.Save()
|
||||
} else {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "密码错误"})
|
||||
ctx.JSON(http.StatusOK, gin.H{"message": "登录成功"})
|
||||
|
||||
return
|
||||
}
|
||||
} else {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "不存在该用户"})
|
||||
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "密码错误"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"message": "登录成功"})
|
||||
// ctx.Redirect(http.StatusOK, "/worker")
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "不存在该用户"})
|
||||
return
|
||||
|
||||
// ctx.Redirect(http.StatusOK, "/worker")
|
||||
// ctx.Next()
|
||||
}
|
||||
|
||||
func logout(ctx *gin.Context) {
|
||||
session := sessions.Default(ctx)
|
||||
session.Clear()
|
||||
session.Save()
|
||||
}
|
||||
|
||||
func userConfig(ctx *gin.Context) {
|
||||
ctx.Request.ParseForm()
|
||||
|
||||
session := sessions.Default(ctx)
|
||||
if session.Get(SessionUser) == nil {
|
||||
user := session.Get(SessionUser)
|
||||
// log.Println(user)
|
||||
if user == nil {
|
||||
ctx.JSON(http.StatusUnauthorized, gin.H{"error": "权限错误"})
|
||||
return
|
||||
}
|
||||
|
@ -64,10 +91,12 @@ func main() {
|
|||
|
||||
eg := gin.New()
|
||||
|
||||
eg.Use(sessions.Sessions(SessionUser, cookie.NewStore([]byte("yame"))))
|
||||
eg.Use(sessions.Sessions(SessionKey, cookie.NewStore([]byte("yame"))))
|
||||
eg.Use(auth)
|
||||
|
||||
eg.POST("/api/login", login)
|
||||
eg.POST("/api/user/config", userConfig)
|
||||
eg.POST("/api/user/logout", logout)
|
||||
|
||||
log.Fatal(eg.Run(":3001"))
|
||||
}
|
||||
|
|
13
main_test.go
13
main_test.go
|
@ -1,20 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/cookie"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
eg := gin.New()
|
||||
|
||||
eg.Use(sessions.Sessions(SessionUser, cookie.NewStore([]byte("yame"))))
|
||||
eg.Use(auth)
|
||||
|
||||
eg.POST("/api/login", login)
|
||||
log.Fatal(eg.Run(":3001"))
|
||||
main()
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import axios from 'axios';
|
||||
import { Form, Select, InputNumber, DatePicker, Switch, Slider, Button } from 'antd';
|
||||
import './App.css';
|
||||
|
@ -9,9 +10,21 @@ import Login from './login';
|
|||
|
||||
const { Option } = Select;
|
||||
|
||||
// 响应拦截
|
||||
axios.interceptors.response.use((response) => {
|
||||
return response
|
||||
}, (err) => {
|
||||
if(err.response.status === 401){
|
||||
ReactDOM.render(<App></App>, document.getElementById('root'));
|
||||
}
|
||||
return Promise.reject(err)
|
||||
})
|
||||
|
||||
|
||||
const App = () => (
|
||||
|
||||
<SiderConfig></SiderConfig>
|
||||
<Login isAutoLogin={true}></Login>
|
||||
// <SiderConfig></SiderConfig>
|
||||
|
||||
);
|
||||
|
||||
|
|
|
@ -10,6 +10,21 @@ import SiderConfig from './siderConfig';
|
|||
|
||||
class Login extends React.Component {
|
||||
|
||||
componentWillMount() {
|
||||
|
||||
if(this.props.isAutoLogin) {
|
||||
|
||||
axios.post("/api/login", new FormData()).then(loginInfo => {
|
||||
axios.post("/api/user/config", new FormData()).then( value => {
|
||||
console.log(value);
|
||||
ReactDom.render(<SiderConfig userName={loginInfo.data["user"]}></SiderConfig>, document.getElementById('root'))
|
||||
} )
|
||||
|
||||
return
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onClick = (e) => {
|
||||
var user = this.refs["login-user"].input.value
|
||||
var pwd = this.refs["login-passwd"].input.value
|
||||
|
@ -22,20 +37,19 @@ class Login extends React.Component {
|
|||
console.log("Cookies are ", document.cookie)
|
||||
ReactDom.render(<SiderConfig userName={user}></SiderConfig>, document.getElementById('root'))
|
||||
return
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Layout style={{ minHeight: "100vh" }}>
|
||||
<Layout onPlay={this.onLoad} style={{ minHeight: "100vh" }}>
|
||||
|
||||
<div style={{ marginTop: "30vh" }}>
|
||||
<Row className="login-input" type="flex" justify="center"><Title>Apollo Local</Title></Row>
|
||||
|
||||
|
||||
<Row className="login-input" type="flex" justify="center">
|
||||
<Input ref="login-user" placeholder="用户" style={{ width: "50vh" }}
|
||||
<Input ref="login-user" placeholder="用户" style={{ width: "50vh" }}
|
||||
prefix={ <Tooltip title="用户名"> <Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} /> </Tooltip>}
|
||||
suffix={
|
||||
<Icon type="info-circle" style={{ color: 'rgba(0,0,0,.45)' }} />
|
||||
|
|
|
@ -21,8 +21,8 @@ class NamespaceSelect extends React.Component {
|
|||
onDropdownVisibleChange = (open) => {
|
||||
if(open) {
|
||||
var resp = axios.post("/api/user/config")
|
||||
console.log(resp)
|
||||
|
||||
console.log(resp)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
import React from 'react';
|
||||
import ReactDom from 'react-dom';
|
||||
import axios from 'axios';
|
||||
import { Layout, Menu, Breadcrumb, Icon, Button, Row, Col, Tag } from 'antd';
|
||||
import { Input } from 'antd';
|
||||
import NamespaceSelect from './namespaceSelect';
|
||||
import Text from 'antd/lib/typography/Text';
|
||||
import Login from './login';
|
||||
import App from './App';
|
||||
|
||||
const { TextArea } = Input;
|
||||
const { Header, Content, Footer, Sider } = Layout;
|
||||
|
@ -18,6 +22,15 @@ class SiderConfig extends React.Component {
|
|||
this.setState({ collapsed });
|
||||
};
|
||||
|
||||
onClickLogout = e => {
|
||||
|
||||
axios.post("/api/user/logout", new FormData()).then(loginInfo => {
|
||||
ReactDom.render(<Login isAutoLogin={false} ></Login>, document.getElementById('root'));
|
||||
return;
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Layout style={{ minHeight: '100vh' }}>
|
||||
|
@ -32,11 +45,9 @@ class SiderConfig extends React.Component {
|
|||
<span>
|
||||
<Icon type="team" />
|
||||
Namespace
|
||||
</span>
|
||||
</span>
|
||||
}>
|
||||
<NamespaceSelect>
|
||||
|
||||
</NamespaceSelect>
|
||||
<NamespaceSelect/>
|
||||
</SubMenu>
|
||||
|
||||
|
||||
|
@ -49,7 +60,7 @@ class SiderConfig extends React.Component {
|
|||
<Col>
|
||||
<Text style={{ alignSelf: "flex-end", fontSize: 16, color: "#f37b1d" }}>用户:</Text>
|
||||
<Text style={{ fontSize: 16 }}>{this.props.userName}</Text>
|
||||
<Button type="primary" style={{ margin: "0px 10px 0 20px", width: '100px', }}>退出</Button>
|
||||
<Button onClick={this.onClickLogout} type="primary" style={{ margin: "0px 10px 0 20px", width: '100px', }}>退出</Button>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user