Skip to main content

Creating sessions

import express from "express"
import { buildSchema } from 'graphql';
import { graphqlHTTP } from 'express-graphql';
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { SessionRequest } from "supertokens-node/framework/express";
import Session from "supertokens-node/recipe/session";

// ...

let app = express();

var GraphQLSchema = buildSchema(`
type Mutation {
login(email: String, password: String): String
}
`);

function getResolvers(req: any, res: any) {
return {
login: async ({ email, password }: { email: string, password: string }) => {
return new Promise(async (resolve, reject) => {
try {
// TODO: Validate credentials
let userIdentifier = "userId"
let session = await Session.createNewSession(req, res, userIdentifier);
resolve("Logged In");
} catch (e) {
reject(e);
}
});
},
};
}

app.use("/graphQL", verifySession({ sessionRequired: false }), graphqlHTTP(async (req, res) => {
return {
schema: GraphQLSchema,
rootValue: getResolvers(req, res),
context: {
session: (req as SessionRequest).session,
}
};
}))
Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react