This repo contains two gleam projects to aid in accessing the Stytch authentication service. Stytch is a developer friendly service providing headless (and nearly-headless) authentication APIs.
Magic link, one time passcode, and passkey (WebAuthn) auth are supported. I am open to PRs for other Stytch APIs.
There are three separate gleam packages:
- stytch_codecs contains types sent to or returned from
Stytch, along with json encoders and gleam decoders. I made it a separate
package so the types can be used in JavaScript frontends that call your API.
See
for documentation.
- stytch_client contains strongly typed Gleam functions
that call the stytch API. It is intended to be called from your backend.
API is documented on
- stytch_ui_model contains lustre model types and update functions for managing your authenticated app in the frontend. It calls specific urls your backend must implement to manage state. This package has not been released to Hex yet as I am uncomfortable with the devex.
In addition, there is an example folder that contains fully working demos of magic link and passcode auth, as well as an example of how auth and modem routes can live together.
- Sign up for Stytch
- Create a backend for your gleam repository (e.g. wisp) Follow Lustre Full Stack Guide if you need assistance.
- Add a
.envfile with three keys:- SECRET_KEY_BASE: random 64 character string (e.g. from
wisp.random_string) - STYTCH_PROJECT_ID: from Stytch dashboard
- STYTCH_SECRET: from Stytch dashboard
- STYTCH_DOMAIN (optional): the WebAuthn relying-party domain for passkeys.
Defaults to
localhost. Set it to your app's domain in production.
- SECRET_KEY_BASE: random 64 character string (e.g. from
- Add routes for
send_sign_in_link,authenticate,me, andsign_outusing session token authentication. JWT might work; I haven't tested yet. Submit a PR updating this bullet if you do! For passkey support, additionally addstart_register_passkey,finish_register_passkey,start_passkey_login,passkey_login, anddelete_passkeyroutes. - For each route (See example routes):
- Decode json payload
- Construct a Stytch client
- Call the appropriate function in stytch_client
- Process the response, handling errors appropriately
- Return appropriate response to frontend.
- Create a frontend for your gleam repository, probably using Lustre.
- Hook up auth model and views as shown in examples.
- Do something with those unstyled auth views.
- Run
gleam run -m lustre/dev build --outdir=../server/priv/staticin client package. - Run
gleam runin server package.