-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
156 lines (137 loc) · 3.92 KB
/
index.ts
File metadata and controls
156 lines (137 loc) · 3.92 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, ".env") });
const express = require("express");
const expressSession = require("express-session");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const morgan = require("morgan");
const axios = require("axios");
const passport = require("passport");
const SpotifyStrategy = require("passport-spotify").Strategy;
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
let REDIRECT_URI = process.env.REDIRECT_URI || "http://localhost:8888/callback";
let FRONTEND_URI = process.env.FRONTEND_URI || "http://localhost:3000";
const PORT = process.env.PORT || 8888;
if (process.env.NODE_ENV !== "production") {
REDIRECT_URI = "http://localhost:8888/callback";
FRONTEND_URI = "http://localhost:3000";
}
// --- PASSPORT AUTH SETUP ---//
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
// https://www.passportjs.org/packages/passport-spotify/
passport.use(
new SpotifyStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
//remember to add this to your developer dashboard redirect uri
callbackURL: `${REDIRECT_URI}`
},
function (accessToken, refreshToken, expires_in, profile, done) {
return done(null, profile, {
accessToken,
refreshToken,
expires_in
});
}
)
);
morgan.token("data", (req, res) => {
if (req.method === "POST") {
return [req.body];
}
if (req.method === "PUT") {
return [req.body];
}
});
const morganMiddleware = morgan(
":method :url :status :res[content-length] - :response-time ms :data"
);
const app = express();
// prioritize all static files
app.use(express.static(path.join(__dirname, "./client/build")));
app.use(express.static(path.join(__dirname, "./client/public")));
app
.use(express.static(path.join(__dirname, "./client/build")))
.use(cors())
.use(express.json())
.use(cookieParser())
.use(morganMiddleware)
.use(passport.initialize())
.use(express.static(path.join(__dirname, "./client/build")));
app.use(
expressSession({
secret: "This is one hell of a secret",
resave: false,
saveUninitialized: false
})
);
app.get("/", function (req, res) {
res.render(path.resolve(__dirname, "./client/build/index.html"));
});
app.get(
"/api/login",
passport.authenticate("spotify", {
display: "popup",
scope: [
"user-read-private",
"user-follow-read",
"playlist-modify-private",
"playlist-read-private",
"playlist-modify-public",
"playlist-read-collaborative"
],
showDialog: true
}),
function (req, res) {}
);
app.get(
"/callback",
passport.authenticate("spotify", {
failureRedirect: REDIRECT_URI
}),
function (req, res) {
const authInfo = JSON.stringify(res.req.authInfo);
res.cookie("authInfo", authInfo);
res.redirect(`${FRONTEND_URI}/playlists`);
}
);
app.get("api/refresh_token", function (req, res) {
// get access token from refresh token
let refresh_token = req.query.refresh_token;
let authOptions = {
url: "https://accounts.spotify.com/api/token",
headers: {
Authorization:
"Basic " +
new Buffer(
process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET
).toString("base64")
},
form: {
grant_type: "refresh_token",
refresh_token
},
json: true
};
axios.post(authOptions.url, null, authOptions).then((response) => {
if (response.status === 200) {
let access_token = response.data.access_token;
res.send({
access_token
});
}
});
});
app.get("*", function (req, res) {
res.sendFile(path.resolve(__dirname, "./client/build", "index.html"));
});
app.listen(PORT, function () {
console.warn(`Node cluster worker ${process.pid}: listening on port ${PORT}`);
});