Docs menu: Quick start

Quick start

Add Vestibule to a Gleam app and wire the OAuth request and callback phases.

Recommended for Wisp and Mist apps

Let middleware own the auth routes

If your app uses Wisp or Mist, start here. Add the core package, one provider strategy, and the middleware package for your server. Use the advanced core path only if your app must control the routes, session storage, and callback handling.

Start here for Wisp or Mist: middleware handles the request and callback routes. First, make this base flow work. Then add more provider packages.

Vestibule packages are not available on Hex. Add them from GitHub with the moving v0 tag. Use Gleam 1.18 or later because companion packages use Git path dependencies. See the installation guide for paths and version information.

[dependencies]
vestibule = { git = "https://github.com/tylerbutler/vestibule.git", ref = "v0" }
vestibule_github = { git = "https://github.com/tylerbutler/vestibule.git", ref = "v0", path = "packages/vestibule_github" }
vestibule_wisp = { git = "https://github.com/tylerbutler/vestibule.git", ref = "v0", path = "packages/vestibule_wisp" }

Route request and callback phases

Register the strategies once. Initialize the state store once. Then route the request and callback paths to the middleware. For Wisp, supply a stable client key from the direct peer or a trusted edge. Do not derive it from unvalidated forwarded headers.

What to notice: the request route stores temporary callback data and starts the redirect. The callback route returns an auth result only after state and PKCE validation.

These route examples use GitHub to show the flow. Use your provider’s setup guide to replace the strategy and client configuration.

Wisp route setup

import gleam/http
import wisp
import vestibule/config
import vestibule/registry
import vestibule/state_store
import vestibule_wisp
import vestibule_github
let assert Ok(registry) =
registry.new()
|> registry.register(
vestibule_github.strategy(),
config.new(
client_id: "client_id",
redirect_uri: "http://localhost:8000/auth/github/callback",
auth: config.client_secret_auth("client_secret"),
),
)
let assert Ok(store) = state_store.create()
// Supply client_key from the direct peer or a trusted edge.
// Do not use unvalidated Forwarded or X-Forwarded-For headers.
case wisp.path_segments(req), req.method {
["auth", provider], http.Get ->
vestibule_wisp.request_phase_for_client(
req,
registry: registry,
provider: provider,
state_store: store,
authorize_options: config.authorize_options(),
client_key: client_key,
)
["auth", provider, "callback"], http.Get
| ["auth", provider, "callback"], http.Post ->
vestibule_wisp.callback_phase(req, registry, provider, store, fn(auth) {
// The response wrapper handles the in-flight cookie lifecycle.
// Map auth.uid(auth) to an account and start your own session.
start_session(auth)
})
_, _ ->
wisp.not_found()
}
Mist route setup

Use the Mist middleware package instead of the Wisp middleware package.

[dependencies]
vestibule = { git = "https://github.com/tylerbutler/vestibule.git", ref = "v0" }
vestibule_mist = { git = "https://github.com/tylerbutler/vestibule.git", ref = "v0", path = "packages/vestibule_mist" }
vestibule_github = { git = "https://github.com/tylerbutler/vestibule.git", ref = "v0", path = "packages/vestibule_github" }

Load a high-entropy secret key base from configuration. Create the options and state store once, then use them in your Mist handler.

import gleam/http
import gleam/http/request.{type Request}
import gleam/http/response.{type Response}
import mist.{type Connection, type ResponseData}
import vestibule/config
import vestibule/state_store
import vestibule_mist
let assert Ok(store) = state_store.create()
let assert Ok(options) = vestibule_mist.new_options(secret_key_base)
fn handle_request(http_request: Request(Connection)) -> Response(ResponseData) {
case request.path_segments(http_request), http_request.method {
["auth", provider], http.Get ->
vestibule_mist.request_phase(
http_request,
registry,
provider,
store,
authorize_options: config.authorize_options(),
options: options,
)
["auth", provider, "callback"], http.Get
| ["auth", provider, "callback"], http.Post ->
vestibule_mist.callback_phase(
http_request,
registry,
provider,
store,
options,
on_success,
)
_, _ ->
not_found()
}
}
  • Initialize the state store once. Reuse the store so that middleware can bind callback data to the user flow.

  • Let middleware consume callback data. After state validation, middleware removes the stored values before token exchange. A malformed callback or state mismatch does not remove a valid login session.

  • Map auth.uid(auth) to your own account. Vestibule authenticates the provider identity. Your app manages user accounts and sessions.

Advanced path for custom routingUse core when your app owns the flowChoose this when you handle routing, sessions, and callback storage yourself.

Core returns the authorization URL, state, and PKCE verifier. Your app stores the values and supplies them when it validates the callback.

[dependencies]
vestibule = { git = "https://github.com/tylerbutler/vestibule.git", ref = "v0" }
vestibule_github = { git = "https://github.com/tylerbutler/vestibule.git", ref = "v0", path = "packages/vestibule_github" }

