-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (55 loc) · 1.86 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
global.express = require('express');
global.app = express();
global.ejs = require('ejs');
global.passport = require('passport');
global.SpotifyStrategy = require('passport-spotify').Strategy;
global.session = require('express-session');
global.config = require('./config.js');
passport.use(new SpotifyStrategy({
clientID: config.clientID,
clientSecret: config.clientSecret,
callbackURL: config.callbackURL
},
function (accessToken, refreshToken, profile, done) {
//console.log(profile); // the data from spotify
return done(null, profile);
}
));
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
const checkAuth = function (req, res, next) {
if (req.isAuthenticated()) return next();
res.redirect('/auth/spotify');
};
app.set('views', __dirname + '/www');
app.set('view engine', 'ejs');
app.use(session({ secret: config.secret, resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.get('/', function (req, res) {
if (req.isAuthenticated()) res.redirect('/profile'); // if logged in, redirect to profile
res.render('index'); // otherwise, render the index page
});
app.get('/profile', checkAuth, function (req, res) {
res.render('profile', { user: req.user });
});
app.get('/auth/spotify', passport.authenticate('spotify'));
app.get('/auth/callback',
passport.authenticate('spotify', { failureRedirect: '/' }),
function (req, res) {
res.redirect('/profile');
}
);
app.get("/logout", (req, res) => {
req.logout(req.user, err => {
if (err) return next(err);
res.redirect("/");
});
});
app.listen(config.port, function () {
console.log('Listening on port', config.port);
});