This example application demonstrates how one may use Stytch within a Vanilla JavaScript application. There is a wide ecosystem of tools for building and serving JavaScript web apps each with their own implementation style and flavor. For that reason, this example is unopinionated and does not use additional tooling other than HTML, CSS, and our Vanilla JavaScript SDK. The entire application is served out of the public
directory using http-server
.
This project uses Stytch's JavaScript SDK which provides pre-built UI components and headless methods to securely interact with Stytch.
This application features Email Magic Links for authentication, as well as our Sessions Management product to manage user sessions. You can use this application's source code as a learning resource, or use it as a jumping off point for your own project. We are excited to see what you build with Stytch!
Follow the steps below to get this application fully functional and running using your own Stytch credentials.
-
Create a Stytch account. Once your account is set up a Project called "My first project" will be automatically created for you.
-
Within your new Project, navigate to SDK configuration, and make the following changes:
-
Navigate to Redirect URLs, and add
http://localhost:3000
as the types Login and Sign-up. -
Finally, navigate to API Keys, and copy your
public_token
. You will need this value later on.
In your terminal clone the project and install dependencies:
git clone https://github.com/stytchauth/stytch-javascript-example.git
cd stytch-javascript-example
npm i
Next, open the file public/js/app.js
and replace the value assigned to the constant STYTCH_PUBLIC_TOKEN
with your public_token
.
const STYTCH_PUBLIC_TOKEN =
'public-token-test-123abcd-1234-1234-abcd-123123abcabc';
After completing all the set up steps above the application can be run with the command:
npm run dev
The application will be available at http://localhost:3000
.
You'll be able to login with Email Magic Links and see your Stytch User object, Stytch Session, and see how logging out works.
This example app showcases a small portion of what you can accomplish with Stytch. Here are a few ideas to explore:
- Add additional login methods like Passwords.
- Replace the pre-built UI with your own using by using the SDK's headless methods.
- Add a Google OAuth button with the high converting Google One Tap UI.
- Secure your app further by building MFA authentication using methods like WebAuthn.
- Use Stytch Sessions to secure your backend.
Join the discussion, ask questions, and suggest new features in our Slack community!
Check out the Stytch Forum or email us at [email protected].