preface

Complete registration and login today and use middleware for verification!

The target

  • User registration and login

Gin middleware is used for verification

User registration and login

Define the entity

UserEntity

package Entity

type UserEntity struct {
	Id        string `gorm:"primaryKey"`
	Age       int
	Password  string
	LoginName string
	NickName  string
}

// Customize the table name
func (UserEntity) TableName(a) string {
	return "User"
}

Copy the code

Download the required packages

uuid

go get github.com/satori/go.uuid
Copy the code

go-cache

go get github.com/patrickmn/go-cache
Copy the code

Adding a Controller

UserController

package UserController

import (
	"github.com/gin-gonic/gin"
	"golang-blog/Model/ViewModel/ApiState"
	"golang-blog/Service/UserService"
)

func GetUserInfo(c *gin.Context) {
	loginName := c.Query("loginName")
	password := c.Query("password")
	if loginName == "" || password == "" {
		ApiState.ArgErrApiResult(c, "loginName or password")
		return
	}
	userInfo := UserService.GetUserInfo(loginName, password)
	ApiState.ResponseSuccess(c, userInfo)
}


Copy the code

RegisterController

package RegisterController

import (
	"github.com/gin-gonic/gin"
	"golang-blog/Model/Entity"
	"golang-blog/Model/ViewModel/ApiState"
	"golang-blog/Service/RegisterService"
	"golang-blog/Service/UtilService"
	"strconv"
)

func RegisterUser(c *gin.Context) {
	loginName := c.PostForm("loginName")
	password := c.PostForm("password")
	if loginName == "" || password == "" {
		ApiState.ArgErrApiResult(c, "loginName or password")
		return
	}
	nickName := c.PostForm("nickName")
	ageStr := c.PostForm("age")
	age, _ := strconv.Atoi(ageStr)
	user := Entity.UserEntity{
		Id:        UtilService.UUId(),
		Age:       age,
		Password:  password,
		LoginName: loginName,
		NickName:  nickName,
	}
	RegisterService.RegisterUser(user)
	ApiState.ResponseSuccess(c, "Registration successful")}Copy the code

LoginController

package LoginController

import (
	"github.com/gin-gonic/gin"
	"golang-blog/Model/ViewModel/ApiState"
	"golang-blog/Service/UserService"
)

func Index(c *gin.Context) {
	loginName := c.PostForm("loginName")
	password := c.PostForm("password")
	if loginName == "" || password == "" {
		ApiState.ArgErrApiResult(c, "loginName or password")
		return
	}
	userInfo := UserService.GetUserInfo(loginName, password)
	if userInfo.Id == "" {
		ApiState.ArgErrApiResult(c, "loginName or password")
		return
	}
	//cookieId := UtilService.UUId()
	//CacheService.SetCache(cookieId, userInfo)

	ApiState.ResponseSuccess(c, userInfo)
}

Copy the code

Set a route for the new controller

package Routers

import (
	"github.com/gin-gonic/gin"
	"golang-blog/Controller/HomeControler"
	"golang-blog/Controller/LoginController"
	"golang-blog/Controller/RegisterController"
	"golang-blog/Controller/UserController"
	"golang-blog/Service/ConfigService"
)

func Init(router *gin.Engine) {
	home := router.Group("Home")
	register := router.Group("Register")
	user := router.Group("User")
	login := router.Group("Login")

	// 1. The first superfluous element will be removed (.. / or //);
	//2. The route searches the new path in a case-insensitive manner.
	//3. If the handler is found correctly, the route is redirected to the correct handler and returns either 301 or 307. //Foo may be redirected to /Foo)
	router.RedirectFixedPath = true

	{
		home.GET("/", HomeControler.Index)
		home.GET("/Hi", HomeControler.Hi)

		register.POST("/RegisterUser", RegisterController.RegisterUser)

		user.GET("/GetUserInfo", UserController.GetUserInfo)

		login.POST("/Index", LoginController.Index)
	}

	serverConfig := ConfigService.GetServerConfig()
	router.Run(serverConfig.HTTP_PORT) // Listen and start the service on 127.0.0.1:8888
}

Copy the code

Service

UserService

package UserService

import (
	"golang-blog/Model/Entity"
	"golang-blog/Service/DbService"
)

// Query user information based on the login name and password
func GetUserInfo(loginName string, password string) Entity.UserEntity {
	var (
		user Entity.UserEntity
	)
	DbService.Db.Where(&Entity.UserEntity{LoginName: loginName, Password: password}).First(&user)
	return user
}

func ByLoginNameGetUser(loginName string) Entity.UserEntity {
	var (
		user Entity.UserEntity
	)
	DbService.Db.Where(&Entity.UserEntity{LoginName: loginName}).First(&user)
	return user
}

func SignIn(user Entity.UserEntity) string {
	user.Id = "123"
	DbService.Db.Create(&user)
	return user.Id
}

func Update(user Entity.UserEntity) {
	DbService.Db.Model(&Entity.UserEntity{}).Where(&Entity.UserEntity{Id: user.Id}).Updates(user)
}

func Delete(userId string) {
	DbService.Db.Delete(&Entity.UserEntity{}, userId)
}

Copy the code

RegisterService

package RegisterService

import (
	"golang-blog/Model/Entity"
	"golang-blog/Service/DbService"
)

// Register a user
func RegisterUser(user Entity.UserEntity) string {
	user.Id = "123"
	DbService.Db.Create(&user)
	return user.Id
}
Copy the code

CacheService

package CacheService

import (
	"github.com/patrickmn/go-cache"
	"time"
)

var goCache = cache.New(30*time.Minute, 30*time.Minute)

func SetCache(key string, value interface{}) {
	goCache.Set(key, value, cache.DefaultExpiration)
}

func GetCache(key string) interface{} {
	value, bo := goCache.Get(key)
	if! bo {return nil
	}
	return value
}

Copy the code

The final result

Registered users

Querying User Information

The login

Source repository address

Github.com/panle666/Go…