const express = require('express'); const router = express.Router(); const axios = require('axios') const {google} = require('googleapis') // TAKEN FROM OUR ALLEGRO APP CONFIGURATION --> https://apps.developer.allegro.pl/ const ALLEGRO_AUTH_TOKEN_URL = "https://allegro.pl/auth/oauth/token" const REDIRECT_URI = "https://dropshipping.xaos.ninja" const CLIENT_ID = "9426c5af899d4de9a513242a8bd9c955" const CLIENT_SECRET = "Wxw96FWT2FNAwkiu39ABhSJqeJX4JJrPpFis6q7kDYGy240cS54Aa0mIzOVgj3jG" // GOOGLE DOCS API const GOOGLE_CLIENT_ID = "811597362147-fekutrahfqdovhl073pdmevpjnctas2t.apps.googleusercontent.com" const GOOGLE_CLIENT_SECRET = "eHig0XC6WhYqaFRX5Mcoxqf9" const SCOPE = ["https://www.googleapis.com/auth/spreadsheets"] const oAuth2Client = new google.auth.OAuth2(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET) /* GET home page. */ router.get('/', function (req, res, next) { const query = req?.query const code = req?.query?.code // when we are redirected from Allegro if (query.code && !query.scope) { const credentials = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString("base64") axios.post(ALLEGRO_AUTH_TOKEN_URL, null, { params: { grant_type: "authorization_code", code: code, redirect_uri: REDIRECT_URI }, headers: {"Authorization": `Basic ${credentials}`} }).then(response => { const {access_token, refresh_token} = response.data res.render('index', { allegro_access_token: access_token, allegro_refresh_token: refresh_token, google_auth_url: "", google_tokens_json: "", }); }) //when we are redirected from Google } else if (query.code && query.scope) { oAuth2Client.getToken({code: code, redirect_uri: REDIRECT_URI}) .then(response => { const google_tokens_json = JSON.stringify(response.tokens) res.render('index', { allegro_access_token: "", allegro_refresh_token: "", google_auth_url: "", google_tokens_json: google_tokens_json }) }) //when we just enter the page } else { const authUrl = oAuth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPE, redirect_uri: REDIRECT_URI, prompt: 'consent' }) res.render('index', { allegro_access_token: "", allegro_refresh_token: "", google_auth_url: authUrl, google_tokens_json: "", }); } }); module.exports = router;