Skip to content

Commit

Permalink
update example app (#96)
Browse files Browse the repository at this point in the history
* format example file using Prettier

* example: specify relative package in package.json

Makes the actual script more consistent.

* make example code callback path match documentation

* example: DRY the port and callback path

* be explicit about how to run example app

* update packages for example app

* switch to nunjucks templates

Swig is unmaintained.

* example: load credentials using dotenv

* remove unused callback for authorization route
  • Loading branch information
afeld committed Sep 6, 2020
1 parent 051e9da commit dae7e3e
Show file tree
Hide file tree
Showing 8 changed files with 1,425 additions and 350 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
node_modules
examples/login/node_modules
test/coverage.html
17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ For example, as route middleware in an [Express](http://expressjs.com/)
application:

```javascript
app.get('/auth/spotify', passport.authenticate('spotify'), function(req, res) {
// The request will be redirected to spotify for authentication, so this
// function will not be called.
});
app.get('/auth/spotify', passport.authenticate('spotify'));

app.get(
'/auth/spotify/callback',
Expand Down Expand Up @@ -84,11 +81,7 @@ app.get(
'/auth/spotify',
passport.authenticate('spotify', {
scope: ['user-read-email', 'user-read-private']
}),
function(req, res) {
// The request will be redirected to spotify for authentication, so this
// function will not be called.
}
})
);
```

Expand All @@ -102,11 +95,7 @@ app.get(
passport.authenticate('spotify', {
scope: ['user-read-email', 'user-read-private'],
showDialog: true
}),
function(req, res) {
// The request will be redirected to spotify for authentication, so this
// function will not be called.
}
})
);
```

Expand Down
25 changes: 22 additions & 3 deletions examples/login/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

If you want to run the project, create a Spotify app on [the Spotify Developer site](https://developer.spotify.com/dashboard/).

1. Add `http://localhost:8888/callback` as a redirect URI to your app profile.
2. Copy the client ID and client secret and paste them in the `app.js` file.
3. Navigate to `http://localhost:8888/`.
1. Add `http://localhost:8888/auth/spotify/callback` as a redirect URI to your app profile.
1. Create a `.env` file with the following:

```
CLIENT_ID=
CLIENT_SECRET=
```

1. Copy the client ID and client secret and paste them into the `.env`.
1. Install the dependencies.

```sh
npm install
```

1. Run the application.

```sh
node app.js
```

1. Navigate to `http://localhost:8888/`.
86 changes: 43 additions & 43 deletions examples/login/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var express = require('express'),
session = require('express-session'),
passport = require('passport'),
swig = require('swig'),
SpotifyStrategy = require('../../lib/passport-spotify/index').Strategy;
var express = require("express"),
session = require("express-session"),
passport = require("passport"),
SpotifyStrategy = require("passport-spotify").Strategy,
consolidate = require("consolidate");

var consolidate = require('consolidate');
require("dotenv").config();

var appKey = 'bb200fb215c346448b3c34bbccaac25d';
var appSecret = '0902db0eb5274d4a8f3ec07d3d00d2c8';
var port = 8888;
var authCallbackPath = "/auth/spotify/callback";

// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
Expand All @@ -16,11 +16,11 @@ var appSecret = '0902db0eb5274d4a8f3ec07d3d00d2c8';
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete spotify profile is serialized
// and deserialized.
passport.serializeUser(function(user, done) {
passport.serializeUser(function (user, done) {
done(null, user);
});

passport.deserializeUser(function(obj, done) {
passport.deserializeUser(function (obj, done) {
done(null, obj);
});

Expand All @@ -31,13 +31,13 @@ passport.deserializeUser(function(obj, done) {
passport.use(
new SpotifyStrategy(
{
clientID: appKey,
clientSecret: appSecret,
callbackURL: 'http://localhost:8888/callback'
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:" + port + authCallbackPath,
},
function(accessToken, refreshToken, expires_in, profile, done) {
function (accessToken, refreshToken, expires_in, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function() {
process.nextTick(function () {
// To keep the example simple, the user's spotify profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the spotify account with a user record in your database,
Expand All @@ -51,29 +51,31 @@ passport.use(
var app = express();

// configure Express
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set("views", __dirname + "/views");
app.set("view engine", "html");

app.use(session({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
app.use(
session({ secret: "keyboard cat", resave: true, saveUninitialized: true })
);
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());

app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + "/public"));

app.engine('html', consolidate.swig);
app.engine("html", consolidate.nunjucks);

app.get('/', function(req, res) {
res.render('index.html', { user: req.user });
app.get("/", function (req, res) {
res.render("index.html", { user: req.user });
});

app.get('/account', ensureAuthenticated, function(req, res) {
res.render('account.html', { user: req.user });
app.get("/account", ensureAuthenticated, function (req, res) {
res.render("account.html", { user: req.user });
});

app.get('/login', function(req, res) {
res.render('login.html', { user: req.user });
app.get("/login", function (req, res) {
res.render("login.html", { user: req.user });
});

// GET /auth/spotify
Expand All @@ -82,15 +84,11 @@ app.get('/login', function(req, res) {
// the user to spotify.com. After authorization, spotify will redirect the user
// back to this application at /auth/spotify/callback
app.get(
'/auth/spotify',
passport.authenticate('spotify', {
scope: ['user-read-email', 'user-read-private'],
showDialog: true
}),
function(req, res) {
// The request will be redirected to spotify for authentication, so this
// function will not be called.
}
"/auth/spotify",
passport.authenticate("spotify", {
scope: ["user-read-email", "user-read-private"],
showDialog: true,
})
);

// GET /auth/spotify/callback
Expand All @@ -99,19 +97,21 @@ app.get(
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get(
'/callback',
passport.authenticate('spotify', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
authCallbackPath,
passport.authenticate("spotify", { failureRedirect: "/login" }),
function (req, res) {
res.redirect("/");
}
);

app.get('/logout', function(req, res) {
app.get("/logout", function (req, res) {
req.logout();
res.redirect('/');
res.redirect("/");
});

app.listen(8888);
app.listen(port, function () {
console.log("App is listening on port " + port);
});

// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
Expand All @@ -122,5 +122,5 @@ function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
res.redirect("/login");
}
Loading

0 comments on commit dae7e3e

Please sign in to comment.