Skip to content

Commit 377e0bc

Browse files
committed
added spotify auth
1 parent ab68e97 commit 377e0bc

File tree

7 files changed

+89
-31
lines changed

7 files changed

+89
-31
lines changed

app.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ const app = express();
33
const mongoose = require('mongoose');
44
require('dotenv/config');
55

6-
7-
8-
96
//Routes
10-
const PlaylistRoute = require('./routes/Playlist');
7+
const playlistRoute = require('./routes/Playlist');
8+
const spotifyRoute = require('./routes/spotify');
119

1210
//Middleware
1311
app.use(express.json());
14-
app.use('/Playlist', PlaylistRoute);
12+
app.use('/playlist', playlistRoute);
13+
app.use('/spotify', spotifyRoute);
1514

1615

1716
app.listen(3000);

controllers/playlistController.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const PLAYLIST =require('../models/Playlist');
2+
3+
4+
5+
exports.view_playlist = function(req, res){
6+
res.send('working');
7+
};
8+
9+
exports.add_playlist = function(req, res){
10+
const playlist = new PLAYLIST({
11+
userId: req.body.userId,
12+
name: req.body.name,
13+
desc: req.body.desc,
14+
songs: req.body.songs.map(song => {
15+
return{
16+
source : song.source,
17+
url : song.url
18+
};
19+
})
20+
});
21+
playlist.save()
22+
.then(data => {
23+
res.json(data);
24+
})
25+
.catch( err => {
26+
res.json({message: err});
27+
});
28+
};

controllers/spotifyController.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
require('dotenv').config();
3+
const crypto = require('crypto');
4+
const querystring = require('querystring');
5+
const client_id = process.env.SCLIENT_ID;
6+
const redirect_uri = process.env.S_REDIRECT;
7+
8+
9+
function generateRandomString(length){
10+
return crypto.randomBytes(length).toString('hex');
11+
};
12+
13+
function hashCode(code){
14+
return crypto.createHash('sha256').update(code).digest('base64');
15+
}
16+
exports.getAuthorization = function (req, res) {
17+
18+
let state = generateRandomString(16);
19+
let code_challenge = hashCode(generateRandomString(128));
20+
let scope = 'user-read-private playlist-read-private';
21+
22+
res.redirect('https://accounts.spotify.com/authorize?' +
23+
querystring.stringify({
24+
response_type: 'code',
25+
client_id: client_id,
26+
scope: scope,
27+
redirect_uri: redirect_uri,
28+
state: state,
29+
code_challenge_method: 'S256',
30+
code_challenge: code_challenge
31+
}));
32+
};

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
},
1818
"homepage": "https://github.com/Raezl/Music-aio#readme",
1919
"dependencies": {
20+
"crypto-js": "^4.1.1",
2021
"dotenv": "^10.0.0",
2122
"express": "^4.17.1",
2223
"mongoose": "^6.0.12",
23-
"nodemon": "^2.0.14"
24+
"nodemon": "^2.0.14",
25+
"querystring": "^0.2.1"
2426
}
2527
}

routes/Playlist.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
const express = require('express');
2-
const Playlist =require('../models/Playlist');
2+
const playlist_controller = require('../controllers/playlistController');
33
const router = express.Router();
44

55
//GET routes
6-
router.get('/', (req,res) => {
7-
res.send('Show Playlist');
8-
});
6+
router.get('/', playlist_controller.view_playlist);
97

10-
//POST routes
11-
router.post('/', (req, res) =>{
12-
const playlist = new Playlist({
13-
userId: req.body.userId,
14-
name: req.body.name,
15-
desc: req.body.desc,
16-
songs: req.body.songs.map(song => {
17-
return{
18-
source : song.source,
19-
url : song.url
20-
};
21-
})
22-
});
23-
playlist.save()
24-
.then(data => {
25-
res.json(data);
26-
})
27-
.catch( err => {
28-
res.json({message: err});
29-
});
30-
});
8+
//POST routes
9+
router.post('/', playlist_controller.add_playlist);
3110

3211
module.exports = router;

routes/spotify.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const express = require('express');
2+
const spotify_controller = require('../controllers/spotifyController');
3+
const router = express.Router();
4+
5+
//GET routes
6+
router.get('/', spotify_controller.getAuthorization);
7+
8+
module.exports = router;

0 commit comments

Comments
 (0)