一,实现用户登录
首先,在routers.go文件中添加路由
然后在UserController.go中改写代码:
package controller
import (
"fmt"
"go-gin-vue/ginessential/common"
"go-gin-vue/ginessential/model"
"go-gin-vue/ginessential/util"
"net/http"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"golang.org/x/crypto/bcrypt"
)
func Register(ctx *gin.Context) {
DB := common.GetDB()
//获取参数
name := ctx.PostForm("name")
telephone := ctx.PostForm("telephone")
password := ctx.PostForm("password")
//数据验证
if len(telephone) != 11 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "手机号必须为11位"})
// ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{"code": 422, "msg": "手机号必须为11位"})
return
}
if len(password) < 6 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "密码不能少于6位"})
return
}
//如果名称没有传,给一个10位的随机数
if len(name) == 0 {
name = util.RandomString(10)
}
fmt.Println(name, telephone, password)
//判断手机号是否存在
if isTelephoneExist(DB, telephone) {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "用户已经存在,不能注册"})
return
}
//创建用户
hasePassword, err := bcrypt.GenerateFromPassword([]byte(password),bcrypt.DefaultCost)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"code":500, "msg":"加密错误"})
return
}
newUser := model.User{
Name: name,
Telephone: telephone,
Password: string(hasePassword),
}
DB.Create(&newUser)
//返回结果
// c.JSON:返回JSON格式的数据
ctx.JSON(200, gin.H{
"code": 200,
"msg": "注册成功",
})
}
func Login(ctx *gin.Context) {
DB := common.GetDB()
//获取参数
telephone := ctx.PostForm("telephone")
password := ctx.PostForm("password")
//数据验证
if len(telephone) != 11 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "手机号必须为11位"})
// ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{"code": 422, "msg": "手机号必须为11位"})
return
}
if len(password) < 6 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "密码不能少于6位"})
return
}
//判断手机号是否存在
var user model.User
DB.Where("telephone = ?", telephone).First(&user)
if(user.ID == 0){
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "用户不存在"})
return
}
//判断密码是否正确
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) ; err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"code":4000, "msg":"密码错误"})
return
}
//发放token给前端
token := "11"
//返回结果
ctx.JSON(200, gin.H{
"code": 200,
"data": gin.H{"token": token},
"msg": "登录成功",
})
}
func isTelephoneExist(db *gorm.DB, telephone string) bool {
var user model.User
db.Where("telephone = ?", telephone).First(&user)
if user.ID != 0 {
return true
}
//
return false
}
新增部分为Login函数块,下面进行具体说明
1,这里主要使用到的是一个为密码加密的函数
hasePassword, err := bcrypt.GenerateFromPassword([]byte(password),bcrypt.DefaultCost)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"code":500, "msg":"加密错误"})
return
}
上面代码的第一行即为我们用户输入的密码进行加密
我们通过postman模拟用户注册
来到我们的数据库中,我们可以看到一个新的用户已经被添加到数据库中了,而且这个用户的密码还是加密过的,我们无法看到
接下来,我们继续通过postman进行测试登录,看看当我们用户输入正确的电话号和密码时能否登陆成功
可以看到,我们成功查询到了这位用户已经注册并且登录成功,也携带了token
这一天我们主要学习了为用户密码加密,防止用户信息泄露