npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-twitter-login

v1.0.1

Published

Simple Twitter™(aka X™) Login with Promises.

Downloads

14

Readme

license node-current version unpacked-size downloads

✨ Features

  • Twitter OAuth lightweight wrap.
  • Promises. 🎈
  • Readable Objects.

All this in < 9kb, what else do you need? ✨

📦 Installation

npm install async-twitter-login

🚀 Quick start

We will configure two routes in our web server, auth/login and auth/callback can have any name :P

Initialization

We import and instantiate, you will need your consumer key and your consumer secret... both are obtained when creating an application from the Twitter Developer Portal.

Finally you will need your callback url, as we said before it would be https://example.com/auth/callback.

import TwitterLogin from 'async-twitter-login'

const twitterLogin = new TwitterLogin({
  consumerKey: 'your-consumer-key',
  consumerSecret: 'your-consumer-secret',
  callbackURL: 'https://example.com/auth/callback'
})

Login

From our auth/login path we call the getRequestToken() method and save in a safe place token and tokenSecret to use it later.

app.get('/auth/login', async (req, res) => {
  try {
    // Get the request token and the redirect URL
    const { token, tokenSecret, redirectURL } = await twitterLogin.getRequestToken()
    
    // Save the token and token secret in safe place
    req.session.token = token
    req.session.tokenSecret = tokenSecret

    // Redirect to Twitter to authenticate in the application
    res.redirect(redirectURL)
    return
  } catch (err) {
    // Handle errors
  }
})

Callback

If the user completes the authorization from twitter, he will be redirected to his auth/callback path together with oauth_token and oauth_verifier as query parameters in the URL, they are accessed with req.query but we only need the oauth_verifier.

We call the getAccessToken() method from our auth/callback path and pass the parameters to it along with the token and tokenSecret that we saved in the previous step.

This method will return a user object with the user's data. 🙍‍♂️

app.get('/auth/callback', async (req, res) => {
  // Get the token and token secret from the session
  const { token, tokenSecret } = req.session

  // Get the oauth_verifier from the query parameters
  const { oauth_verifier: verifier } = req.query

  if (!token || !tokenSecret || !verifier) {
    // Handle missing or invalid data
  }

  try {
    // Get the access token and the user data
    const user = await twitterLogin.getAccessToken({ token, tokenSecret, verifier })

    // Delete the token and token secret from the session
    delete req.session.token
    delete req.session.tokenSecret

    // The user object is a readable object with the user's data.
    // user = {
    //   id,
    //   username,
    //   accessToken,
    //   accessTokenSecret
    // }
    req.session.user = user

    // Redirect to the home page
    res.redirect('/')
    return
  } catch (err) {
    // Handle errors
  }
})

Copyright & License

© 2021 Brian Fernandez

This project is licensed under the MIT license. See the file LICENSE for details.

Disclaimer

No affiliation with X Corp.

This package is a third-party offering and is not a product of X Corp.

Twitter™ and X™ are trademarks of X Corp.