diff --git a/main.go b/main.go index fd06e1b..4bde70c 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ import ( "time" ) -const Version = "v0.0.9" +const Version = "v0.1.0" func main() { log.Fatal(Run()) diff --git a/pkg/api/account.go b/pkg/api/account.go index 900d3e5..a045f08 100644 --- a/pkg/api/account.go +++ b/pkg/api/account.go @@ -156,7 +156,7 @@ func ConfirmTOTPEndpoint(c echo.Context) error { return Success(c, nil) } -func ResetTOTPEndpoint(c echo.Context) error { +func ReloadTOTPEndpoint(c echo.Context) error { account, _ := GetCurrentAccount(c) key, err := totp.NewTOTP(totp.GenerateOpts{ @@ -183,6 +183,15 @@ func ResetTOTPEndpoint(c echo.Context) error { }) } +func ResetTOTPEndpoint(c echo.Context) error { + account, _ := GetCurrentAccount(c) + u := &model.User{ + TOTPSecret: "-", + } + model.UpdateUserById(u, account.ID) + return Success(c, "") +} + func ChangePasswordEndpoint(c echo.Context) error { account, _ := GetCurrentAccount(c) @@ -208,7 +217,28 @@ func ChangePasswordEndpoint(c echo.Context) error { return LogoutEndpoint(c) } +type AccountInfo struct { + Id string `json:"id"` + Username string `json:"username"` + Nickname string `json:"nickname"` + Type string `json:"type"` + EnableTotp bool `json:"enableTotp"` +} + func InfoEndpoint(c echo.Context) error { account, _ := GetCurrentAccount(c) - return Success(c, account) + + user, err := model.FindUserById(account.ID) + if err != nil { + return err + } + + info := AccountInfo{ + Id: user.ID, + Username: user.Username, + Nickname: user.Nickname, + Type: user.Type, + EnableTotp: user.TOTPSecret != "" && user.TOTPSecret != "-", + } + return Success(c, info) } diff --git a/pkg/api/routes.go b/pkg/api/routes.go index bcf75f7..220f4ba 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -38,6 +38,7 @@ func SetupRoutes() *echo.Echo { e.POST("/logout", LogoutEndpoint) e.POST("/change-password", ChangePasswordEndpoint) + e.GET("/reload-totp", ReloadTOTPEndpoint) e.POST("/reset-totp", ResetTOTPEndpoint) e.POST("/confirm-totp", ConfirmTOTPEndpoint) e.GET("/info", InfoEndpoint) diff --git a/web/package.json b/web/package.json index bdf8fe7..4b9ad8a 100644 --- a/web/package.json +++ b/web/package.json @@ -1,6 +1,6 @@ { "name": "next-terminal", - "version": "0.0.9", + "version": "0.1.0", "private": true, "dependencies": { "@ant-design/icons": "^4.3.0", diff --git a/web/src/components/user/Info.js b/web/src/components/user/Info.js index 40114d5..ecf63b1 100644 --- a/web/src/components/user/Info.js +++ b/web/src/components/user/Info.js @@ -1,9 +1,10 @@ import React, {Component} from 'react'; -import {Button, Card, Form, Image, Input, Layout, PageHeader} from "antd"; +import {Button, Card, Form, Image, Input, Layout, Modal, PageHeader, Result, Space} from "antd"; import {itemRender} from '../../utils/utils' import request from "../../common/request"; import {message} from "antd/es"; import Logout from "./Logout"; +import {ExclamationCircleOutlined, ReloadOutlined} from "@ant-design/icons"; const {Content} = Layout; const {Meta} = Card; @@ -27,13 +28,34 @@ const formTailLayout = { labelCol: {span: 3}, wrapperCol: {span: 6, offset: 3}, }; +const {confirm} = Modal; class Info extends Component { - state = {} + state = { + user: { + enableTotp: false + } + } passwordFormRef = React.createRef(); + componentDidMount() { + this.loadInfo(); + } + + loadInfo = async () => { + let result = await request.get('/info'); + if (result['code'] === 1) { + this.setState({ + user: result['data'] + }) + sessionStorage.setItem('user', JSON.stringify(result['data'])); + } else { + message.error(result['message']); + } + } + onNewPasswordChange(value) { this.setState({ 'newPassword': value.target.value @@ -75,6 +97,7 @@ class Info extends Component { let result = await request.post('/confirm-totp', values); if (result.code === 1) { message.success('TOTP启用成功'); + await this.loadInfo(); this.setState({ qr: "", secret: "" @@ -85,7 +108,7 @@ class Info extends Component { } resetTOTP = async () => { - let result = await request.post('/reset-totp'); + let result = await request.get('/reload-totp'); if (result.code === 1) { this.setState({ qr: result.data.qr, @@ -167,33 +190,93 @@ class Info extends Component {

双因素认证

-