Skip to content

Instantly share code, notes, and snippets.

@lxhom
Created May 5, 2021 15:19
Show Gist options
  • Select an option

  • Save lxhom/a632aca1af4cdc74a83baf6b80077350 to your computer and use it in GitHub Desktop.

Select an option

Save lxhom/a632aca1af4cdc74a83baf6b80077350 to your computer and use it in GitHub Desktop.

Revisions

  1. lxhom created this gist May 5, 2021.
    50 changes: 50 additions & 0 deletions oauth.node.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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>
    `);
    });
    });