Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react
Backend Setup
#
a) Complete the setup guidesFollow the frontend and backend pre-built UI setup guides to setup SuperTokens.
#
b) Install GraphQL dependencies on your backendnote
We use express-graphql as an example, SuperTokens can be used with other GraphQL libraries.
npm i express-graphql graphql
#
c) Setup GraphQL#
Create a schemaimport { buildSchema } from 'graphql';
var schema = buildSchema(` type Query { hello: String }`);
#
Create resolversvar resolvers = { hello: () => "Hello World!",}
Refer to the official guide to know more.
#
d) Add a GraphQL endpoint with optional session verificationimport express from "express"import { graphqlHTTP } from 'express-graphql';import { verifySession } from "supertokens-node/recipe/session/framework/express";import { buildSchema } from 'graphql';
var GraphQLSchema = buildSchema(`...`); // Refer to step cvar resolvers = {/* ... */ }; // Refer to step c// ...
let app = express();
/** The verifySession middleware adds the session object to req* { sessionRequired: false } allows access to the endpoint even * when a session does not exist*/app.use("/graphQL", verifySession({ sessionRequired: false }), graphqlHTTP(async (req: any, res) => { return { schema: GraphQLSchema, rootValue: resolvers, // req.session will be undefined if a session does not exist context: { session: req.session, } };}))
Refer to this page to know more about the session object