Consumer Authentication

/

Quickstarts

/

Quickstarts

/

Node

Node Quickstart

This quickstart guide outlines the steps to integrate Stytch’s Consumer Authentication product into a Node application. For this example we’ll be using Express, but Stytch’s Node SDK is framework agnostic.

Overview

Stytch offers developers a Node SDK that can be used in server-side applications. This guide will walk your through initial set up of a login or create flow using Email Magic Links. Want to skip straight to the source code? Check out an example app here.

Getting Started

1
Install Stytch SDK and configure your API Keys

Create a Stytch Consumer Project in your Stytch Dashboard if you haven't already.

Install our Node SDK in your Express environment:

npm install stytch

Configure your Stytch Project's API keys as environment variables:

STYTCH_PROJECT_ID="YOUR_STYTCH_PROJECT_ID"
STYTCH_SECRET="YOUR_STYTCH_PROJECT_SECRET"
# Use your Project's 'test' or 'live' credentials

2
Set up your Express app

Initialize the Stytch client in your Express app and set up routes to handle authentication:

const express = require('express');
const stytch = require('stytch');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));

const stytchClient = new stytch.Client({
  project_id: process.env.STYTCH_PROJECT_ID,
  secret: process.env.STYTCH_SECRET,
  env: stytch.envs.test,
});

app.post('/login_or_create', (req, res) => {
  const email = req.body.email;

  stytchClient.magicLinks.email.loginOrCreate({
    email: email
  })
  .then(response => {
    res.json(response)
  })
  .catch(err => {
    res.status(500).send(err.toString())
  });
});

app.get('/authenticate', (req, res) => {
  const token = req.query.token;
  stytchClient.magicLinks.authenticate({
    token: token
  })
  .then(response => {
    res.send(`Hello, ${response.user.emails[0].email}!`)
  })
  .catch(err => {
    res.status(401).send(err.toString())
  });
});

app.listen(3000, () => console.log('Server running on http://localhost:3000'));

For Email Magic Links, you must specify a redirect URL in your Project's Dashboard to authenticate the token. By default, the redirect URL is set to http://localhost:3000/authenticate.

You can specify additional Redirect URLs in your Project's Dashboard, and override the default by passing in an explicit signup_magic_link_url or login_magic_link_url argument.

You can read more about redirect URLs in this guide.

3
Test your application

Run your Express application and send a POST request to the /login_or_create endpoint with an email address to test the login or create auth flow. You will recieve an email in your inbox with an Email Magic Link, which redirects you to the /authenticate endpoint or the redirect URL you set in your Dashboard.

4
What's next

Check out our product-specific guides for how to handle full authentication flows for each product you'd like to support, like Email Magic Links and OAuth.