Skip to content

Instantly share code, notes, and snippets.

@DennisAlund
Last active October 21, 2018 15:15
Show Gist options
  • Save DennisAlund/e672833ffbe5720634275fb6b757f1fb to your computer and use it in GitHub Desktop.
Save DennisAlund/e672833ffbe5720634275fb6b757f1fb to your computer and use it in GitHub Desktop.
Firebase cloud function for OAuth handshake in medium article https://medium.com/evenbit/151c1c98641d
export const oauth_redirect = functions.https.onRequest(async (request, response) => {
if (request.method !== "GET") {
console.error(`Got unsupported ${request.method} request. Expected GET.`);
return response.send(405, "Only GET requests are accepted");
}
if (!request.query && !request.query.code) {
return response.status(401).send("Missing query attribute 'code'");
}
const options = {
uri: "https://slack.com/api/oauth.access",
method: "GET",
json: true,
qs: {
code: request.query.code,
client_id: functions.config().slack.id,
client_secret: functions.config().slack.secret,
redirect_uri: `https://us-central1-${process.env.GCLOUD_PROJECT}.cloudfunctions.net/oauth_redirect`
}
};
const result = await rp(options);
if (!result.ok) {
console.error("The request was not ok: " + JSON.stringify(result));
return response.header("Location", `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com`).send(302);
}
await admin.database().ref("installations").child(result.team_id).set({
token: result.access_token,
team: result.team_id,
webhook: {
url: result.incoming_webhook.url,
channel: result.incoming_webhook.channel_id
}
});
response.header("Location", `https://${process.env.GCLOUD_PROJECT}.firebaseapp.com/success.html`).send(302);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment