Cleaning callback server code
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
641f33c952
commit
aec2427291
@ -0,0 +1,76 @@
|
||||
//#region REQUIRE
|
||||
const express = require('express')
|
||||
const axios = require('axios');
|
||||
const { Client } = require('pg');
|
||||
//#endregion
|
||||
|
||||
//#region CONSTANTS
|
||||
const port = 80
|
||||
const clientId = process.env.CLIENT_ID;
|
||||
const clientSecret = process.env.CLIENT_SECRET;
|
||||
const redirectUri = 'https://felixmielcarek.github.io/callback/';
|
||||
//#endregion
|
||||
|
||||
//#region APP INIT
|
||||
|
||||
const app = express()
|
||||
app.listen(port, () => { console.log(`Big brother is listening on port ${port}`) })
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region ACCESS TOKEN
|
||||
|
||||
app.get('/', async (req, res) => {
|
||||
const client = new Client({
|
||||
host: 'felixmielcarek-bigbrotherdb',
|
||||
user: process.env.POSTGRES_USER,
|
||||
database: process.env.POSTGRES_DATABASE,
|
||||
password: process.env.POSTGRES_PASSWORD,
|
||||
port: 5432
|
||||
})
|
||||
|
||||
const code = req.query.code;
|
||||
|
||||
var authOptions = {
|
||||
url: 'https://accounts.spotify.com/api/token', method: 'post', json: true,
|
||||
data: { code: code, redirect_uri: redirectUri, grant_type: 'authorization_code' },
|
||||
headers: {
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
|
||||
}
|
||||
};
|
||||
|
||||
const response1 = await axios(authOptions);
|
||||
const accessToken = response1.data.access_token
|
||||
const refreshToken = response1.data.refresh_token
|
||||
|
||||
try {
|
||||
const response2 = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } });
|
||||
|
||||
const data = {
|
||||
SpotifyId: response2.data.id,
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshToken
|
||||
};
|
||||
|
||||
await client.connect()
|
||||
|
||||
const sqlQuery = `
|
||||
INSERT INTO public.users (spotifyid,accesstoken,refreshtoken)
|
||||
VALUES ($1,$2,$3)
|
||||
ON CONFLICT (spotifyid) DO UPDATE SET
|
||||
accesstoken = EXCLUDED.accesstoken,
|
||||
refreshtoken = EXCLUDED.refreshtoken
|
||||
`;
|
||||
|
||||
client.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (err, res) => {
|
||||
if (err) {
|
||||
console.error('Error executing query', err);
|
||||
return;
|
||||
}
|
||||
console.log('Data inserted/updated successfully');
|
||||
client.end();
|
||||
});
|
||||
} catch (error) { console.log('Error getting user Spotify id') }
|
||||
});
|
||||
//#endregion
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "bigbrother-web",
|
||||
"name": "bigbrother-callback-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "app.js",
|
@ -1,13 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<base href="https://codefirst.iut.uca.fr/containers/felixmielcarek-bigbrotherwebapp/">
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
<title>Big Brother</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>You are connected</div>
|
||||
</body>
|
||||
</html>
|
@ -1,19 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<base href="https://codefirst.iut.uca.fr/containers/felixmielcarek-bigbrotherwebapp/">
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
<title>Big Brother</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>Welcome to Big Brother</div>
|
||||
<button id="login-btn">Log In</button>
|
||||
<script>
|
||||
document.getElementById('login-btn').addEventListener('click', function() {
|
||||
window.location.href = '/login';
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -1,15 +0,0 @@
|
||||
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap');
|
||||
|
||||
html, body {
|
||||
font-family: 'Montserrat', sans-serif;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: lightgrey;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-around;
|
||||
align-items: center;
|
||||
}
|
@ -1,136 +0,0 @@
|
||||
//#region REQUIRE
|
||||
const path = require('path');
|
||||
const express = require('express')
|
||||
//const cookieParser = require('cookie-parser');
|
||||
const axios = require('axios');
|
||||
const queryString = require('querystring');
|
||||
const pg = require('pg');
|
||||
//#endregion
|
||||
|
||||
//#region CONSTANTS
|
||||
const staticDir = path.join(__dirname, '../public');
|
||||
const port = 80
|
||||
const clientId = process.env.CLIENT_ID;
|
||||
const clientSecret = process.env.CLIENT_SECRET;
|
||||
const redirectUri = 'https://codefirst.iut.uca.fr/containers/felixmielcarek-bigbrotherwebapp/callback';
|
||||
const scope = 'user-read-private user-read-email user-library-read user-library-modify';
|
||||
const { Client } = pg
|
||||
const client = new Client({
|
||||
user: process.env.POSTGRES_USER,
|
||||
host: 'felixmielcarek-bigbrotherdb',
|
||||
database: process.env.POSTGRES_DATABASE,
|
||||
password: process.env.POSTGRES_PASSWORD,
|
||||
port: 5432
|
||||
})
|
||||
//#endregion
|
||||
|
||||
//#region VARIABLES
|
||||
let state = '';
|
||||
//#endregion
|
||||
|
||||
//#region APP INIT
|
||||
const app = express()
|
||||
//app.use(cookieParser());
|
||||
app.use(express.static(staticDir));
|
||||
app.get(['/',''], function (req, res) {
|
||||
/*req.cookies['account'] == 'true'
|
||||
? res.sendFile(staticDir + '/home.html')
|
||||
:*/ res.sendFile(staticDir + '/login.html');
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Big brother is listening on port ${port}`);
|
||||
})
|
||||
//#endregion
|
||||
|
||||
//#region USER AUTHORIZATION
|
||||
function generateRandomString(length) {
|
||||
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
let randomString = '';
|
||||
for (let i = 0; i < length; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * charset.length);
|
||||
randomString += charset[randomIndex];
|
||||
}
|
||||
return randomString;
|
||||
}
|
||||
|
||||
app.get('/login', function (req, res) {
|
||||
state = generateRandomString(16);
|
||||
|
||||
res.redirect('https://accounts.spotify.com/authorize?' +
|
||||
queryString.stringify({
|
||||
response_type: 'code',
|
||||
client_id: clientId,
|
||||
scope: scope,
|
||||
redirect_uri: redirectUri,
|
||||
state: state
|
||||
}));
|
||||
});
|
||||
//#endregion
|
||||
|
||||
//#region ACCESS TOKEN
|
||||
app.get('/callback', (req, res) => {
|
||||
//res.cookie('account', 'true', { maxAge: 360000 });
|
||||
|
||||
const code = req.query.code;
|
||||
if(state != req.query.state) {
|
||||
console.error("Spotify state error.")
|
||||
return
|
||||
}
|
||||
|
||||
res.redirect('/');
|
||||
|
||||
var authOptions = {
|
||||
url: 'https://accounts.spotify.com/api/token',
|
||||
method: 'post',
|
||||
data: {
|
||||
code: code,
|
||||
redirect_uri: redirectUri,
|
||||
grant_type: 'authorization_code'
|
||||
},
|
||||
headers: {
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
'Authorization': 'Basic ' + (new Buffer.from(clientId + ':' + clientSecret).toString('base64'))
|
||||
},
|
||||
json: true
|
||||
};
|
||||
|
||||
axios(authOptions)
|
||||
.then(async response => {
|
||||
const accessToken = response.data.access_token
|
||||
const refreshToken = response.data.refresh_token
|
||||
|
||||
try {
|
||||
const response = await axios.get(`https://api.spotify.com/v1/me`, { headers: { 'Authorization': 'Bearer ' + accessToken, } });
|
||||
|
||||
const data = {
|
||||
SpotifyId: response.data.id,
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshToken
|
||||
};
|
||||
|
||||
await client.connect()
|
||||
|
||||
const sqlQuery = `
|
||||
INSERT INTO public.users (spotifyid,accesstoken,refreshtoken)
|
||||
VALUES ($1,$2,$3)
|
||||
ON CONFLICT (spotifyid) DO UPDATE SET
|
||||
accesstoken = EXCLUDED.accesstoken,
|
||||
refreshtoken = EXCLUDED.refreshtoken
|
||||
`;
|
||||
|
||||
client.query(sqlQuery, [data.SpotifyId, data.AccessToken, data.RefreshToken], (err, res) => {
|
||||
if (err) {
|
||||
console.error('Error executing query', err);
|
||||
return;
|
||||
}
|
||||
console.log('Data inserted/updated successfully');
|
||||
client.end();
|
||||
});
|
||||
} catch (error) { console.log('Error getting user Spotify id') }
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('Error:', error);
|
||||
});
|
||||
});
|
||||
//#endregion
|
Loading…
Reference in new issue