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

@umanghome/login-with-twitter-cf-workers

v1.0.1

Published

Login with Twitter. OAuth without the nonsense - for CloudFlare Workers

Downloads

6

Readme

@umanghome/login-with-twitter-cf-workers npm downloads

Login with Twitter for CloudFlare Workers. OAuth without the nonsense.

Forked from feross/login-with-twitter and modified to be executable on CloudFlare Workers.

Features

This module is designed to be the lightest possible wrapper on Twitter OAuth.

All this in < 100 lines of code.

Install

npm install @umanghome/login-with-twitter-cf-workers

Usage

Set up two routes on your web sever. We'll call them /twitter and /twitter/callback, but they can be named anything.

Initialization

Initialize this module with the consumer key and secret for your Twitter App you created with an Twitter Developer account.

const LoginWithTwitter = require('@umanghome/login-with-twitter-cf-workers');

const tw = new LoginWithTwitter({
  consumerKey: '<your consumer key>',
  consumerSecret: '<your consumer secret>',
  callbackUrl: 'https://example.com/twitter/callback',
});

Login

Call login from your /twitter route, saving the OAuth tokenSecret to use later. In this example, we use the request session (using, for example, express-session).

app.get('/twitter', (req, res) => {
  tw.login()
    .then(({ tokenSecret, url }) => {
      // Save the OAuth token secret for use in your /twitter/callback route
      req.session.tokenSecret = tokenSecret;

      // Redirect to the /twitter/callback route, with the OAuth responses as query params
      res.redirect(url);
    })
    .catch((err) => {
      // Handle the error your way
    });
});

Callback

Then, call callback from your /twitter/callback route. The request will include oauth_token and oauth_verifier in the URL, accessible with req.query. Pass those into callback, along with the OAuth tokenSecret you saved in the login callback above, and a callback that handles a user object that this module will return.

app.get('/twitter/callback', (req, res) => {
  tw.callback(
    {
      oauth_token: req.query.oauth_token,
      oauth_verifier: req.query.oauth_verifier,
    },
    req.session.tokenSecret
  )
    .then((user) => {
      // Delete the tokenSecret securely
      delete req.session.tokenSecret;

      // The user object contains 4 key/value pairs, which
      // you should store and use as you need, e.g. with your
      // own calls to Twitter's API, or a Twitter API module
      // like `twitter` or `twit`.
      // user = {
      //   userId,
      //   userName,
      //   userToken,
      //   userTokenSecret
      // }
      req.session.user = user;

      // Redirect to whatever route that can handle your new Twitter login user details!
      res.redirect('/');
    })
    .catch((err) => {
      // Handle the error your way
    });
});

Logout

If you want to implement logout, simply delete the user object stored in the session.


For more information, check out the implementation in index.js.

license

MIT. Copyright (c) Umang Galaiya.