GithubHelp home page GithubHelp logo

shettysai / passport-spotify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jmperez/passport-spotify

0.0 0.0 0.0 108 KB

Spotify authentication strategy for Passport and Node.js.

License: MIT License

Makefile 5.23% JavaScript 94.77%

passport-spotify's Introduction

Passport-Spotify

Passport strategy for authenticating with Spotify using the OAuth 2.0 API.

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

For more information about Spotify's OAuth 2.0 implementation, check their Web API Authorization Guide.

Installation

$ npm install passport-spotify

Usage

Configure Strategy

The Spotify authentication strategy authenticates users using a Spotify 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.

const SpotifyStrategy = require('passport-spotify').Strategy;

passport.use(
  new SpotifyStrategy(
    {
      clientID: client_id,
      clientSecret: client_secret,
      callbackURL: 'http://localhost:8888/auth/spotify/callback'
    },
    function(accessToken, refreshToken, expires_in, profile, done) {
      User.findOrCreate({ spotifyId: profile.id }, function(err, user) {
        return done(err, user);
      });
    }
  )
);

Authenticate Requests

Use passport.authenticate(), specifying the 'spotify' strategy, to authenticate requests.

For example, as route middleware in an Express application:

app.get('/auth/spotify', passport.authenticate('spotify'));

app.get(
  '/auth/spotify/callback',
  passport.authenticate('spotify', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  }
);

Using scopes

Depending on the data you want to fetch, you may want to specify custom scopes. For more information about scopes in the Spotify Web API check their developer site.

By default, no scope is passed. That means that you won't fetch information such as display name, picture or email. You can get those by using these scopes:

  • user-read-email: Returns the email address of the user on Spotify, if it exists.
  • user-read-private: Returns private information about the user such as display name and picture, if they are set.

You can specify the parameters in the authenticate call:

app.get(
  '/auth/spotify',
  passport.authenticate('spotify', {
    scope: ['user-read-email', 'user-read-private']
  })
);

Forcing login dialog

You can force the login dialog using the showDialog parameter when authenticating:

app.get(
  '/auth/spotify',
  passport.authenticate('spotify', {
    scope: ['user-read-email', 'user-read-private'],
    showDialog: true
  })
);

Examples

For a complete, working example, refer to the login example.

You can get your keys on Spotify - My Applications.

Tests

$ npm install --dev
$ make test

Build and Coverage Status

Build Status Coverage Status

License

The MIT License

passport-spotify's People

Contributors

jmperez avatar greenkeeper[bot] avatar afeld avatar andriyor avatar flaque avatar hughrawlinson 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.