Skip to content

Instantly share code, notes, and snippets.

@lydemann
Created December 28, 2020 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lydemann/1d123f60f1074366edaa2904fc6fec1b to your computer and use it in GitHub Desktop.
Save lydemann/1d123f60f1074366edaa2904fc6fec1b to your computer and use it in GitHub Desktop.
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