SuperTokens

SuperTokens

  • Docs
  • Discord
  • Blog

›SIDEBAR_REPLACE_DOC_With FaunaDB

SIDEBAR_REPLACE_DOC_About this recipe

  • About this recipe

Quick setup

  • Frontend
  • Backend
  • Core

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

    Database Setup

    • If using MySQL
    • If using PostgreSQL
    • If using MongoDB
    • Rename database tables

SIDEBAR_REPLACE_DOC_About appInfo

  • About appInfo

Common customizations

  • Creating a new session
  • Session Verification in API
  • Sign Out
  • Revoking a session manually
  • Change session timeout
  • Checking if a session exists on the frontend
  • Get user information on the frontend
  • Handling session expiry
  • Securing a website route / component
  • Fetching sessions for a user
  • Update JWT Payload
  • Update Session Data
  • Session and user roles

    • Assigning roles to a session
    • Reading roles in an API
    • Reading roles in the frontend
    • Updating roles in a session
  • Multiple API endpoints
  • Cookies and Https
  • Cookie Consent
  • Share sessions across sub domains
  • Anti CSRF
  • Same site cookies
  • JWT Signing key rotation
  • Access token blacklisting
  • Using in an iframe
  • Customizing Error Handling
  • Changing base path

    • Website Base Path
    • API Base Path

    Core

    • Adding API Keys
    • Tuning Performance
    • Logging

    Core CLI

    • Overview
    • Start
    • List
    • Stop
    • Uninstall

Advanced customizations

  • Overview
  • Frontend functions override

    • About
    • How to use

    Backend functions override

    • About
    • How to use

    APIs override

    • About
    • How to use
    • Disabling APIs

    Frontend hooks

    • Pre API Hook
    • Handle Event Hook

SIDEBAR_REPLACE_DOC_With FaunaDB

  • Using with FaunaDB

Serverless Deployment

    With Netlify

    • About
    • 1. Frontend Setup
    • 2. Backend config
    • 3. Exposing Auth APIs
    • 4. Session verification / Building your APIs
    • 5. Next steps

    With AWS Lambda

    • About
    • 1. Frontend Setup
    • 2. Backend config
    • 3. Exposing Auth APIs
    • 4. Setting Up API Gateway & CORS
    • 5. Session verification / Building your APIs
    • 6. Next steps

Testing

  • Testing with Postman

SIDEBAR_REPLACE_DOC_SDK Reference

  • SDK Reference

SIDEBAR_REPLACE_DOC_API Reference

  • API Reference

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.

Integration

1️⃣ Complete the Quick setup guide

  • Make sure you have completed the frontend, backend and SuperTokens core setup.

2️⃣ Use the override config in Session.init()

NodeJS
let supertokens = require("supertokens-node");
let Session = require("supertokens-node/recipe/session");
let { RecipeImplementation } = require("supertokens-node/recipe/session/faunadb");

supertokens.init({
supertokens: {...},
appInfo: {...},
recipeList: [
Session.init({
...
override: {
functions: (originalImplementation) => {
return new RecipeImplementation(originalImplementation, {
userCollectionName: "users",
accessFaunadbTokenFromFrontend: true,
faunaDBClient: new faunadb.Client({
secret: "<SECRET>",
}),
});
},
},

}),
]
});

3️⃣ Creating a new session

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

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

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");
});

4️⃣ Retrieve the Fauna user token in any API

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

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

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);
});

If using TypeScript, the type of req.session is SessionContainer, imported like import {SessionContainer} from "supertokens-node/recipe/session/faunadb"

5️⃣ 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

With ReactJS
Plain JS

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

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

// query FaunaDB...

import SuperTokens from 'supertokens-website';

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

// query FaunaDB...
← Handle Event HookAbout →