Handle the two phases yourself

Before the redirect, store authorization_request.state(auth_request) and authorization_request.code_verifier(auth_request). Then redirect to authorization_request.url(auth_request). Validate the provider and callback state, then atomically consume the stored flow before calling handle_callback. Pass the consumed values to that function.

What to notice: use an assertion only when you create the authorization request. A callback can be stale or forged. The provider can also reject it. Handle each error result.

import gleam/dict
import gleam/option
import vestibule
import vestibule/authorization_request
import vestibule/config
import vestibule/error
import vestibule_github
let strategy = vestibule_github.strategy()
let client_config =
config.new(
client_id: "client_id",
redirect_uri: "http://localhost:8000/auth/github/callback",
auth: config.client_secret_auth("client_secret"),
)
let options = config.authorize_options()
let assert Ok(auth_request) =
vestibule.create_authorization_request(
strategy,
config: client_config,
options: options,
)
// Store authorization_request.state(auth_request) and
// authorization_request.code_verifier(auth_request) server-side,
// bound to this user's session, with an expiration time.
// Redirect user to authorization_request.url(auth_request).
let params =
dict.from_list([
#("state", "state from callback"),
#("code", "authorization code from callback"),
])
// Validate and atomically consume the stored session before this call.
// Pass the consumed state and verifier; do not restore them after failure.
// Never assert the callback result.
case
vestibule.handle_callback(
strategy,
client_config,
params,
"expected state from session",
"code verifier from session",
expected_nonce: option.None,
)
{
Ok(auth) -> {
// Map auth.uid(auth) to an account and start a session.
sign_in(auth)
}
// Possible CSRF or a stale tab: discard and restart the flow.
Error(err) ->
case error.kind(err) {
error.StateMismatchKind -> restart_sign_in()
// Provider, code-exchange, network, or decode failure.
_ -> show_auth_error(err)
}
}
  • Store state and PKCE verifier server-side before redirecting. Bind the values to the user’s session. Set a short expiration time.

  • Do not reuse stored callback data. Atomically consume the stored values before token exchange. Do not restore them after a failure. Expire unused values quickly. A state mismatch must not delete a valid login session.

  • Never assert the callback result. A stale tab, denied consent, a network error, or a forged state value can occur at run time.

When the callback fails

Many callback failures are harmless. Examples include a stale tab, use of the back button, or a reused link. A forged state value is hostile. Vestibule returns a typed error for each failure, so your app can respond without a crash.

What to notice: stop the current OAuth flow after a failure. Offer a retry only for a temporary upstream error. Never retry a state mismatch with the same callback data.

import gleam/option
import vestibule
import vestibule/error
// Validate and atomically consume the stored flow before this call.
case
vestibule.handle_callback(
strategy,
client_config,
params,
expected_state,
verifier,
expected_nonce: option.None,
)
{
Ok(auth) -> sign_in(auth)
// Classify the failure with error.kind/0; the ErrorKind enum carries an
// OtherKind catch-all, so new kinds never break this match.
Error(err) ->
case error.kind(err) {
// Wrong state: treat as hostile. Log server-side, restart the flow.
error.StateMismatchKind -> restart_sign_in()
// The provider rejected the request. For example, the user denied
// consent. Inspect error.provider_error(err) for the structured details.
error.ProviderKind -> back_to_login()
// Transient upstream failures are worth a retry prompt.
error.NetworkKind | error.CodeExchangeKind -> offer_retry()
// Catch-all: show a generic message, keep error.message(err) in logs.
_ -> show_auth_error(err)
}
}

Why a callback fails

  • StateMismatchKind: The state is wrong or missing. This can indicate CSRF.
  • ProviderKind: The provider rejected the request. For example, the user denied consent.
  • MissingCallbackParamKind: The provider omitted a required value.
  • NetworkKind / CodeExchangeKind: A temporary upstream problem occurred.

How to respond

  • Discard the failed callback. Do not reuse its parameters.
  • Restart the flow after a stale tab, reused link, or state mismatch.
  • Offer a retry only for a temporary network or code-exchange failure.
  • Log the specific error on the server. Show users a generic sign-in error.

Do not expose the error. The reason can contain internal details. Log it on the server and return a generic message to the browser.

Before you demo

Vestibule has not been security audited — use it for demos and prototypes, not production. Even in a demo, complete these checks before you enable sign-in for users. Keep OAuth callback data on the server. Give the data a short expiration time and bind it to the user’s session. Reject missing or mismatched callbacks.

  • Non-local redirect URIs must use HTTPS.
  • Store state and PKCE verifier values on the server for a short time.
  • Remove client secrets, authorization codes, and PKCE verifiers from logs and error reports.
  • A cookie-secret change invalidates OAuth callbacks that are in progress.