增加从环境变量获取配置&修复修改密码失败的bug&增加退出登录&修复新增用户无法登录的bug

This commit is contained in:
dushixiang
2020-12-26 23:56:02 +08:00
parent 712e2cfe84
commit 31e18d0418
24 changed files with 195 additions and 53 deletions

View File

@ -13,6 +13,11 @@ type LoginAccount struct {
Password string `json:"password"`
}
type ChangePassword struct {
NewPassword string `json:"newPassword"`
OldPassword string `json:"oldPassword"`
}
func LoginEndpoint(c echo.Context) error {
var loginAccount LoginAccount
if err := c.Bind(&loginAccount); err != nil {
@ -21,10 +26,10 @@ func LoginEndpoint(c echo.Context) error {
user, err := model.FindUserByUsername(loginAccount.Username)
if err != nil {
return err
return Fail(c, -1, "您输入的账号或密码不正确")
}
if err := utils.Encoder.Match([]byte(user.Password), []byte(loginAccount.Password)); err != nil {
return err
return Fail(c, -1, "您输入的账号或密码不正确")
}
token := utils.UUID()
@ -43,7 +48,28 @@ func LogoutEndpoint(c echo.Context) error {
}
func ChangePasswordEndpoint(c echo.Context) error {
return nil
account, _ := GetCurrentAccount(c)
var changePassword ChangePassword
if err := c.Bind(&changePassword); err != nil {
return err
}
if err := utils.Encoder.Match([]byte(account.Password), []byte(changePassword.OldPassword)); err != nil {
return Fail(c, -1, "您输入的原密码不正确")
}
passwd, err := utils.Encoder.Encode([]byte(changePassword.NewPassword))
if err != nil {
return err
}
u := &model.User{
Password: string(passwd),
}
model.UpdateUserById(u, account.ID)
return LogoutEndpoint(c)
}
func InfoEndpoint(c echo.Context) error {

View File

@ -7,6 +7,16 @@ import (
"time"
)
func ErrorHandler(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if err := next(c); err != nil {
return Fail(c, 0, err.Error())
}
return nil
}
}
func Auth(next echo.HandlerFunc) echo.HandlerFunc {
urls := []string{"download", "recording", "login", "static", "favicon", "logo"}

View File

@ -36,6 +36,7 @@ func SetupRoutes() *echo.Echo {
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}))
e.Use(ErrorHandler)
e.Use(Auth)
e.POST("/login", LoginEndpoint)

View File

@ -16,7 +16,7 @@ func UserCreateEndpoint(c echo.Context) error {
var pass []byte
var err error
if pass, err = utils.Encoder.Encode([]byte("admin")); err != nil {
if pass, err = utils.Encoder.Encode([]byte(item.Password)); err != nil {
return err
}
item.Password = string(pass)

View File

@ -1,16 +1,16 @@
package config
import (
"log"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type Config struct {
DB string
Server *Server
Mysql *Mysql
Sqlite *Sqlite
}
type Mysql struct {
@ -21,36 +21,31 @@ type Mysql struct {
Database string
}
type Sqlite struct {
File string
}
type Server struct {
Addr string
}
func SetupConfig() *Config {
func SetupConfig() (*Config, error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.SetConfigType("yml")
viper.AddConfigPath("/etc/next-terminal/")
viper.AddConfigPath("$HOME/.next-terminal")
viper.AddConfigPath(".")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
//pflag.String("mysql.hostname", "127.0.0.1", "mysql hostname")
//pflag.Int("mysql.port", 3306, "mysql port")
//pflag.String("mysql.username", "mysql", "mysql username")
//pflag.String("mysql.password", "mysql", "mysql password")
//pflag.String("mysql.database", "next_terminal", "mysql database")
//pflag.String("server.addr", "0.0.0.0:8088", "server listen addr")
pflag.Parse()
_ = viper.BindPFlags(pflag.CommandLine)
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
return nil, err
}
var config = &Config{
DB: viper.GetString("db"),
Mysql: &Mysql{
Hostname: viper.GetString("mysql.hostname"),
Port: viper.GetInt("mysql.port"),
@ -58,10 +53,13 @@ func SetupConfig() *Config {
Password: viper.GetString("mysql.password"),
Database: viper.GetString("mysql.database"),
},
Sqlite: &Sqlite{
File: viper.GetString("sqlite.file"),
},
Server: &Server{
Addr: viper.GetString("server.addr"),
},
}
return config
return config, nil
}