Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix v3 endpoints #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TIMES=2
SESSION_SECRET=This is my funky secret oh my god it has ninja turtles
CLEVER_CLIENT_SECRET=<YOUR-CLIENT-SECRET>
CLEVER_CLIENT_ID=<YOUR-CLIENT_ID>
REDIRECT_URL=http://localhost:5000/oauth
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ npm-debug.log
# Docker
Dockerfile
docker-compose.yml

#Environment
.env
14 changes: 11 additions & 3 deletions clever.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ REDIRECT_URL = process.env.REDIRECT_URL;
async function runOAuthFlow(code) {
console.log("getting token");
const token = await getToken(code);
console.log("1: " + token)

console.log("getting me");
const response = await request('/me', token);
Expand All @@ -24,7 +25,8 @@ async function runOAuthFlow(code) {
}

async function getToken(code) {
var auth = new Buffer(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64');
var auth = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64');

var token_options = {
url: 'https://clever.com/oauth/tokens',
headers: { 'Authorization': 'Basic ' + auth },
Expand All @@ -42,7 +44,7 @@ async function getToken(code) {
}

async function request(route, token) {
var url = `https://api.clever.com/v2.0${route}`;
var url = `https://api.clever.com/v3.0${route}`;

const data = req({
url: url,
Expand All @@ -57,6 +59,8 @@ async function getMyInfo(user_id, user_type, token) {
var route = `/${user_type}s/${user_id}`;
const response = await request(route, token);

console.log("Getting Info" + JSON.stringify(response));

return response["data"];
}

Expand All @@ -69,7 +73,9 @@ async function getMySections(user_id, user_type, token) {

async function getMySectionsWithStudents(user_id, user_type, token) {
var route = `/${user_type}s/${user_id}/sections`;

const response = await request(route, token);
console.log("Getting Sections" + JSON.stringify(response));

const sections = response["data"];

Expand All @@ -82,9 +88,11 @@ async function getMySectionsWithStudents(user_id, user_type, token) {
}

async function getStudentsForSection(section_id, token) {
var route = `/sections/${section_id}/students`;
var route = `/sections/${section_id}/users`;
const response = await request(route, token);

console.log("Getting Users for Section" + JSON.stringify(response));

return response["data"];
}

Expand Down
30 changes: 19 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path')
// Making use of a lightweight library to wrap common Clever requests
// For Clever libraries in other languages, see https://dev.clever.com/docs/libraries
const clever = require("./clever");
require('dotenv').config();

const PORT = process.env.PORT || 5000

Expand Down Expand Up @@ -42,6 +43,8 @@ app.get('/', (req, res) => {

const { me, user_type, sections } = req.session;

console.log("Redirecting...");

// Pass this data to the html template to show to the user
return res.render("index", {
me,
Expand Down Expand Up @@ -71,17 +74,22 @@ app.get('/signup', (req, res) => {
// Receiving the login back from Clever - we want to exchange the oauth code for a token that can be used to fetch
// information about the user, like their name and what classes they teach
app.get('/oauth', async (req, res) => {
var code = req.query.code;
const user = await clever.runOAuthFlow(code);

// If we already have the user, log them in, otherwise create the new user
if (userAlreadyExists(user)) {
// Great! The user already exists, so log them in
logInUser(req, user);
} else {
// We have a new user, create the user and log them in
await createUserAndSaveToDB(req, user);
logInUser(req, user);
try{
var code = req.query.code;
const user = await clever.runOAuthFlow(code);

// If we already have the user, log them in, otherwise create the new user
if (userAlreadyExists(user)) {
// Great! The user already exists, so log them in
logInUser(req, user);
} else {
// We have a new user, create the user and log them in
await createUserAndSaveToDB(req, user);
logInUser(req, user);
}
}
catch(e){
console.log(e)
}

// Now go back to the main page
Expand Down
Loading