SuperTokens Golang SDK

repository·master·Indexed 41 days ago

https://github.com/supertokens/supertokens-golang

A 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.

Tokens
17.7K
Snippets
79
Records
87
Agent score
100%

What's inside SuperTokens

  1. Overview of SuperTokens Golang SDK

    master
    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.
  2. Access session information in Twirp APIs

    master

    When using verifySession in optional mode (SessionRequired: false), you can access the SessionContainer object within your API handlers to check for an active session.

    Because the session is optional, you must check if the SessionContainer object is nil before attempting to use it. If it is not nil, you can retrieve information such as the userId or 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
    }
  3. Integrate SuperTokens with Twirp

    master

    To integrate SuperTokens with a Twirp server, you must wrap your Twirp server with SuperTokens middleware and handle session verification.

    1. Middleware Setup: Use supertokens.Middleware(verifySession(twirpServer)) within your CORS configuration. This exposes SuperTokens APIs (like sign-up and sign-in) to the frontend.
    2. Session Verification Mode: Use verifySession in optional mode by setting SessionRequired to false. This allows all requests to pass through session verification without enforcing a session. If a session is present, a SessionContainer object is provided; otherwise, it is nil.
    3. 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 401 reply 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)))
  4. Initialize the Session recipe

    master

    To use the Session recipe, you must initialize it using the Init function. This returns a supertokens.Recipe instance 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
    })
  5. Initialize the Email Verification recipe

    master

    Use Init to register the Email Verification recipe with your SuperTokens instance. It requires a configuration object of type evmodels.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.Recipe
  6. Initialize the Passwordless recipe

    master

    To use the Passwordless recipe, call Init with a configuration object of type plessmodels.TypeInput. This returns a supertokens.Recipe instance 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
    })
  7. Integrate Oso with SuperTokens using Go-Chi

    master

    This example demonstrates how to combine SuperTokens with Oso (an authorization framework) within a Go application using the go-chi router. It provides a pattern for managing access rights alongside SuperTokens authentication.

    Note: The example uses a file database/database.go which contains pre-configured emails with different access rights (sourced from the Oso quickstarter) to facilitate testing authorization logic.

    go run main.go