zdpgo_gin_login

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2024 License: Apache-2.0 Imports: 6 Imported by: 2

README

zdpgo_gin_login

适配gin框架的登录注册功能组件,通过本框架轻松拥有登录注册相关的API接口

安装

go get github.com/zhangdapeng520/zdpgo_gin_login

使用

基本用法
package main

import (
	gin "github.com/zhangdapeng520/zdpgo_gin"
	ginLogin "github.com/zhangdapeng520/zdpgo_gin_login"
	gorm "github.com/zhangdapeng520/zdpgo_gorm"
	_ "github.com/zhangdapeng520/zdpgo_mysql"
)

var (
	jwtKey       = "zhangdapeng.com"
	passwordSalt = "zhangdapeng.com"
)

func main() {
	db, err := gorm.Open(
		"mysql",
		"root:root@tcp(127.0.0.1:3306)/blog?charset=utf8",
	)
	if err != nil {
		panic(err)
	}
	db.AutoMigrate(&ginLogin.GinLoginUser{})
	defer db.Close()

	r := gin.Default()

	r.POST("/user/register/", ginLogin.GetRegisterHandler(db, passwordSalt))
	r.POST("/user/login/", ginLogin.GetLoginHandler(db, jwtKey, passwordSalt))

	r.Run(":8888")
}
使用注册路由的方法

通过ginLogin.RegisterRouter(r, db, passwordSalt, jwtKey)方法,我们会自动拥有注册接口和登录接口。

示例代码:

package main

import (
	gin "github.com/zhangdapeng520/zdpgo_gin"
	ginLogin "github.com/zhangdapeng520/zdpgo_gin_login"
	gorm "github.com/zhangdapeng520/zdpgo_gorm"
	_ "github.com/zhangdapeng520/zdpgo_mysql"
)

var (
	jwtKey       = "zhangdapeng.com"
	passwordSalt = "zhangdapeng.com"
)

func main() {
	db, err := gorm.Open(
		"mysql",
		"root:root@tcp(127.0.0.1:3306)/zdpgo_gin_login?charset=utf8",
	)
	if err != nil {
		panic(err)
	}
	db.AutoMigrate(&ginLogin.GinLoginUser{})
	defer db.Close()

	r := gin.Default()
	ginLogin.RegisterRouter(r, db, passwordSalt, jwtKey)
	r.Run(":8888")
}

测试注册接口:

req -X POST -H 'Content-Type:application/json' -d '{\"username\":\"zhangdapeng\",\"password\":\"zhangdapeng520\"}' http://127.0.0.1:8888/zdpgo_gin_login/register/

测试登录接口:

req -X POST -H 'Content-Type:application/json' -d '{\"username\":\"zhangdapeng\",\"password\":\"zhangdapeng520\"}' http://127.0.0.1:8888/zdpgo_gin_login/login/

版本

v0.1.0
  • 增加注册和登录两个接口的路由获取方式
v0.1.1
  • 登录接口返回用户名
v0.1.2
  • 增加注册路由的方法

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLoginHandler

func GetLoginHandler(db *gorm.DB, jwtKey, passwordSalt string) gin.HandlerFunc

GetLoginHandler 获取登录的路由

func GetRegisterHandler

func GetRegisterHandler(db *gorm.DB, passwordSalt string) gin.HandlerFunc

GetRegisterHandler 获取注册的路由

func GetToken

func GetToken(id int, username, jwtKey string) (string, error)

GetToken 生成Token

func RegisterRouter added in v0.1.2

func RegisterRouter(
	app *gin.Engine,
	db *gorm.DB,
	passwordSalt string,
	jwtKey string,
)

RegisterRouter 注册登录相关的路由 @param app API根路由 @param db 操作数据库的对象 @param passwordSalt 密码加密的盐值 @param jwtKey JWT加密的盐值

Types

type GinLoginUser

type GinLoginUser struct {
	Id       int    `json:"id"`
	Username string `json:"username" gorm:"column:username;unique"`
	Password string `json:"password,omitempty"`
}

type MyClaims

type MyClaims struct {
	Id       int    `json:"id"`
	Username string `json:"username"`
	jwt.RegisteredClaims
}

func ParseToken

func ParseToken(tokenString, jwtKey string) (*MyClaims, error)

ParseToken 解析Token

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL