cotter

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2021 License: MIT Imports: 7 Imported by: 0

README

cotter-go

Go HTTP middleware for https://www.cotter.app authentication.

Install

go get github.com/thehadalone/cotter-go

Examples

Default
authMiddleware, err := cotter.NewMiddleware(context.Background(), <YOUR_COTTER_API_KEY_ID>)
if err != nil {
    log.Fatal(err)
}

r := chi.NewRouter()
r.Use(authMiddleware)
Custom error handler
errorHandler := func(w http.ResponseWriter, r *http.Request, e error) {
    if errors.Is(e, cotter.ErrUnauthorized) {
        // Custom error handling logic.
        return
    }

    http.Error(w, e.Error(), http.StatusInternalServerError)
}

authMiddleware, err := cotter.NewMiddleware(ctx, <YOUR_COTTER_API_KEY_ID>, cotter.WithErrorHandler(errorHandler))
if err != nil {
    log.Fatal(err)
}

r := chi.NewRouter()
r.Use(authMiddleware)
Get Cotter user ID from the context.
handler := func(w http.ResponseWriter, r *http.Request) {
    cotterID := cotter.UserID(r.Context())
}

Documentation

Index

Constants

View Source
const ErrUnauthorized e = "unauthorized"

ErrUnauthorized represents token validation errors.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(ctx context.Context, apiKeyID string, opts ...Option) (func(http.Handler) http.Handler, error)

NewMiddleware creates new Cotter authentication middleware.

func SetUserID added in v1.1.0

func SetUserID(ctx context.Context, userID string) context.Context

SetUserID returns a copy of the context with the provided Cotter user ID.

func UserID

func UserID(ctx context.Context) string

UserID extracts Cotter user ID from the context.

Types

type ErrorHandler

type ErrorHandler func(w http.ResponseWriter, r *http.Request, e error)

ErrorHandler function.

type Option

type Option func(*options)

Option for the middleware.

func WithErrorHandler

func WithErrorHandler(errorHandler ErrorHandler) Option

WithErrorHandler option sets custom error handler for the middleware.

Jump to

Keyboard shortcuts

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