connect

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 4 Imported by: 0

README

Connect Middleware for echo

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/connect-middleware-for-echo

Getting Started

package main

import (
	"github.com/go-zoox/connect-middleware-for-echo"
	"github.com/labstack/echo/v4"
)

func main() {
	r := echo.New()

	r.Use(connect.Create("YOUR_SECRET_KEY"))

	r.GET("/user", func(c echo.Context) error {
		user, err := connect.GetUser(c)
		if err != nil {
			return c.JSON(401, map[string]any{
				"message": "unauthorized",
			})
		}

		return c.JSON(200, map[string]any{
			"user": user,
		})
	})

	r.GET("/", func(c echo.Context) error {
		return c.JSON(200, map[string]any{
			"message": "helloworld",
		})
	})

	r.Logger.Fatal(r.Start(":8080"))
}

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

View Source
const ContextUserKey = "zoox.connect::user"

ContextUserKey is the key for the user in the context.

Variables

View Source
var Version = "1.2.0"

Version is the current version of the package.

Functions

func Create

func Create(secretKey string, opts ...*CreateOptions) echo.MiddlewareFunc

Create creates a connect middleware for gin.

func GetToken added in v1.2.0

func GetToken(ctx echo.Context) (token string, err error)

GetToken get user from context.

func GetUser

func GetUser(ctx echo.Context) (u *user.User, err error)

GetUser gets the user from the context.

func MustGetToken added in v1.2.0

func MustGetToken(ctx echo.Context) (token string)

MustGetToken get user from context.

func MustGetUser added in v1.2.0

func MustGetUser(ctx echo.Context) (u *user.User)

MustGetUser get user from context.

Types

type CreateOptions

type CreateOptions struct {
	RequireAuth bool
}

CreateOptions is the options for the Create middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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