Sign in with STX
Describes github.com/stxapp/stx-go v0.3.1, the current release.
Sign in with STX is OpenID Connect on top of the app flow: your server learns who the member is from a verified ID token and, if you ask for member scopes, gets their grant in the same round trip. How it works is described in Sign in with your STX account; this page shows the SDK calls. It runs on your server only: the secret and the tokens never belong in a browser.
signIn, err := oauth.NewSignIn(oauth.SignInConfig{ Issuer: "https://demo.stxapp.io", ClientID: "stxapp_client_9f2c41d7", ClientSecret: os.Getenv("STX_OAUTH_CLIENT_SECRET"), RedirectURL: "https://sideline.example.com/auth/stx/callback",})if err != nil { log.Fatal(err)}Start and finish
Section titled “Start and finish”Start returns the URL to send the member to and a SavedSignIn to keep in their server session; Finish on the callback checks state, exchanges the code and verifies the ID token against the nonce.
var sessions sync.Map // your session store
http.HandleFunc("/auth/stx", func(w http.ResponseWriter, r *http.Request) { started, err := signIn.Start(r.Context(), oauth.StartParams{}) if err != nil { http.Error(w, err.Error(), http.StatusBadGateway) return } sessions.Store(r.RemoteAddr, started.Saved) http.Redirect(w, r, started.URL, http.StatusFound)})
http.HandleFunc("/auth/stx/callback", func(w http.ResponseWriter, r *http.Request) { saved, ok := sessions.LoadAndDelete(r.RemoteAddr) if !ok { http.Error(w, "sign-in expired", http.StatusBadRequest) return } done, err := signIn.Finish(r.Context(), r.URL.Query(), saved.(oauth.SavedSignIn)) if errors.Is(err, oauth.ErrAccessDenied) { fmt.Fprint(w, "Sign-in canceled.") return } if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } fmt.Fprintf(w, "Signed in as %s (%s)", done.Identity.Subject, done.Identity.Email)})Key your user record on Identity.Subject, the member’s stable id for your app, not on the email. SavedSignIn marshals to JSON, so it fits a cookie-backed or database session.
StartParams options: Connection goes straight to a sign-in provider ("google", "apple" or "x"), LoginHint pre-fills the member’s email, Scopes asks for member scopes as well, and Prompt: "none" reconnects silently when the member is already signed in.
Tokens after sign-in
Section titled “Tokens after sign-in”done.Token holds the access token and, when granted, a refresh token. signIn.Refresh renews it, signIn.UserInfo reads the member’s claims again, and signIn.Disconnect revokes the grant when the member disconnects your app.
identity, err := signIn.UserInfo(ctx, "stxapp_at_4f1b9c2d", "c0a8f6e2-6d1e-4c43-9a57-2f1d9a3e8b10")if err != nil { log.Fatal(err)}fmt.Println(identity.Email, identity.EmailVerified) // member@example.com trueWith a login service
Section titled “With a login service”When STX is a login option inside a service such as Auth0 or Clerk, the service runs the sign-in, so your server does not call Start or Finish. Enter the settings from Login services in the service; signIn.Configuration and app.Metadata read the same values from the exchange.
To act for the member, your server needs the STX tokens the service received. Store the refresh token under your own user id and use MemberClient: the first call refreshes and stores the new pair. STX rotates the refresh token on every refresh, so from then on only your server refreshes.
err := tokens.Set(ctx, "u_1842", &oauth.Token{RefreshToken: "stxapp_rt_7d2e9a41", Expiry: time.Now()})if err != nil { log.Fatal(err)}member, err := app.MemberClient(tokens, "u_1842", nil)if err != nil { log.Fatal(err)}balance, err := member.Account.Balance(ctx)if err != nil { log.Fatal(err)}fmt.Println(balance.AvailableBalance) // 746.8600If the service does not pass the STX tokens on, keep your login and link STX accounts from your server; see Connect a member’s account.

