Socket
Socket
Sign inDemoInstall

twitter-login

Package Overview
Dependencies
3
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    twitter-login

Twitter Login package using oAuth v1.0


Version published
Maintainers
1
Created

Readme

Source

npm-version GitHub issues GitHub forks GitHub license

Twitter Login. OAuth 1.0 flow

Selling Point

This package can be used to get Twitter user access token using oAuth 1.0 flow.

Implements a Client-Side flow for login with twitter, similar to one provided by Facebook and Google. This can be used to get User Access Token to make API calls to Twitter where User Context is required

Installation:

  • Clone as a Git repository

    git clone https://github.com/knitesh/twitter-login.git
    
  • Install as a node_module

    npm i twitter-login --save
    
    OR
    
    npm install twitter-login --save
    

Sample Usage - Express APP

const express = require("express");
const session = require("express-session");
const TwitterLogin = require("twitter-login");

const app = express();
const port = 9000;

const sessionConfig = {
  user: null,
  tokenSecret: null,
  secret: "keyboard cat",
};

app.use(session(sessionConfig));

const twitter = new TwitterLogin({
  consumerKey: "<your api key>",
  consumerSecret: "<your api secret key>",
  callbackUrl: "http://localhost:$port/twitter/auth/userToken",
});

app.get("/twitter/auth", async (req, res) => {
  try {
    const result = await twitter.login();
    // Save the OAuth token secret for use in your /twitter/callback route
    req.session.tokenSecret = result.tokenSecret;
    console.log(result);
    // Redirect to the /twitter/callback route, with the OAuth responses as query params
    res.redirect(result.url);
  } catch (err) {
    // Handle Error here
    res.send("Twitter login error.");
  }
});

app.get("/twitter/auth/userToken", async (req, res) => {
  try {
    const oAuthParam = {
      oauth_token: req.query.oauth_token,
      oauth_verifier: req.query.oauth_verifier,
    };
    const userInfo = await twitter.callback(
      oAuthParam,
      req.session.tokenSecret
    );
    // Delete the tokenSecret securely
    delete req.session.tokenSecret;

    req.session.user = userInfo;

    // Redirect to whatever route that can handle your new Twitter login user details!
    res.redirect("/");
  } catch (err) {
    // Handle Error here
    res.send("Twitter login error.");
  }
});

app.get("/", (req, res) => {
  const _user = req.session && req.session.user;
  if (_user) {
    res.send(JSON.stringify(_user));
  } else {
    res.send("Login with Twitter");
  }
});

app.listen(port, () => {
  console.log("Example app listening at http://localhost:${port}");
});

Keywords

FAQs

Last updated on 23 Aug 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc