middleware

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoginRequired

func LoginRequired[T any](secretKey string, userLoader UserLoader[T]) gora.MiddlewareFunc

LoginRequired when called with secretKey and userLoader creates a jwt middleware that automatically extracts jwt from the request header, verifies it fetches the user using the userLoader function and attaches it to the context with the key "user". You can access the user downstream from the context and cast it Usage:

secretKey := os.Getenv("SECRET_KEY")
func userLoader(id uint) (models.User, error){
	return repo.FetchUser(id)
}
AuthMiddleware := LoginRequired(secretKey, userLoader)

r := gora.Default()
r.Use(AuthMiddleware)

Types

type UserLoader

type UserLoader[T any] func(userId uint) (user T, err error)

UserLoader function loads user from the database given the id. Returns the user and an error if user can not be loaded or user is not active.

Jump to

Keyboard shortcuts

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