修复同一用户和邮箱重复注册问题
This commit is contained in:
parent
4a1d5eb195
commit
c4b3eeb394
|
|
@ -1,9 +1,12 @@
|
||||||
package dao
|
package dao
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `gorm:"primaryKey column:id"`
|
gorm.Model
|
||||||
Name string `gorm:"column:name"`
|
Name string `gorm:"column:name"`
|
||||||
Age int `gorm:"column:age"`
|
Age int `gorm:"column:age"`
|
||||||
Email string `gorm:"column:email"`
|
Email string `gorm:"column:email"`
|
||||||
|
|
@ -13,7 +16,7 @@ type User struct {
|
||||||
UpdateTime string `gorm:"column:update_time"`
|
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}
|
user := User{Name: name, Email: email, Password: password}
|
||||||
DB.Create(&user)
|
DB.Create(&user)
|
||||||
return user.ID
|
return user.ID
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ type RLReq struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginHandler(c *gin.Context) {
|
func loginHandler(c *gin.Context) {
|
||||||
|
|
||||||
var req_data RLReq
|
var req_data RLReq
|
||||||
tokenString := ""
|
tokenString := ""
|
||||||
if err := c.ShouldBindJSON(&req_data); err == nil {
|
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分钟
|
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 {
|
} else {
|
||||||
c.JSON(400, gin.H{"error": "user not found", "code": 1, "message": "error"})
|
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 散列值
|
hasher.Write([]byte(req_data.Password)) // 生成密码的 MD5 散列值
|
||||||
req_data.Password = hex.EncodeToString(hasher.Sum(nil)) // 生成密码的 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)
|
id := service.CreateUser(req_data.User, req_data.Password, req_data.Email)
|
||||||
// 生成 JWT 令牌
|
// 生成 JWT 令牌
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateUser(name string, password, email string) int {
|
func CreateUser(name string, password, email string) uint {
|
||||||
return dao.CreateUser(name, password, email)
|
return dao.CreateUser(name, password, email)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -23,3 +23,12 @@ func GetUser(name, password string) dao.User {
|
||||||
}
|
}
|
||||||
return 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