Add Auth Middleware
Learn how to create and configure an authentication middleware instance to secure your Go application.
Create an authentication middleware instance to protect your application endpoints. Later in this guide, you'll apply that middleware to your Go handlers to enforce authentication on incoming requests.
Create Auth Middleware
Create an authentication middleware using the goauth.Middlewarestype in your project (for example, in your main.go file). Apply app.RequireAuthenticationto any http.Handler that requires a valid authenticated session.
This middleware retrieves the session from your configured SessionManager, verifies that the access token is valid, refreshes the token when necessary, and stores the session in the request context for downstream handlers. If no valid session exists, the request returns a 401 Unauthorized response. If token refresh fails, the user is redirected to the login URL.
// main.go
package main
import (
"github.com/wristband-dev/go-auth"
)
func main() {
cfg := goauth.NewAuthConfig(
"<WRISTBAND_CLIENT_ID>",
"<WRISTBAND_CLIENT_SECRET>",
"<WRISTBAND_APPLICATION_VANITY_DOMAIN>",
)
wristbandAuth, err := cfg.WristbandAuth()
if err != nil {
// Handle error
}
sessionManager := NewSessionStore("<your-generated-secret>", true)
app := wristbandAuth.NewApp(sessionManager)
// ADD: Create middleware chain for protected endpoints
authMiddlewares := goauth.Middlewares{
app.RequireAuthentication
}
}Updated 8 days ago
What’s Next
Next, you'll use the Wristband SDK to create the necessary authentication endpoints in your Go server.