Skip to content

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

35 Commits

Folders and files

Repository files navigation

Stytch Authentication from Gleam

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 Hex Docs 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 Hex Docs
  • 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.

Steps to set up authentication (magic link)

  1. Sign up for Stytch
  2. Create a backend for your gleam repository (e.g. wisp) Follow Lustre Full Stack Guide if you need assistance.
  3. Add a .env file 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.
  4. Add routes for send_sign_in_link, authenticate, me, and sign_out using session token authentication. JWT might work; I haven't tested yet. Submit a PR updating this bullet if you do! For passkey support, additionally add start_register_passkey, finish_register_passkey, start_passkey_login, passkey_login, and delete_passkey routes.
  5. For each route (See example routes):
    1. Decode json payload
    2. Construct a Stytch client
    3. Call the appropriate function in stytch_client
    4. Process the response, handling errors appropriately
    5. Return appropriate response to frontend.
  6. Create a frontend for your gleam repository, probably using Lustre.
  7. Hook up auth model and views as shown in examples.
  8. Do something with those unstyled auth views.
  9. Run gleam run -m lustre/dev build --outdir=../server/priv/static in client package.
  10. Run gleam run in server package.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages