user

package
v0.0.0-...-e07aaa2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const BCRYPT_COST = 12

Variables

This section is empty.

Functions

func ConfigureRoutes

func ConfigureRoutes(router *gin.Engine, db *gorm.DB, jwtSecret string)

func RequireAuth

func RequireAuth(JWTSecret string) gin.HandlerFunc

Middleware that requires a valid JWT token to be present in the Authorization header, meaning the user has an account to modify the functions they own. You want to add this middleware to a group (router.Group()) of routes that require authentication. JWTSecret is the secret key used to sign the JWT tokens. This middleware will set the "userID" and "username" keys in the gin.Context.

Example:

func ConfigureRoutes(router *gin.Engine, db *gorm.DB, jwtSecret string) {
	controller := Controller{DB: db, JWTSecret: jwtSecret}
	group := router.Group("/my-prefix")
	group.Use(RequireAuth(jwtSecret))
	group.POST("/", controller.postFunction)
	group.GET("/test", controller.testFunction)
}

Types

type Claims

type Claims struct {
	jwt.RegisteredClaims
	ID uint `json:"id"`
}

type Controller

type Controller struct {
	DB        *gorm.DB
	JWTSecret string
}

type User

type User struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
}

Jump to

Keyboard shortcuts

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