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
Weekly downloads
46
increased by84%
Maintainers
1
Install size
462 kB
Created
Weekly downloads
 

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
    

Usage

To start twitter Login process

await twitter.login()

To get user details

await twitter.callback(auth, tokenSecret)

Sample 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))

// get consumer key and consumer secret from Twitter App setting
const twitter = new TwitterLogin({
  consumerKey: '<your api key>',
  consumerSecret: '<your api secret key>',
  callbackUrl: 'http://localhost:$port/twitter/auth/userToken',
})

// Route where user will get directed on clicking on Login button
app.get('/twitter/auth', async (req, res) => {
  try {
    const result = await twitter.login()
    // Save the OAuth token secret for use in your /twitterauth/userToken Callback route
    req.session.tokenSecret = result.tokenSecret
    // Redirect to the /twitterauth/userToken route, with the OAuth responses as query params
    res.redirect(result.url)
  } catch (err) {
    // Handle Error here
    res.send('Twitter login error.')
  }
})

// Callback Route to retreive user auth token and secret
// This needs to be whitelisted in your twitter app
app.get('/twitter/auth/userToken', async (req, res) => {
  try {
    const oAuthParam = {
      oauth_token: req.query.oauth_token,
      oauth_verifier: req.query.oauth_verifier,
    }

    // call function passing Auth and Token Secret
    const userInfo = await twitter.callback(
      oAuthParam,
      req.session.tokenSecret,
    )
    // Delete the tokenSecret securely
    delete req.session.tokenSecret
    // Add User Info to your session
    req.session.user = userInfo
    // Redirect to route that can extract user detail
    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 -http://localhost:9000/twitter/auth')
  }
})

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

Keywords

FAQs

Last updated on 29 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