Add Auth Middleware
Create an authentication middleware instance needed to secure your Go application.
To protect your application endpoints, you'll need to create an authentication middleware instance. Later in this guide, you'll use that middleware on your Go handlers to enforce authentication on incoming requests.
Create Auth Middleware
Create an authentication middleware using the goauth.Middlewares type in your project (e.g. main.go file). Apply app.RequireAuthentication to any http.Handler that should require a valid authenticated session. This middleware retrieves the session from your configured SessionManager, verifies that the access token is still valid (refreshing it when necessary), and stores the session in the request context for downstream handlers. If no valid session exists, the request will receive a 401 Unauthorized response or be redirected to the login URL if token refresh fails.
// 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>",
goauth.WithDangerouslyDisableSecureCookies(),
)
auth, 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 about 1 hour ago
Next, you'll use the Wristband SDK to create the necessary authentication endpoints in your Go server.