App Logo
Get Started

Basic Usage

Email & Password + Sessions

Standalone Mode

config.toml
# -- EMAIL PLUGIN --

[plugins.email]
enabled = true
provider = "smtp"  # Options: "smtp", "resend"
# If using Resend, make sure to set RESEND_API_KEY environment variable.
# Can also be set via FROM_ADDRESS environment variable
from_address = "noreply@example.com"

# -- EMAIL & PASSWORD PLUGIN --

[plugins.email_password]
enabled = true
min_password_length = 8
max_password_length = 32
disable_sign_up = false
require_email_verification = true
auto_sign_in = true
send_email_on_sign_up = true

# -- SESSION PLUGIN --

[plugins.session]
enabled = true

# -- ROUTE MAPPINGS --

[[route_mappings]]
path = "/me"
method = "GET"
plugins = ["session.auth"]

[[route_mappings]]
path = "/sign-in"
method = "POST"
plugins = ["session.auth.optional"]

[[route_mappings]]
path = "/sign-up"
method = "POST"
plugins = ["session.auth.optional"]

[[route_mappings]]
path = "/sign-out"
method = "POST"
plugins = ["session.auth"]

Library Mode

main.go
package main

import (
	"log"
	"net/http"

	"github.com/joho/godotenv"

	authula "github.com/Authula/authula"
	authulaconfig "github.com/Authula/authula/config"
	authulamodels "github.com/Authula/authula/models"
	emailplugin "github.com/Authula/authula/plugins/email"
	emailpasswordplugin "github.com/Authula/authula/plugins/email-password"
	emailpasswordplugintypes "github.com/Authula/authula/plugins/email-password/types"
	emailplugintypes "github.com/Authula/authula/plugins/email/types"
	sessionplugin "github.com/Authula/authula/plugins/session"
)

func main() {
	_ = godotenv.Load(".env")

	config := authulaconfig.NewConfig(
		authulaconfig.WithAppName("YourAppName"),
		authulaconfig.WithDatabase(authulamodels.DatabaseConfig{
			Provider: "sqlite",
			URL:      "app.db",
		}),
		// Or for PostgreSQL
		// authulaconfig.WithDatabase(authulamodels.DatabaseConfig{
		//		Provider:         "postgres",
		//		ConnectionString: os.Getenv("DATABASE_URL"),
		// }),
		authulaconfig.WithRouteMappings(
			[]authulamodels.RouteMapping{
				{
					Path:    "/me",
					Method:  "GET",
					Plugins: []string{"session.auth"},
				},
				{
					Path:    "/sign-out",
					Method:  "POST",
					Plugins: []string{"session.auth"},
				},
			},
		),
	)

	auth := authula.New(&authula.AuthConfig{
		Config: config,
		Plugins: []authulamodels.Plugin{
			emailplugin.New(emailplugintypes.EmailPluginConfig{
				Enabled:     true,
				Provider:    emailplugintypes.ProviderSMTP,
				// Or if using Resend, make sure to set RESEND_API_KEY environment variable.
				// Provider: emailplugintypes.ProviderResend,
				FromAddress: "YourApp@YourDomain.com",
			}),
			emailpasswordplugin.New(emailpasswordplugintypes.EmailPasswordPluginConfig{
				Enabled:                  true,
				MinPasswordLength:        8,
				MaxPasswordLength:        32,
				DisableSignUp:            false,
				RequireEmailVerification: true,
				AutoSignIn:               true,
				SendEmailOnSignUp:        true,
			}),
			sessionplugin.New(sessionplugin.SessionPluginConfig{
				Enabled: true,
			}),
		},
	})

	// Start the server and mount the handler
	log.Fatal(http.ListenAndServe(":8080", auth.Handler()))
}

.env
 SMTP_HOST="localhost"
 SMTP_PORT="465"
 SMTP_USER="YourUser@YourDomain.com"
 SMTP_PASS="YourPassword"

Testing the Setup

Sign Up

curl -X POST http://localhost:8080/auth/sign-up \
  -H "Content-Type: application/json" \
  -d '{"name": "John Doe", "email": "john.doe@example.com", "password": "password123"}'

Sign In

curl -X POST http://localhost:8080/auth/sign-in \
  -H "Content-Type: application/json" \
  -d '{"email": "john.doe@example.com", "password": "password123"}'

Get Session

curl -X GET http://localhost:8080/auth/me \
  -H "Cookie: authula.session_token=your-session-token"

On this page