diff --git a/models/users.go b/models/users.go index 967128c..6e146ff 100644 --- a/models/users.go +++ b/models/users.go @@ -1,8 +1,10 @@ package models import ( + "fmt" "megrez/libs/crypto" "megrez/services/config" + "strconv" "time" "gorm.io/gorm" @@ -24,7 +26,8 @@ type Users struct { } func (u *Users) PasswordHash(password string) string { - return crypto.Sha256(password + u.CreatedAt.GoString() + config.GetSystemSalt()) + fmt.Println(password, u.CreatedAt.UnixMicro(), config.GetSystemSalt()) + return crypto.Sha256(password + strconv.FormatInt(u.CreatedAt.UnixMicro(), 10) + config.GetSystemSalt()) } func (u *Users) CheckPassword(password string) bool { diff --git a/routers/api/v1/user/register.go b/routers/api/v1/user/register.go index 9235ed7..6ca6334 100644 --- a/routers/api/v1/user/register.go +++ b/routers/api/v1/user/register.go @@ -35,7 +35,6 @@ func registerHandler(ctx iris.Context) { Role: 0, Balance: 0, } - user.Password = user.PasswordHash(userReq.Password) if !config.GetSystemVerify() { user.Role = 1 @@ -48,5 +47,13 @@ func registerHandler(ctx iris.Context) { return } + user.Password = user.PasswordHash(userReq.Password) + result = database.DB.Save(&user) + if result.Error != nil { + l.Error("save user error: %v", result.Error) + middleware.Error(ctx, middleware.CodeRegisterError, iris.StatusInternalServerError) + return + } + middleware.Success(ctx) } diff --git a/services/system/init.go b/services/system/init.go index 9f8dde8..4d4afde 100644 --- a/services/system/init.go +++ b/services/system/init.go @@ -23,12 +23,17 @@ func systemInit() (err error) { Email: "admin@gpuManager.com", Role: 3, } - user.Password = user.PasswordHash("admin123456") result := database.DB.Create(&user) if result.Error != nil { l.Error("Create admin user failed, Error: %v", result.Error) return } + user.Password = user.PasswordHash("admin123456") + result = database.DB.Save(&user) + if result.Error != nil { + l.Error("Save admin user failed, Error: %v", result.Error) + return + } l.Info("System init success")