Created
December 28, 2020 18:34
-
-
Save lydemann/1d123f60f1074366edaa2904fc6fec1b to your computer and use it in GitHub Desktop.
server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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