fxsupertoken

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2025 License: MIT Imports: 11 Imported by: 0

README

fxSupertoken

A SuperTokens integration module for fxEcho that provides authentication and session management capabilities.

Features

  • fxEcho Compatibility: Fully compatible with the fxEcho module architecture
  • Dependency Injection: Uses Uber's fx for dependency injection
  • Configuration Management: Integrates with fxConfig for centralized configuration
  • Middleware Support: Provides both SuperTokens middleware and session verification middleware
  • Priority-based Middleware: Supports priority-based middleware registration

Installation

go get github.com/UTOL-s/module/fxSupertoken

Configuration

Add SuperTokens configuration to your config.yaml:

supertokens:
  connection_uri: "your_supertokens_connection_uri"
  connection_api_key: "your_supertokens_api_key"
  app_name: "YourApp"
  api_domain: "http://localhost:8080"
  website_domain: "http://localhost:3000"
  api_base_path: "/api/auth"
  web_base_path: "/api/auth"
  email:
    host: "smtp.gmail.com"
    password: "your_email_password"
    email: "your_email@gmail.com"

Usage

Basic Integration
package main

import (
    "github.com/labstack/echo/v4"
    "go.uber.org/fx"
    "go.uber.org/zap"

    fxConfig "github.com/UTOL-s/module/fxConfig"
    fxEcho "github.com/UTOL-s/module/fxEcho"
    fxSupertoken "github.com/UTOL-s/module/fxSupertoken"
)

func main() {
    app := fx.New(
        // Provide core dependencies
        fx.Provide(
            NewConfig,
            NewLogger,
        ),

        // Register SuperTokens middlewares
        fx.Provide(
            fxSupertoken.AsSuperTokensMiddleware(),
            fxSupertoken.AsVerifySessionMiddleware(),
        ),

        // Include modules
        fxSupertoken.FxSupertoken,
        fxEcho.FxEcho,

        // Start application
        fx.Invoke(func(e *echo.Echo, logger *zap.Logger) {
            logger.Info("Application started")
        }),
    )

    app.Run()
}
Using Middlewares
SuperTokens Middleware

The SuperTokens middleware provides basic session management:

func ProtectedHandler(c echo.Context) error {
    session := c.Get("supertokensSession")
    return c.JSON(200, map[string]interface{}{
        "message": "Protected route",
        "session": session,
    })
}

// Register route with middleware
e.GET("/protected", ProtectedHandler, fxSupertoken.SupertokenMiddleware)
Session Verification Middleware

The session verification middleware provides enhanced session verification:

func VerifySessionHandler(c echo.Context) error {
    session := c.Get("session")
    return c.JSON(200, map[string]interface{}{
        "message": "Session verified",
        "session": session,
    })
}

// Register route with middleware
e.GET("/verify-session", VerifySessionHandler, fxSupertoken.VerifySession)
Advanced Usage with Route Groups
func main() {
    app := fx.New(
        // ... other providers

        fx.Invoke(func(e *echo.Echo) {
            // Create protected route group
            protected := e.Group("/api")
            protected.Use(fxSupertoken.SupertokenMiddleware)
            
            protected.GET("/users", GetUsersHandler)
            protected.POST("/users", CreateUserHandler)
            
            // Create admin route group with session verification
            admin := e.Group("/admin")
            admin.Use(fxSupertoken.VerifySession)
            
            admin.GET("/dashboard", AdminDashboardHandler)
        }),
    )

    app.Run()
}

API Reference

Types
SuperTokensConfig

Configuration structure for SuperTokens:

type SuperTokensConfig struct {
    ConnectionURI    string
    ConnectionAPIKey string
    EmailHost        string
    EmailPassword    string
    Email            string
    APIBasePath      string
    WebBasePath      string
    AppName          string
    APIDomain        string
    WebsiteDomain    string
}
MiddlewareRegistryIf

Interface for middleware registration:

type MiddlewareRegistryIf interface {
    Priority() int
    Middleware() echo.MiddlewareFunc
}
Functions
NewSuperTokensConfig

Creates SuperTokens configuration from fxConfig:

func NewSuperTokensConfig(config *fxConfig.Config) (*SuperTokensConfig, error)
NewSuperTokensMiddleware

Creates the main SuperTokens middleware:

func NewSuperTokensMiddleware() echo.MiddlewareFunc
NewVerifySessionMiddleware

Creates the session verification middleware:

func NewVerifySessionMiddleware() echo.MiddlewareFunc
AsSuperTokensMiddleware

Annotates SuperTokens middleware for fxEcho:

func AsSuperTokensMiddleware() any
AsVerifySessionMiddleware

Annotates session verification middleware for fxEcho:

func AsVerifySessionMiddleware() any
Module
FxSupertoken

The main fx module that provides all SuperTokens functionality:

var FxSupertoken = fx.Module(
    "fxsupertoken",
    fx.Provide(
        NewSuperTokensConfig,
        NewSuperTokensMiddleware,
        NewVerifySessionMiddleware,
        NewSuperTokensMiddlewareRegistry,
        NewVerifySessionMiddlewareRegistry,
    ),
    fx.Invoke(InitSuperTokens),
)

Middleware Priority

The module uses priority-based middleware registration:

  • SuperTokens Middleware: Priority 100
  • Session Verification Middleware: Priority 200

Higher priority middlewares are executed first.

Error Handling

The module provides proper error handling for:

  • Configuration loading errors
  • SuperTokens initialization errors
  • Session verification errors
  • Middleware execution errors

Examples

See the example/ directory for complete working examples.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

License

This project is licensed under the MIT License.

Documentation

Index

Constants

View Source
const ModuleName = "fxsupertoken"

Variables

FxInit is the fx invoke function for initializing SuperTokens

Functions

func AsMiddleware

func AsMiddleware(f any) any

AsMiddleware annotates a middleware constructor for fxEcho compatibility

func AsSuperTokensMiddleware

func AsSuperTokensMiddleware() any

AsSuperTokensMiddleware annotates SuperTokens middleware for fxEcho

func AsSuperTokensWrapperMiddleware

func AsSuperTokensWrapperMiddleware() any

AsSuperTokensWrapperMiddleware annotates SuperTokens wrapper middleware for fxEcho

func AsVerifySessionMiddleware

func AsVerifySessionMiddleware() any

AsVerifySessionMiddleware annotates session verification middleware for fxEcho

func DefaultCORSHandler

func DefaultCORSHandler() echo.MiddlewareFunc

DefaultCORSHandler returns a custom CORS middleware with specific allowed origins Includes localhost ports for development and production UTOL domains

func InitSuperTokens

func InitSuperTokens(config *SuperTokensConfig) error

InitSuperTokens initializes SuperTokens with the provided configuration

func NewSuperTokensMiddleware

func NewSuperTokensMiddleware(config *SuperTokensConfig) echo.MiddlewareFunc

NewSuperTokensMiddleware creates the main SuperTokens middleware

func NewSuperTokensWrapperMiddleware

func NewSuperTokensWrapperMiddleware() echo.MiddlewareFunc

NewSuperTokensWrapperMiddleware creates the SuperTokens wrapper middleware

func NewVerifySessionMiddleware

func NewVerifySessionMiddleware(config *SuperTokensConfig) echo.MiddlewareFunc

NewVerifySessionMiddleware creates the session verification middleware

func SuperTokensMiddlewareWrapper

func SuperTokensMiddlewareWrapper() echo.MiddlewareFunc

SuperTokensMiddlewareWrapper wraps SuperTokens middleware for Echo

func SupertokenMiddleware

func SupertokenMiddleware(config *SuperTokensConfig) echo.MiddlewareFunc

SupertokenMiddleware creates a middleware that checks if SuperTokens is initialized

func VerifySession

func VerifySession(config *SuperTokensConfig) echo.MiddlewareFunc

VerifySession creates a middleware that verifies SuperTokens session

Types

type MiddlewareRegistryIf

type MiddlewareRegistryIf interface {
	Priority() int
	Middleware() echo.MiddlewareFunc
}

MiddlewareRegistryIf defines the interface for middleware registration

func NewSuperTokensMiddlewareRegistry

func NewSuperTokensMiddlewareRegistry(config *SuperTokensConfig) MiddlewareRegistryIf

NewSuperTokensMiddlewareRegistry creates a middleware registry for SuperTokens

func NewSuperTokensWrapperMiddlewareRegistry

func NewSuperTokensWrapperMiddlewareRegistry() MiddlewareRegistryIf

NewSuperTokensWrapperMiddlewareRegistry creates a middleware registry for SuperTokens wrapper

func NewVerifySessionMiddlewareRegistry

func NewVerifySessionMiddlewareRegistry(config *SuperTokensConfig) MiddlewareRegistryIf

NewVerifySessionMiddlewareRegistry creates a middleware registry for session verification

type SuperTokensConfig

type SuperTokensConfig struct {
	ConnectionURI    string `mapstructure:"connection_uri"`
	ConnectionAPIKey string `mapstructure:"connection_api_key"`
	EmailHost        string `mapstructure:"email.host"`
	EmailPassword    string `mapstructure:"email.password"`
	Email            string `mapstructure:"email"`
	APIBasePath      string `mapstructure:"api_base_path"`
	WebBasePath      string `mapstructure:"web_base_path"`
	AppName          string `mapstructure:"app_name"`
	APIDomain        string `mapstructure:"api_domain"`
	WebsiteDomain    string `mapstructure:"website_domain"`
	IsInitialized    bool   // Track if SuperTokens is properly initialized
}

SuperTokensConfig holds SuperTokens configuration

func NewSuperTokensConfig

func NewSuperTokensConfig(config *fxConfig.Config) (*SuperTokensConfig, error)

NewSuperTokensConfig creates SuperTokens configuration from fxConfig

type SuperTokensMiddlewareRegistry

type SuperTokensMiddlewareRegistry struct {
	// contains filtered or unexported fields
}

SuperTokensMiddlewareRegistry implements MiddlewareRegistryIf for SuperTokens middleware

func (*SuperTokensMiddlewareRegistry) Middleware

func (*SuperTokensMiddlewareRegistry) Priority

func (s *SuperTokensMiddlewareRegistry) Priority() int

Jump to

Keyboard shortcuts

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