SuperTokens

SuperTokens

  • Docs
  • Discord
  • Blog

›Sessions

SIDEBAR_REPLACE_DOC_Introduction

  • Introduction

Quick setup

  • Video tutorial & Architecture
  • Frontend
  • Backend
  • Core

    • Core Overview
    • Self Hosted setup with Docker
    • Self Hosted setup without Docker
    • Managed Service

    Database Setup

    • MySQL
    • PostgreSQL

Common customizations

  • Sign Out
  • Sign Up Form

    • About
    • Adding Extra Fields
    • Adding / Modifying field validators
    • Embed in a page
    • Handling signup success
    • Terms of service & Privacy policy links

    Sign In Form

    • About
    • Adding / Modifying field validators
    • Password managers
    • Embed in a page
    • Show Sign In by default

    Reset Password

    • About
    • Reset password email
    • Embed in a page

    Email Verification

    • About
    • Customising the email sent
    • Embed in a page

    Sessions

    • About
    • Cookie Consent
    • Creating a new session
    • Session Verification in API
    • Change session timeout
    • Checking if a session exists on the frontend
    • Get user information on the frontend
    • Using with FaunaDB

    Styling

    • Changing Colours
    • Changing Style via CSS
    • Themes

    Changing base path

    • Website Base Path
    • API Base Path

    Multi Tenancy

    • About
    • One login, many sub domains
    • One login per sub domain
  • User Pagination

Advanced users

    Advanced session management

    • Share sessions across sub domains
    • Anti CSRF
    • JWT Signing key rotation
    • Access token blacklisting
    • Customizing Error Handling

    Supertokens Core config

    • Adding API Keys
    • Tuning Performance
    • Logging
    • Rename database tables

    Make your own frontend

    • Sign-up / Sign-in custom theme
    • Reset password custom theme

    Make your own backend

    • Sign up custom API
    • Sign in custom API
    • Reset password custom APIs

NextJS

  • SuperTokens with NextJS
  • Deploy with Vercel
  • Deploy with Netlify

SIDEBAR_REPLACE_DOC_SDKs

  • SDKs

SIDEBAR_REPLACE_DOC_Compatibility Table

  • Compatibility Table

Migration

  • Migrating from an older version of SuperTokens
  • Migrating to SuperTokens
  • Migrating away from SuperTokens
  • From managed service to self hosted

Using with FaunaDB

This integration only works if you have stored your users in FaunaDB. So, in case you are using Auth0, Okta, or store your users outside of FaunaDB, you will need to wait for our integration to support it.

SuperTokens provides an integration with FaunaDB that allows you to:

  • Create a Fauna token for a user who just logged in
  • Access the Fauna user token on your frontend client and backend APIs, so that you can query FaunaDB from anywhere
  • Securely refresh the session and Fauna user token automatically
  • Automatically revoke the Fauna user token when the session associated with that user is revoked.

This integration is only available for NodeJS and ReactJS as of now. If you would like additional tech stack support, please open an issue on our Github.

Integration

1️⃣ Complete the Quick setup guide

  • Make sure you have completed the frontend, backend and SuperTokens core setup.
  • If you intend to only use session management from SuperTokens, you do not need to call EmailPassword.init() in the recipeList array on the frontend and backend.

2️⃣ Change import statements on the backend

Replace require("supertokens-node/recipe/session") with

require("supertokens-node/recipe/session/faunadb")

3️⃣ Add FaunaDB options to the Session.init() function

let supertokens = require("supertokens-node");
let Session = require("supertokens-node/recipe/session/faunadb");

supertokens.init({
    supertokens: {...},
    appInfo: {...},
    recipeList: [
        Session.init({
            faunadbSecret: "FAUNA DB SECRET",
            userCollectionName: "COLLECTION NAME",
            accessFaunadbTokenFromFrontend: false
        })
    ]
});

4️⃣ Creating a new session

On login, you would want to create a new session using the "FaunaDB reference ID" of the logged in user.

let Session = require("supertokens-node/recipe/session/faunadb");

app.post("/login", async function (req, res) {
    // check for user credentials..

    let userId = "<FAUNADB REFERENCE ID>";
    await Session.createNewSession(res, userId);
    res.send("logged in");
});

5️⃣ Retrieve the Fauna user token in any API

After session verification, you can use the session.getFaunadbToken() function in the API

let Session = require("supertokens-node/recipe/session/faunadb");

app.post("/like-comment", Session.verifySession(), function (req, res) {
    let userId = req.session.getUserId();
    let faunaToken = await req.session.getFaunadbToken();

    // query FaunaDB on behalf of the currently logged in user.
    
    res.send(userId);
});

6️⃣ Retrieve the Fauna user token on the frontend

In order to do this, you will need to set accessFaunadbTokenFromFrontend to true when calling Session.init on the backend.

Then on the frontend, once a user logs in, you can retrieve the JWT payload and use the key "faunadbToken" to read the token. Here is an example

// **This is on your frontend**

import Session from 'supertokens-auth-react/recipe/session';

let jwtPayload = await Session.getJWTPayloadSecurely();
let faunadbToken = jwtPayload["faunadbToken"];

// query FaunaDB...
← Get user information on the frontendChanging Colours →