Changing token recovery request type
continuous-integration/drone/push Build is passing Details

main
Félix MIELCAREK 11 months ago
parent 1d3e691a38
commit e938e6f07b

@ -24,32 +24,40 @@ 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('/', async (req, res) => {
stepBeggining("Setting: deactivation"); stepBeggining("Activation");
const code = req.query.code; const code = req.query.code;
const authOptions = { const authOptions = {
url: 'https://accounts.spotify.com/api/token', method: 'post', json: true, url: 'https://accounts.spotify.com/api/token', method: 'get', json: true,
data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' }, data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' },
headers: { headers: {
'content-type': 'application/x-www-form-urlencoded', 'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64')) 'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
} }
}; };
let userId; let data;
try { try {
const response1 = await axios(authOptions); const response1 = await axios(authOptions);
const accessToken = response1.data.access_token; 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, } });
userId = response2.data.SpotifyId; data = {
SpotifyId: response2.data.id,
AccessToken: accessToken,
RefreshToken: refreshToken
};
} catch (error) { console.log(`Error getting user Spotify id: ${error}`) } } catch (error) { console.log(`Error getting user Spotify id: ${error}`) }
const sqlQuery = ` const sqlQuery = `
DELETE FROM users INSERT INTO users (spotifyid, accesstoken, refreshtoken)
WHERE spotifyid = ?; VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE
accesstoken = VALUES(accesstoken),
refreshtoken = VALUES(refreshtoken);
`; `;
try { try {
const pool = mariadb.createPool({ const pool = mariadb.createPool({
host: 'felixmielcarek-bigbrotherdb', host: 'felixmielcarek-bigbrotherdb',
@ -61,19 +69,19 @@ app.get('/settings/deactivate', async (req,res) => {
const conn = await pool.getConnection(); const conn = await pool.getConnection();
conn.query(sqlQuery, [userId], (err, res) => { conn.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (err, res) => {
if (err) { if (err) {
console.error('Error executing query', err); console.error('Error executing query', err);
return; return;
} }
console.log('Data deleted successfully'); console.log('Data inserted/updated successfully');
conn.end(); conn.end();
}); });
} catch (error) { console.log(`Error accessing database: ${error}`) } } catch (error) { console.log(`Error accessing database: ${error}`) }
}) });
app.get('/', async (req, res) => { app.get('/settings/deactivate', async (req,res) => {
stepBeggining("Activation"); stepBeggining("Setting: deactivation");
const code = req.query.code; const code = req.query.code;
const authOptions = { const authOptions = {
@ -82,30 +90,22 @@ app.get('/', async (req, res) => {
headers: { headers: {
'content-type': 'application/x-www-form-urlencoded', 'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64')) 'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
} }
}; };
let data; let userId;
try { try {
const response1 = await axios(authOptions); const response1 = await axios(authOptions);
const accessToken = response1.data.access_token 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 = { userId = response2.data.SpotifyId;
SpotifyId: response2.data.id,
AccessToken: accessToken,
RefreshToken: refreshToken
};
} catch (error) { console.log(`Error getting user Spotify id: ${error}`) } } catch (error) { console.log(`Error getting user Spotify id: ${error}`) }
const sqlQuery = ` const sqlQuery = `
INSERT INTO users (spotifyid, accesstoken, refreshtoken) DELETE FROM users
VALUES (?, ?, ?) WHERE spotifyid = ?;
ON DUPLICATE KEY UPDATE
accesstoken = VALUES(accesstoken),
refreshtoken = VALUES(refreshtoken);
`; `;
try { try {
const pool = mariadb.createPool({ const pool = mariadb.createPool({
host: 'felixmielcarek-bigbrotherdb', host: 'felixmielcarek-bigbrotherdb',
@ -117,12 +117,12 @@ app.get('/', async (req, res) => {
const conn = await pool.getConnection(); const conn = await pool.getConnection();
conn.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (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;
} }
console.log('Data inserted/updated successfully'); console.log('Data deleted successfully');
conn.end(); conn.end();
}); });
} catch (error) { console.log(`Error accessing database: ${error}`) } } catch (error) { console.log(`Error accessing database: ${error}`) }

Loading…
Cancel
Save