参考视频:【评论送书】Go语言 Gin+Vue 前后端分离实战 - OceanLearn_哔哩哔哩_bilibili
上一篇:Gin+Gorm+PostGresSQL+Vue项目实战(2)_代码骑士的博客-CSDN博客
一、实现用户登录:
1、修改routes.go代码:
用户登录路径:
r.POST("/api/auth/login", controller.Login)
文件所在位置:
routes.go 代码:
package router
import (
"testGinAndVue01/controller"
"github.com/gin-gonic/gin"
)
func CollectRoute(r *gin.Engine) *gin.Engine {
r.POST("/api/auth/register", controller.Register)
r.POST("/api/auth/login", controller.Login)
return r
}
2、修改usercontroller.go代码
添加Login(登录)函数:
func Login(ctx *gin.Context) {
//获取参数
telephone := ctx.PostForm("telephone")
password := ctx.PostForm("password")
//数据验证
if len(telephone) != 11 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "手机号必须是11位"})
return
}
if len(password) < 6 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "密码至少6位"})
return
}
//判断用户是否注册过
if isTelephoneExist_(telephone) {
//判断密码是否正确
var user model.User
DB := common.GetDB()
DB.Where("telephone=?", telephone).First(&user)
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 400, "msg": "密码错误"})
return
} else {
//发放token给前端
token := "111"
//返回结果
ctx.JSON(200, gin.H{
"code": 200,
"data": gin.H{"token": token},
"msg": "登录成功",
})
}
} else {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "该用户不存在"})
return
}
}
创建用户实现密码加密:
//创建用户
hasedPassword, 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(hasedPassword),
}
全部代码:
package controller
import (
"log"
"net/http"
"testGinAndVue01/common"
"testGinAndVue01/model"
"testGinAndVue01/util"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
//"gorm.io/gorm"
)
func Register(ctx *gin.Context) {
//db := common.InitDB()
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位"})
return
}
if len(password) < 6 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "密码至少6位"})
return
}
//如果名称为空值就随机生成十位字符串
if len(name) == 0 {
name = util.RandomString(10)
}
log.Println(name, telephone, password)
//判断手机号是否存在
if isTelephoneExist_(telephone) {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "该手机号已被注册"})
return
}
//创建用户
hasedPassword, 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(hasedPassword),
}
db.Create(&newUser)
//返回结果
ctx.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "注册成功!",
})
}
func isTelephoneExist_(telephone string) bool {
var user model.User
db := common.GetDB()
db.Where("telephone=?", telephone).First(&user)
if user.ID != 0 {
return true
} else {
return false
}
}
func Login(ctx *gin.Context) {
//获取参数
telephone := ctx.PostForm("telephone")
password := ctx.PostForm("password")
//数据验证
if len(telephone) != 11 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "手机号必须是11位"})
return
}
if len(password) < 6 {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "密码至少6位"})
return
}
//判断用户是否注册过
if isTelephoneExist_(telephone) {
//判断密码是否正确
var user model.User
DB := common.GetDB()
DB.Where("telephone=?", telephone).First(&user)
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 400, "msg": "密码错误"})
return
} else {
//发放token给前端
token := "111"
//返回结果
ctx.JSON(200, gin.H{
"code": 200,
"data": gin.H{"token": token},
"msg": "登录成功",
})
}
} else {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"code": 422, "msg": "该用户不存在"})
return
}
}
3、登录演示
先将以前的数据信息清除,因为这次使用了密码加密代码,用户信息需要重新注册。
加密之后的算法字符长度会发生变化,所以我们需要将字符长度增大。
删除表再重新运行程序。
还有一个坑点也是在做项目时遇到的,就是go语言的包命名(包括项目名)尽量使用小写,因为go不能区分大小写,所以像是大小写交替出现的这种情况就会发生导包失败。
注册:
post:
http://localhost:8080/api/auth/register
post:
http://localhost:8080/api/auth/login
数据库:
这里的密码都是加密的,那我们是如何判断用户密码是否正确呢?
首先我们先用password记录原始密码(未加密)在和此时数据库中对应手机号的密码(已加密)的进行比较 ,通过bcrypt包中的比较密码函数即可解决问题。
下一篇: