Adapters
Integrate with popular Go HTTP routers and frameworks using Authula's adapters.
Overview
Authula provides adapters for some popular Go HTTP routers and frameworks, allowing you to easily integrate authentication into your applications. These adapters are designed to work seamlessly with the Authula handler, making it easy to mount the authentication routes on your preferred router or framework. Some have adapters and others can be used directly with the handler without needing an adapter.
Note: When using adapters, any custom routes you create outside the Authula handler won't have access to the Authula hooks system. To make use of hooks on your custom routes, use the method
auth.RegisterCustomRoute().
Standard Library (net/http)
The following example shows how to use the Authula handler with the standard library's net/http package. This doesn't require an adapter and can be used directly with the handler provided by Authula.
package main
import (
"log"
"net/http"
authula "github.com/Authula/authula"
authulaconfig "github.com/Authula/authula/config"
authulamodels "github.com/Authula/authula/models"
)
func main() {
config := authulaconfig.NewConfig(/*...*/)
auth := authula.New(&authula.AuthConfig{
Config: config,
Plugins: []authulamodels.Plugin{},
})
// Mount handler
http.Handle("/api/auth/", auth.Handler())
// Start the server and mount the handler
log.Fatal(http.ListenAndServe(":8080", nil))
}Chi
Authula already uses Chi under the hood. The following example shows how to add more routes to Authula. This doesn't require an adapter and can be used directly with the handler provided by Authula.
package main
import (
"log"
"net/http"
authula "github.com/Authula/authula"
authulaconfig "github.com/Authula/authula/config"
authulamodels "github.com/Authula/authula/models"
)
func main() {
config := authulaconfig.NewConfig(/*...*/)
auth := authula.New(&authula.AuthConfig{
Config: config,
Plugins: []authulamodels.Plugin{},
})
// Add a custom route. This will be added outside of auth routes such as /api/auth
// allowing you to define your own custom routes.
auth.RegisterCustomRoute(authulamodels.Route{
Method: "GET",
Path: "/api/health",
Metadata: map[string]any{},
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqCtx, _ := authulamodels.GetRequestContext(r.Context())
reqCtx.SetJSONResponse(http.StatusOK, map[string]any{
"status": "ok",
})
}),
})
log.Fatal(http.ListenAndServe(":8080", auth.Handler()))
}Echo
The following example shows how to use Echo to mount the Authula handler. This allows you to easily integrate Authula into your Echo-based applications and doesn't require an adapter since the handler can be used directly.
package main
import (
"log"
"net/http"
"github.com/labstack/echo/v4"
authula "github.com/Authula/authula"
authulaconfig "github.com/Authula/authula/config"
authulamodels "github.com/Authula/authula/models"
)
func main() {
config := authulaconfig.NewConfig(/*...*/)
auth := authula.New(&authula.AuthConfig{
Config: config,
Plugins: []authulamodels.Plugin{},
})
echoInstance := echo.New()
// Mount auth handler
echoInstance.Any("/api/auth/*", echo.WrapHandler(auth.Handler()))
log.Fatal(echoInstance.Start(":8080"))
}Fiber
The following example shows how to use the official Fiber adapter to mount the Authula handler on a Fiber application.
package main
import (
"log"
"github.com/gofiber/fiber/v3"
authula "github.com/Authula/authula"
authulaconfig "github.com/Authula/authula/config"
authulamodels "github.com/Authula/authula/models"
fiberadapter "github.com/Authula/authula/adapters/fiber"
)
func main() {
config := authulaconfig.NewConfig(/*...*/)
auth := authula.New(&authula.AuthConfig{
Config: config,
Plugins: []authulamodels.Plugin{},
})
app := fiber.New()
// Mount Authula handler using the Fiber adapter
app.Use("/api/auth", fiberadapter.New(fiberadapter.Config{
Handler: auth.Handler(),
}))
// Add your custom routes here using app.Get, app.Post, etc.
log.Fatal(app.Listen(":8080"))
}