Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mr-pascal/63b0d96aa74890abb675dd68cb386880 to your computer and use it in GitHub Desktop.
Save mr-pascal/63b0d96aa74890abb675dd68cb386880 to your computer and use it in GitHub Desktop.
// part of external-service/index.ts
/**
* 1. Define a GET endpoint with the url "/ping"
*/
app.get('/ping', async (request, reply) => {
// Just rename global variable for ease of use
const url: string = cloudFunctionEndpoint;
// 2. Check if clientEmail and privateKey exists, otherwise auth is not possible
if (!clientEmail || ! privateKey) {
reply
.status(500)
.send(`Make sure that the 'CLIENT_EMAIL' and 'PRIVATE_KEY' environment variables exists!!!`);
return;
}
// 3. Create JWT using the email and the private key from the Environment variables
const clientjwt = new JWT({
email: clientEmail,
key: privateKey,
});
// 4. Fetch Bearer token for further authorization
const token = await clientjwt.fetchIdToken(url);
// 5. Set Bearer token to the header map
// A simple JSON doesn't seem to work with the library, even though underlying typings tell so
const headers = new Map([
['Authorization', `Bearer ${token}`],
]);
// 6. Do the Actual request to the Cloud Function
const result = await clientjwt
.request({url, headers});
// 7. Return the status and payload of the Cloud Function
reply.send({
status: result.status,
body: result.data,
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment