-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.js
68 lines (63 loc) · 1.77 KB
/
authentication.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
const connection = require('./db_config')
module.exports = (req, res, next) => {
if (req.cookies['auth']) {
var auth_cookie = req.cookies['auth'];
// if token exists, set the access token on the API object, so we can make requests
spotifyApi.setAccessToken(auth_cookie);
// A check if the token is expired
spotifyApi.getMe().then(function (data) {
var user_id = data.body.id
// We have valid spotify data so we can continue
connection.query('SELECT * FROM `tb_users` WHERE user_id = ? ', [user_id], function (err, rows, fields) {
if (err) { console.log(err); return }
if (!rows.length){
connection.query('INSERT INTO `tb_users` (`user_id`) VALUES (?)', [user_id], function (err, rows, fields) {
if (!err) {
console.log('Added new user to db')
}
else
{
console.log(err)
}
})
}
});
req.getMe = data;
req.Authentication = true;
next();
}, function (err) {
res.clearCookie("auth")
req.Authentication = false;
req.Error = err.body.error
next();
});
}
else
{
var scopes = [
"ugc-image-upload",
"user-read-playback-state",
"user-modify-playback-state",
"user-read-currently-playing",
"user-read-private",
"user-read-email",
"user-follow-modify",
"user-follow-read",
"user-library-modify",
"user-library-read",
"streaming",
"app-remote-control",
"user-read-playback-position",
"user-top-read",
"user-read-recently-played",
"playlist-modify-private",
"playlist-read-collaborative",
"playlist-read-private",
"playlist-modify-public",
];
var state = 'Watchlist';
req.redirect = spotifyApi.createAuthorizeURL(scopes, state);
req.Authentication = false;
next();
}
}