|
|
@ -12,25 +12,15 @@ const redirectUri = 'https://felixmielcarek.github.io/callback/';
|
|
|
|
//#endregion
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
//#region APP INIT
|
|
|
|
//#region APP INIT
|
|
|
|
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
const app = express()
|
|
|
|
app.listen(port, () => { console.log(`Big brother is listening on port ${port}`) })
|
|
|
|
app.listen(port, () => { console.log(`Big brother is listening on port ${port}`) })
|
|
|
|
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
//#region ACCESS TOKEN
|
|
|
|
//#region ACCESS TOKEN
|
|
|
|
app.get('/settings/deactivate', async (req,res) => {
|
|
|
|
app.get('/settings/deactivate', async (req,res) => {
|
|
|
|
|
|
|
|
//console.log("
|
|
|
|
const code = req.query.code;
|
|
|
|
const code = req.query.code;
|
|
|
|
|
|
|
|
const authOptions = {
|
|
|
|
const pool = mariadb.createPool({
|
|
|
|
|
|
|
|
host: 'felixmielcarek-bigbrotherdb',
|
|
|
|
|
|
|
|
user: process.env.MARIADB_USER,
|
|
|
|
|
|
|
|
database: process.env.MARIADB_DATABASE,
|
|
|
|
|
|
|
|
password: process.env.MARIADB_PASSWORD,
|
|
|
|
|
|
|
|
connectionLimit: 5
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var authOptions = {
|
|
|
|
|
|
|
|
url: 'https://accounts.spotify.com/api/token', method: 'post', json: true,
|
|
|
|
url: 'https://accounts.spotify.com/api/token', method: 'post', json: true,
|
|
|
|
data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' },
|
|
|
|
data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' },
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
@ -38,22 +28,32 @@ app.get('/settings/deactivate', async (req,res) => {
|
|
|
|
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
|
|
|
|
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
let userId;
|
|
|
|
|
|
|
|
|
|
|
|
const response1 = await axios(authOptions);
|
|
|
|
try {
|
|
|
|
const accessToken = response1.data.access_token
|
|
|
|
const response1 = await axios(authOptions);
|
|
|
|
|
|
|
|
const accessToken = response1.data.access_token;
|
|
|
|
|
|
|
|
const response2 = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } });
|
|
|
|
|
|
|
|
userId = response2.data.SpotifyId;
|
|
|
|
|
|
|
|
} catch (error) { console.log(`Error getting user Spotify id: ${error}`) }
|
|
|
|
|
|
|
|
|
|
|
|
let conn;
|
|
|
|
const sqlQuery = `
|
|
|
|
|
|
|
|
DELETE FROM users
|
|
|
|
|
|
|
|
WHERE spotifyid = ?;
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
conn = await pool.getConnection()
|
|
|
|
const pool = mariadb.createPool({
|
|
|
|
const response2 = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } });
|
|
|
|
host: 'felixmielcarek-bigbrotherdb',
|
|
|
|
|
|
|
|
user: process.env.MARIADB_USER,
|
|
|
|
|
|
|
|
database: process.env.MARIADB_DATABASE,
|
|
|
|
|
|
|
|
password: process.env.MARIADB_PASSWORD,
|
|
|
|
|
|
|
|
connectionLimit: 5
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const sqlQuery = `
|
|
|
|
const conn = await pool.getConnection();
|
|
|
|
DELETE FROM users
|
|
|
|
|
|
|
|
WHERE spotifyid = ?;
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conn.query(sqlQuery, [response2.data.SpotifyId], (err, res) => {
|
|
|
|
conn.query(sqlQuery, [userId], (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error('Error executing query', err);
|
|
|
|
console.error('Error executing query', err);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
@ -61,21 +61,12 @@ app.get('/settings/deactivate', async (req,res) => {
|
|
|
|
console.log('Data deleted successfully');
|
|
|
|
console.log('Data deleted successfully');
|
|
|
|
conn.end();
|
|
|
|
conn.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (error) { console.log('Error getting user Spotify id') }
|
|
|
|
} catch (error) { console.log(`Error accessing database: ${error}`) }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => {
|
|
|
|
app.get('/', async (req, res) => {
|
|
|
|
const pool = mariadb.createPool({
|
|
|
|
|
|
|
|
host: 'felixmielcarek-bigbrotherdb',
|
|
|
|
|
|
|
|
user: process.env.MARIADB_USER,
|
|
|
|
|
|
|
|
database: process.env.MARIADB_DATABASE,
|
|
|
|
|
|
|
|
password: process.env.MARIADB_PASSWORD,
|
|
|
|
|
|
|
|
connectionLimit: 5
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const code = req.query.code;
|
|
|
|
const code = req.query.code;
|
|
|
|
|
|
|
|
const authOptions = {
|
|
|
|
var authOptions = {
|
|
|
|
|
|
|
|
url: 'https://accounts.spotify.com/api/token', method: 'post', json: true,
|
|
|
|
url: 'https://accounts.spotify.com/api/token', method: 'post', json: true,
|
|
|
|
data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' },
|
|
|
|
data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' },
|
|
|
|
headers: {
|
|
|
|
headers: {
|
|
|
@ -83,31 +74,38 @@ app.get('/', async (req, res) => {
|
|
|
|
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
|
|
|
|
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
let data;
|
|
|
|
const response1 = await axios(authOptions);
|
|
|
|
|
|
|
|
const accessToken = response1.data.access_token
|
|
|
|
|
|
|
|
const refreshToken = response1.data.refresh_token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let conn;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
conn = await pool.getConnection()
|
|
|
|
const response1 = await axios(authOptions);
|
|
|
|
|
|
|
|
const accessToken = response1.data.access_token
|
|
|
|
|
|
|
|
const refreshToken = response1.data.refresh_token
|
|
|
|
const response2 = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } });
|
|
|
|
const response2 = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } });
|
|
|
|
|
|
|
|
data = {
|
|
|
|
const data = {
|
|
|
|
|
|
|
|
SpotifyId: response2.data.id,
|
|
|
|
SpotifyId: response2.data.id,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
RefreshToken: refreshToken
|
|
|
|
RefreshToken: refreshToken
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
} catch (error) { console.log(`Error getting user Spotify id: ${error}`) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sqlQuery = `
|
|
|
|
|
|
|
|
INSERT INTO users (spotifyid, accesstoken, refreshtoken)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
|
|
|
|
accesstoken = VALUES(accesstoken),
|
|
|
|
|
|
|
|
refreshtoken = VALUES(refreshtoken);
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const pool = mariadb.createPool({
|
|
|
|
|
|
|
|
host: 'felixmielcarek-bigbrotherdb',
|
|
|
|
|
|
|
|
user: process.env.MARIADB_USER,
|
|
|
|
|
|
|
|
database: process.env.MARIADB_DATABASE,
|
|
|
|
|
|
|
|
password: process.env.MARIADB_PASSWORD,
|
|
|
|
|
|
|
|
connectionLimit: 5
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const sqlQuery = `
|
|
|
|
const conn = await pool.getConnection();
|
|
|
|
INSERT INTO users (spotifyid, accesstoken, refreshtoken)
|
|
|
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
|
|
|
|
accesstoken = VALUES(accesstoken),
|
|
|
|
|
|
|
|
refreshtoken = VALUES(refreshtoken);
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conn.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (err, res) => {
|
|
|
|
conn.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
@ -117,6 +115,6 @@ app.get('/', async (req, res) => {
|
|
|
|
console.log('Data inserted/updated successfully');
|
|
|
|
console.log('Data inserted/updated successfully');
|
|
|
|
conn.end();
|
|
|
|
conn.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (error) { console.log('Error getting user Spotify id') }
|
|
|
|
} catch (error) { console.log(`Error accessing database: ${error}`) }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
//#endregion
|