-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
68 lines (53 loc) · 1.7 KB
/
auth.js
File metadata and controls
68 lines (53 loc) · 1.7 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
var spotifyAPI = require('spotify-web-api-node');
var express = require('express');
var AUTH_PORT = 6969;
var AUTH_DIRECTORY = '/auth';
var ADMIN_DIRECTORY = '/admin';
var REDIRECT_DIRECTORY = '/confirm';
var HOST_ID = '1161086985';
var app = express();
var settings = {
clientId : "cab4b5b99f96426e9dfaf76d268f3871",
clientSecret: "3000734987d6434a963489d900c65641",
redirectUri: "http%3A%2F%2F129.31.218.236%3A6969%2Fauth%2Fconfirm"
};
var spot = new spotifyAPI(settings);
var tokens = [];
exports.auth_user = function(){
app.listen(AUTH_PORT, function(){
console.log('Authentication link setup on http://localhost:'+AUTH_PORT+AUTH_DIRECTORY);
});
app.get(AUTH_DIRECTORY, function(req,res){
var url = spot.createAuthorizeURL(['user-read-private', 'user-read-email'], "shitty-state");
res.redirect(url);
});
app.get(AUTH_DIRECTORY + REDIRECT_DIRECTORY, function(req,res){
var code = req.query.code;
spot.authorizationCodeGrant(code)
.then(function(data) {
// Set the access token on the API object to use it in later calls
spot.setAccessToken(data.body['access_token']);
spot.setRefreshToken(data.body['refresh_token']);
spot.getMe().then(function(user){
tokens.push({userID:user.body.id, token: data.body['access_token']});
console.log(tokens);
});
res.send("Thanks for Authenticating!");
}, function(err) {
console.log('Something went wrong!', err);
});
});
}
exports.get_tokens() = function(){
return tokens();
}
//get the business token
exports.get_host = function(){
var token;
for(i = 0; i < tokens.length; i++){
if(tokens[i].userID == HOST_ID){
token = tokens[i];
}
}
return token;
}