Skip to content

Instantly share code, notes, and snippets.

@lydemann

lydemann/server.ts

Created Dec 28, 2020
Embed
What would you like to do?
server.ts
const verifyToken = async ({ authorization, schoolid }) => {
if (authorization) {
const newToken = authorization.replace('Bearer ', '');
const header = await admin
.auth()
.verifyIdToken(newToken)
.then(decodedToken => {
if (decodedToken.firebase.tenant !== schoolid) {
throw new AuthenticationError("User doesn't have access to school");
}
return {
...decodedToken,
schoolId: schoolid
} as AuthIdentity;
})
.catch(function(error) {
// Handle error
throw new AuthenticationError('No Access: Invalid id token');
});
return header;
} else {
throw new AuthenticationError('No Access: No id token provided');
}
};
export function gqlServer() {
const app = express();
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req, res }) => {
const auth = await verifyToken(req.headers as any);
return {
auth: auth || {},
req,
res
} as RequestContext;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment