Giter VIP home page Giter VIP logo

passport-twitch.js's Introduction

passport-twitch.js

Build Status semantic-release

Downloads Downloads npm version License

dependencies dev dependencies

Code Climate Discord Chat
PayPal

Twitch is a trademark or registered trademark of Twitch Interactive, Inc. in the U.S. and/or other countries. "passport-twitch.js" is not operated by, sponsored by, or affiliated with Twitch Interactive, Inc. in any way.

Passport strategies for authenticating with Twitch using OAuth 2.0 on the New & Old Twitch API.

This module lets you authenticate using Twitch in your Node.js applications. By plugging into Passport, Twitch authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Install

$ npm install passport-twitch.js

Usage of OAuth 2.0

Configure Strategy

The Twitch OAuth 2.0 authentication strategy authenticates users using a Twitch account and OAuth 2.0 tokens. The strategy requires a verify callback, which accepts these credentials and calls done providing a user, as well as options specifying a client ID, client secret, and callback URL.

var passport       = require("passport");
var twitchStrategy = require("passport-twitch.js").Strategy;

passport.use(new twitchStrategy({
    clientID: TWITCH_CLIENT_ID,
    clientSecret: TWITCH_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/twitch/callback",
    scope: "user_read"
  },
  function(accessToken, refreshToken, profile, done) {
    //Handle Database Query Addition Here.
  }
));

Authenticate Requests

Use passport.authenticate(), specifying the "twitch.js" strategy, to authenticate requests.

For example, as route middleware in an Express application:

app.get("/auth/twitch", passport.authenticate("twitch.js"));
app.get("/auth/twitch/callback", passport.authenticate("twitch.js", { failureRedirect: "/" }), function(req, res) {
    // Successful authentication, redirect home.
    res.redirect("/");
});

Optionally, the forceVerify option can be set to true to indicate that the user should be re-prompted for authorization:

app.get("/auth/twitch", passport.authenticate("twitch.js", {forceVerify: true}));

The request to this route should include a GET or POST data with the keys access_token and optionally, refresh_token set to the credentials you receive from Twitch.

GET /auth/twitch?access_token=<TOKEN>

Issues

If you receive a 401 Unauthorized error, it is most likely because you have wrong access token or not yet specified any application permissions. Once you refresh access token with new permissions, try to send this access token again.

Example

#!/bin/env node

require('dotenv').config();

var express = require('express');
var session = require('express-session');
var helmet = require('helmet');
var bodyParser = require('body-parser');

var passport = require('passport');
var refresh = require('passport-oauth2-refresh');
var _strategy = require('passport-twitch.js').Strategy;

var app = express();

app.use(session({
  key: process.env.SESSION_KEY,
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: {
    secure: true
  }
}));

app.use(helmet());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

passport.serializeUser(function(u, d) {
  d(null, u);
});
passport.deserializeUser(function(u, d) {
  d(null, u);
});

var TwitchStrategy = new _strategy({
  clientID: process.env.TWITCH_ID,
  clientSecret: process.env.TWITCH_SECRET,
  callbackURL: "http://127.0.0.1:3000/auth/twitch/callback",
  scope: ["guilds", "connections", "email"]
}, function(accesstoken, refreshToken, profile, done) {
  console.log(profile);
  return done(null, profile);
});

passport.use(TwitchStrategy);
refresh.use(TwitchStrategy);

app.get('/', function(req, res) {
  if (!req.session.user) {
    res.redirect('/login');
  } else {
    res.send(`Hello ${req.session.user.display_name}`);
  }
});

app.get('/login', passport.authenticate('twitch.js'}));
app.get('/auth/twitch/callback', passport.authenticate('twitch.js', { failureRedirect: '/' }), function(req, res) {
  req.session.user = req.user;
  console.log(req.user);
  console.log(req.query);
  res.redirect('/');
});
app.listen(process.env.SITE_PORT, process.env.SITE_HOST, function() {
  console.log(`Express Started`);
});

Projected Maintained by: Randolph Aarseth(Bioblaze Payne)

passport-twitch.js's People

Contributors

bioblaze avatar holfz avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Forkers

zachtix

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.