-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
92 lines (77 loc) · 2.73 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require('dotenv').config();
const express = require('express');
const bp = require('body-parser');
const mongoose = require('mongoose');
const expressJWT = require('express-jwt');
const auth = require('./routes/auth');
const playlist = require('./routes/playlist');
const locked = require('./routes/locked');
var querystring = require('querystring');
let request = require('request')
var cloudinary = require('cloudinary');
const port = process.env.port || 3000;
const app = express();
app.use(bp.json({ limit: '50mb' }));
app.use(bp.urlencoded({ extended: false, limit: '50mb' }));
mongoose.connect('mongodb://localhost/moodMusic');
//ADDED config cloudinary (want to add to route once created)
cloudinary.config({
cloud_name: 'dieaqkurh',
api_key: process.env.REACT_APP_CLOUDINARY_API_KEY,
api_secret: process.env.REACT_APP_CLOUDINARY_API_SECRET
});
app.use(express.static(`${__dirname}/client/build`))
app.use('/auth', auth);
app.use('/playlist', playlist);
app.use('/locked', expressJWT({ secret: process.env.JWT_SECRET }).unless({ method: 'POST' }), locked);
let redirect_uri = 'http://localhost:3000/callback'
app.get('/spotifylogin', function(req, res) {
console.log('hit the sfy login route ... should redirect')
res.redirect('https://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: process.env.REACT_APP_SPOTIFY_KEY,
scope: 'user-read-private user-read-email playlist-modify-public',
redirect_uri
}))
})
app.get('/callback', function(req, res) {
let code = req.query.code || null
let authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer(
process.env.REACT_APP_SPOTIFY_KEY + ':' + process.env.REACT_APP_SPOTIFY_SECRET
).toString('base64'))
},
json: true
}
request.post(authOptions, function(error, response, body) {
console.log('heyo')
var access_token = body.access_token
res.cookie('ACCESS_TOKEN', access_token);
res.sendFile(`${__dirname}/client/public/closewindow.html`)
})
})
//creating the test route
app.post('/cloudinary-data', function (req, res) {
console.log('HIT CLOUD_DATA POST ROUTE')
//NEED name delete 'something'
cloudinary.v2.api.resource(req.body.imgPublicId, { colors: true },
function (error, result) {
res.json(result);
});
});
app.get('*', (req, res) => {
console.log('wildcard route hit')
res.sendFile(`${__dirname}/client/build/index.html`);
});
const server = app.listen(port, () => {
console.log('You\'re listening to the smooooth sounds of port ' + port)
});
module.exports = server;