Skip to content

Instantly share code, notes, and snippets.

@lxhom
Created May 5, 2021 15:19
Show Gist options
  • Save lxhom/a632aca1af4cdc74a83baf6b80077350 to your computer and use it in GitHub Desktop.
Save lxhom/a632aca1af4cdc74a83baf6b80077350 to your computer and use it in GitHub Desktop.
what has three legs and makes people want to cry? three-legged oauth. so heres a template written in node/express with login-with-twitter
const express = require('express');
const lwt = require("login-with-twitter");
const app = express()
// twitter oauth
const tw = new lwt({
consumerKey: '[#yourkey]',
consumerSecret: '[#yourkey]',
callbackUrl: 'https://[#your url]/callback'
});
app.get("/callback", (req, res) => {
res.send(`
<script>
location.href = location.href.replace("/c?", "/gen?") + "&token_secret=" + localStorage.tokenSecret;
</script>
`);
});
app.get("/generate-token", (req, res) => {
tw.callback({
oauth_token: req.query.oauth_token,
oauth_verifier: req.query.oauth_verifier
}, req.query.token_secret, (err, user) => {
if (err) {
throw err;
}
// auth tokens are stored in the user object. edit this part and add a logged in page
res.send(`
<script>
location.href = "/logged-in?authdata=${escape(JSON.stringify(user))}"
</script>
`);
});
});
app.get("/start-login", (req, res) => {
tw.login((err, tokenSecret, url) => {
if (err) {
throw err;
}
res.send(`
<script>
localStorage.tokenSecret = "${tokenSecret}";
location.href = "${url}";
</script>
`);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment