Giter Site home page Giter Site logo

br14n-sol / async-twitter-login Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 267 KB

Simple Twitter login, without much of the bullshit. and promises... who doesn't like them?

Home Page: https://www.npmjs.com/package/async-twitter-login

License: MIT License

TypeScript 94.24% Shell 5.76%
login login-oauth login-system twitter twitter-api twitter-login twitter-login-integration twitterlogin

async-twitter-login's Introduction

async-twitter-login

npm npm bundle size npm Libraries.io SourceRank

Simple Twitter login, without much of the bullshit. and promises... who doesn't like them?

โœจ Features

  • Twitter OAuth lightweight wrap.
  • Promises. ๐ŸŽˆ
  • Readable Objects.

All this in < 4kb, 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 comsumer 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.

// ECMAScript
import AsyncTwitterLogin from 'async-twitter-login'

// CommonJS
const AsyncTwitterLogin = require('async-twitter-login').default

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

Login

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

app.get('/auth/login', async (req, res) => {
  try {
    const { token, tokenSecret, redirectUrl } = await twitterLogin.request()
    
    // Save the token secret in a safe place
    req.session.tokenSecret = tokenSecret

    // Redirect to Twitter to authorize the application
    res.redirect(redirectUrl)
  } 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.

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

This method will return a user object. ๐Ÿง”

app.get('/auth/callback', async (req, res) => {
  try {
    const { oauth_token: token, oauth_verifier: verifier } = req.query
    const tokenSecret = req.session.tokenSecret
    const user = await twitterLogin.callback(token, tokenSecret, verifier)

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

    // The user object is a readable object with the user's data.
    // user = {
    //   id,
    //   userName,
    //   token,
    //   tokenSecret
    // }
    req.session.user = user

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

License

MIT License ยฉ 2021 - 2023 Brian Fernandez.

async-twitter-login's People

Contributors

br14n-sol avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.