修复同一用户和邮箱重复注册问题
This commit is contained in:
parent
4a1d5eb195
commit
c4b3eeb394
|
|
@ -1,9 +1,12 @@
|
|||
package dao
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int `gorm:"primaryKey column:id"`
|
||||
gorm.Model
|
||||
Name string `gorm:"column:name"`
|
||||
Age int `gorm:"column:age"`
|
||||
Email string `gorm:"column:email"`
|
||||
|
|
@ -13,7 +16,7 @@ type User struct {
|
|||
UpdateTime string `gorm:"column:update_time"`
|
||||
}
|
||||
|
||||
func CreateUser(name string, password, email string) int {
|
||||
func CreateUser(name string, password, email string) uint {
|
||||
user := User{Name: name, Email: email, Password: password}
|
||||
DB.Create(&user)
|
||||
return user.ID
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ type RLReq struct {
|
|||
}
|
||||
|
||||
func loginHandler(c *gin.Context) {
|
||||
|
||||
var req_data RLReq
|
||||
tokenString := ""
|
||||
if err := c.ShouldBindJSON(&req_data); err == nil {
|
||||
|
|
@ -50,7 +49,7 @@ func loginHandler(c *gin.Context) {
|
|||
}
|
||||
worker.SetRedisWithExpire(tokenString, tokenString, time.Hour*10) // 设置过期时间为10分钟
|
||||
// 返回令牌
|
||||
c.JSON(200, gin.H{"token": tokenString, "username": req_data.User, "code": 0, "message": "success"})
|
||||
c.JSON(200, gin.H{"code": 0, "message": "success", "token": tokenString})
|
||||
} else {
|
||||
c.JSON(400, gin.H{"error": "user not found", "code": 1, "message": "error"})
|
||||
}
|
||||
|
|
@ -69,6 +68,10 @@ func registerHandler(c *gin.Context) {
|
|||
hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值
|
||||
req_data.Password = hex.EncodeToString(hasher.Sum(nil)) // 生成密码的 MD5 散列值
|
||||
}
|
||||
if service.ContainsUser(req_data.User, req_data.Email) == true {
|
||||
c.JSON(400, gin.H{"error": "user already exists", "code": 1, "message": "error"})
|
||||
return
|
||||
}
|
||||
id := service.CreateUser(req_data.User, req_data.Password, req_data.Email)
|
||||
// 生成 JWT 令牌
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"videoplayer/dao"
|
||||
)
|
||||
|
||||
func CreateUser(name string, password, email string) int {
|
||||
func CreateUser(name string, password, email string) uint {
|
||||
return dao.CreateUser(name, password, email)
|
||||
}
|
||||
|
||||
|
|
@ -23,3 +23,12 @@ func GetUser(name, password string) dao.User {
|
|||
}
|
||||
return dao.User{}
|
||||
}
|
||||
|
||||
func ContainsUser(name, email string) bool {
|
||||
user := dao.FindUserByName(name)
|
||||
user2 := dao.FindUserByEmail(email)
|
||||
if user.ID != 0 || user2.ID != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue