SuperTokens Golang SDK
repository·master·Indexed 41 days ago
https://github.com/supertokens/supertokens-golangA client-side interface for Go applications to interact with the SuperTokens HTTP service for secure authentication and session management. It includes integrations for web frameworks such as Gin-Gonic, Fiber, Echo, Gorilla Mux, go-chi, go-zero, and net/http, as well as support for Twirp servers and authorization frameworks like Oso.
What's inside SuperTokens
- The SuperTokens Golang SDK is a library designed to facilitate communication between a Go-based API process and the SuperTokens HTTP service. It allows developers to integrate SuperTokens authentication and session management capabilities into their Go applications.
Access session information in Twirp APIs
masterWhen using
verifySessionin optional mode (SessionRequired: false), you can access theSessionContainerobject within your API handlers to check for an active session.Because the session is optional, you must check if the
SessionContainerobject isnilbefore attempting to use it. If it is notnil, you can retrieve information such as theuserIdor session data, or perform actions like revoking the session.// Conceptual pattern for checking session in an API handler if sessionContainer != nil { // Access userId or session data userId := sessionContainer.UserId // Or revoke session }Integrate SuperTokens with Twirp
masterTo integrate SuperTokens with a Twirp server, you must wrap your Twirp server with SuperTokens middleware and handle session verification.
- Middleware Setup: Use
supertokens.Middleware(verifySession(twirpServer))within your CORS configuration. This exposes SuperTokens APIs (like sign-up and sign-in) to the frontend. - Session Verification Mode: Use
verifySessionin optional mode by settingSessionRequiredtofalse. This allows all requests to pass through session verification without enforcing a session. If a session is present, aSessionContainerobject is provided; otherwise, it isnil. - Error Handling: Implement a Twirp request interceptor to catch errors thrown by SuperTokens functions (e.g., when retrieving session info). The interceptor should catch these errors and return a
401reply to the client.
To run the provided example server, use:
go run cmd/server/main.go// Example of the middleware pattern used in the integration CORS(supertokens.Middleware(verifySession(twirpServer)))- Middleware Setup: Use
Initialize the Session recipe
masterTo use the Session recipe, you must initialize it using the
Initfunction. This returns asupertokens.Recipeinstance that must be part of your SuperTokens configuration.import "github.com/supertokens/supertokens-golang/recipe/session" import "github.com/supertokens/supertokens-golang/recipe/session/sessmodels" // Inside your SuperTokens initialization session.Init(&sessmodels.TypeInput{ // configuration options here })Initialize the Email Verification recipe
masterUse
Initto register the Email Verification recipe with your SuperTokens instance. It requires a configuration object of typeevmodels.TypeInput.import "github.com/supertokens/supertokens-golang/recipe/emailverification" // config should be of type evmodels.TypeInput emailVerificationRecipe := emailverification.Init(config)func Init(config evmodels.TypeInput) supertokens.RecipeInitialize the Passwordless recipe
masterTo use the Passwordless recipe, call
Initwith a configuration object of typeplessmodels.TypeInput. This returns asupertokens.Recipeinstance that must be registered during the SuperTokens initialization process.import "github.com/supertokens/supertokens-golang/recipe/passwordless" // ... inside your SuperTokens initialization passwordless.Init(plessmodels.TypeInput{ // configuration options here })Run the go-zero and SuperTokens integration example
masterTo run the demonstration server that integrates the
go-zeroframework with SuperTokens, use the standard Go run command from the root of the example directory.go run main.goIntegrate Gorilla Mux with SuperTokens
masterThis example demonstrates how to integrate the Gorilla Mux router with the SuperTokens Golang SDK. It provides a template for setting up authentication and session management within a Gorilla Mux-based web server.
go run main.goRun the Go-Chi and SuperTokens integration example
masterTo execute the example server demonstrating the integration between Go-Chi and SuperTokens, navigate to the example directory and use the Go CLI to run the main entry point.
go run main.goRun the Labstack Echo integration example
masterTo run the example demonstrating how to integrate Labstack Echo with SuperTokens, navigate to the example directory and use the Go CLI to execute the main entry point.
go run main.goIntegrate SuperTokens with Fiber
masterThis example demonstrates how to integrate the SuperTokens Golang SDK with the Fiber web framework. To run the provided integration example, navigate to the directory and use the Go CLI.
go run main.goIntegrate Oso with SuperTokens using Go-Chi
masterThis example demonstrates how to combine SuperTokens with Oso (an authorization framework) within a Go application using the
go-chirouter. It provides a pattern for managing access rights alongside SuperTokens authentication.Note: The example uses a file
database/database.gowhich contains pre-configured emails with different access rights (sourced from the Oso quickstarter) to facilitate testing authorization logic.go run main.go