Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 38 additions & 39 deletions controllers/spotifyController.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
const spotifyApi = require("../routes/spotifyRoute")
var spotifyData = null;

const spotifyApi = require("../routes/spotifyRoute");
const spotifyData = null;

const spotifyAuth = async (req, res, next) => {
var code = req.query.code;
if (req.query.code !== null) {
var code = req.query.code;
if (req.query.code != null) {
var code = req.query.code;

await spotifyApi.spotifyApi.authorizationCodeGrant(code).then(
data=>{
const access_token = data.body['access_token'];
const refresh_token = data.body['refresh_token'];
spotifyApi.spotifyApi.setAccessToken(access_token);
spotifyApi.spotifyApi.setRefreshToken(refresh_token);
console.log('Spotify Authorized')
}
).catch(err => {
console.log(err);
})
// spotifyApi.spotifyApi.setAccessToken(code);
spotifyApi.spotifyApi.getMyCurrentPlaybackState().then(
function(data) {
if(data.body && data.body.is_playing){
console.log(data.body["item"]["name"])
res.render('index')
} else {
console.log('Nothing is Playing right now');
res.render('index')
}
}, function(err) {
console.log('Something Went Wrong', err)
}
)

}
else {
res.render("index")
}
}

await spotifyApi.spotifyApi
.authorizationCodeGrant(code)
.then((data) => {
const access_token = data.body.access_token;
const refresh_token = data.body.refresh_token;
spotifyApi.spotifyApi.setAccessToken(access_token);
spotifyApi.spotifyApi.setRefreshToken(refresh_token);
console.log("Spotify Authorized");
})
.catch((err) => {
console.log(err);
});
// spotifyApi.spotifyApi.setAccessToken(code);
spotifyApi.spotifyApi.getMyCurrentPlaybackState().then(
function (data) {
if (data.body && data.body.is_playing) {
console.log(data.body.item.name);
res.render("index");
} else {
console.log("Nothing is Playing right now");
res.render("index");
}
},
function (err) {
console.log("Something Went Wrong", err);
}
);
} else {
res.render("index");
}
};

module.exports = {
spotifyAuth,
}
spotifyAuth,
};
96 changes: 47 additions & 49 deletions routes/calendarRoute.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,56 @@
const express = require("express")
const router = express.Router()
const express = require("express");
const router = express.Router();


const { google } = require('googleapis')
const { OAuth2 } = google.auth
const redirect_uri='https://core-prelims.herokuapp.com/dashboard/calendar'
const scopes = ['https://www.googleapis.com/auth/calendar.readonly'];
const { google } = require("googleapis");
const { OAuth2 } = google.auth;
const redirect_uri = "https://core-prelims.herokuapp.com/dashboard/calendar";
const scopes = ["https://www.googleapis.com/auth/calendar.readonly"];
const oAuth2Client = new OAuth2(
process.env.CALENDAR_CLIENT_ID,
process.env.CALENDAR_CLIENT_SECRET,
redirect_uri
)

);

router.get("/", (req, res) => {
const code = req.query.code;
if (code !== null) {
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error("Error Retrieveing code", err);
oAuth2Client.setCredentials(token);
const calendar = google.calendar({ version: "v3", auth: oAuth2Client });
calendar.events.list(
{
calendarId: "primary",
timeMin: new Date().toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: "startTime",
},
(err, res) => {
if (err) return console.log("The API returned an error: " + err);
const events = res.data.items;
if (events.length) {
console.log("Upcoming 10 events:");
events.map((event, i) => {
const start = event.start.dateTime || event.start.date;
console.log(`${start} - ${event.summary}`);
});
} else {
console.log("No upcoming events found.");
}
}
);
});
}
res.render("calendar");
});

router.get("/", (req, res)=>{
let code = req.query.code
if(code != null) {
oAuth2Client.getToken(code, (err,token) => {
if (err) return console.error('Error Retrieveing code', err)
oAuth2Client.setCredentials(token)
const calendar = google.calendar({version: "v3",auth:oAuth2Client});
calendar.events.list({
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const events = res.data.items;
if (events.length) {
console.log('Upcoming 10 events:');
events.map((event, i) => {
const start = event.start.dateTime || event.start.date;
console.log(`${start} - ${event.summary}`);
});
} else {
console.log('No upcoming events found.');
}
});
})
}
res.render("calendar")
})

router.post('/connect', (req,res)=>{


router.post("/connect", (req, res) => {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
access_type: "offline",
scope: scopes,
});
console.log(authUrl)
res.redirect(authUrl)
})
module.exports = router;
console.log(authUrl);
res.redirect(authUrl);
});
module.exports = router;
Loading