first commit
This commit is contained in:
128
routes/auth.js
Normal file
128
routes/auth.js
Normal file
@@ -0,0 +1,128 @@
|
||||
var express = require("express");
|
||||
var passport = require("passport");
|
||||
var TeamsnapStrategy = require("passport-teamsnap");
|
||||
|
||||
// Configure the TeamSnap strategy for use by Passport.
|
||||
//
|
||||
// OAuth 2.0-based strategies require a `verify` function which receives the
|
||||
// credential (`accessToken`) for accessing the Facebook API on the user's
|
||||
// behalf, along with the user's profile. The function must invoke `cb`
|
||||
// with a user object, which will be set at `req.user` in route handlers after
|
||||
// authentication.
|
||||
passport.use(
|
||||
new TeamsnapStrategy(
|
||||
{
|
||||
apiVersion: "3",
|
||||
clientID: process.env["TEAMSNAP_CLIENT_ID"],
|
||||
clientSecret: process.env["TEAMSNAP_CLIENT_SECRET"],
|
||||
callbackURL: "/auth/teamsnap/callback",
|
||||
passReqToCallback: true,
|
||||
},
|
||||
function (req, accessToken, refreshToken, profile, done) {
|
||||
json = JSON.parse(profile._raw);
|
||||
new_profile = { access_token: accessToken };
|
||||
new_profile["id"] = json.collection.items[0].data.filter(
|
||||
(e) => e.name == "id"
|
||||
)[0].value;
|
||||
new_profile["email"] = json.collection.items[0].data.filter(
|
||||
(e) => e.name == "email"
|
||||
)[0].value;
|
||||
new_profile["first_name"] = json.collection.items[0].data.filter(
|
||||
(e) => e.name == "first_name"
|
||||
)[0].value;
|
||||
console.log("LI#35 session is ", req.session);
|
||||
console.log("LI#35 session id is ", req.session.id);
|
||||
req.session.teamsnap_access_token = accessToken;
|
||||
teamsnap.init(process.env["TEAMSNAP_CLIENT_ID"]);
|
||||
teamsnap.auth(accessToken);
|
||||
// teamsnap.enablePersistence();
|
||||
return done(null, new_profile);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// Configure Passport authenticated session persistence.
|
||||
//
|
||||
// In order to restore authentication state across HTTP requests, Passport needs
|
||||
// to serialize users into and deserialize users out of the session. In a
|
||||
// production-quality application, this would typically be as simple as
|
||||
// supplying the user ID when serializing, and querying the user record by ID
|
||||
// from the database when deserializing. However, due to the fact that this
|
||||
// example does not have a database, the complete Facebook profile is serialized
|
||||
// and deserialized.
|
||||
passport.serializeUser(function (user, cb) {
|
||||
process.nextTick(function () {
|
||||
console.log("L#51 serializing user id", user.id);
|
||||
cb(null, {
|
||||
id: user.id,
|
||||
username: user.email,
|
||||
name: user.firstName,
|
||||
accessToken: user.access_token,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
passport.deserializeUser(function (user, cb) {
|
||||
process.nextTick(function () {
|
||||
return cb(null, user);
|
||||
});
|
||||
});
|
||||
|
||||
var router = express.Router();
|
||||
|
||||
/* GET /login
|
||||
*
|
||||
* This route prompts the user to log in.
|
||||
*
|
||||
* The 'login' view renders an HTML page, which contain a button prompting the
|
||||
* user to sign in with TeamSnap. When the user clicks this button, a request
|
||||
* will be sent to the `GET /login/federated/teamsnap` route.
|
||||
*/
|
||||
router.get("/login", function (req, res, next) {
|
||||
res.render("login");
|
||||
});
|
||||
|
||||
/* GET /login/federated/teamsnap
|
||||
*
|
||||
* This route redirects the user to TeamSnap, where they will authenticate.
|
||||
*
|
||||
* Signing in with TeamSnap is implemented using OAuth 2.0. This route initiates
|
||||
* an OAuth 2.0 flow by redirecting the user to TeamSnap's identity server.
|
||||
* Once there, TeamSnap will authenticate the user
|
||||
* and obtain their consent to release identity information to this app.
|
||||
*
|
||||
* Once TeamSnap has completed their interaction with the user, the user will be
|
||||
* redirected back to the app.
|
||||
*/
|
||||
router.get("/login/federated/teamsnap", passport.authenticate("teamsnap"));
|
||||
|
||||
/*
|
||||
This route completes the authentication sequence when TeamSnap redirects the
|
||||
user back to the application. When a new user signs in, a user account is
|
||||
automatically created and their TeamSnap account is linked. When an existing
|
||||
user returns, they are signed in to their linked account.
|
||||
*/
|
||||
router.get(
|
||||
"/auth/teamsnap",
|
||||
passport.authenticate("teamsnap", function (err, user, info, status) {})
|
||||
);
|
||||
|
||||
router.get("/auth/teamsnap/callback", function (req, res, next) {
|
||||
passport.authenticate("teamsnap", function (err, user, info, status) {
|
||||
if (err) {
|
||||
// do something with the error
|
||||
console.error("error: ", err);
|
||||
}
|
||||
// success
|
||||
console.log("L#105 user is ", user);
|
||||
req.logIn(user, function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
return res.redirect("/");
|
||||
});
|
||||
})(req, res, next);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
390
routes/index.js
Normal file
390
routes/index.js
Normal file
@@ -0,0 +1,390 @@
|
||||
var express = require("express");
|
||||
var ensureLogIn = require("connect-ensure-login").ensureLoggedIn;
|
||||
var papaparse = require("papaparse");
|
||||
|
||||
var ensureLoggedIn = ensureLogIn();
|
||||
|
||||
var router = express.Router();
|
||||
|
||||
function authTeamsnap(user) {
|
||||
if (!teamsnap.isAuthed()) {
|
||||
teamsnap.init(process.env["TEAMSNAP_CLIENT_ID"]);
|
||||
teamsnap.auth(user.accessToken);
|
||||
}
|
||||
}
|
||||
|
||||
function availabilitiesSort(a, b) {
|
||||
status_code_sort = [
|
||||
teamsnap.AVAILABILITIES.YES,
|
||||
teamsnap.AVAILABILITIES.MAYBE,
|
||||
teamsnap.AVAILABILITIES.NO,
|
||||
teamsnap.AVAILABILITIES.NONE,
|
||||
];
|
||||
a_sort = status_code_sort.indexOf(a.statusCode);
|
||||
b_sort = status_code_sort.indexOf(b.statusCode);
|
||||
if (a_sort > b_sort) {
|
||||
return 1;
|
||||
}
|
||||
if (a_sort < b_sort) {
|
||||
return -1;
|
||||
}
|
||||
if (a_sort == b_sort) {
|
||||
if (a.member.lastName < b.member.lastName) {
|
||||
return -1;
|
||||
}
|
||||
if (a.member.lastName > b.member.lastName) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function fetch_stats(resolve, reject) {
|
||||
url =
|
||||
"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv&gid={tab_id}";
|
||||
papaparse.Papa.parse(url, {
|
||||
download: true,
|
||||
complete: function (results) {
|
||||
results.data.forEach((row, i) => {
|
||||
if (i == 0 || row[2] == "Totals" || row[2] == "") {
|
||||
return;
|
||||
}
|
||||
d = {
|
||||
first_name: row[3],
|
||||
last_name: row[2],
|
||||
jersey_number: row[1],
|
||||
pa: row[5],
|
||||
ab: row[6],
|
||||
avg: row[20],
|
||||
obp: row[21],
|
||||
slg: row[22],
|
||||
};
|
||||
});
|
||||
resolve(d);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/* GET home page. */
|
||||
router.get("/", ensureLoggedIn, function (req, res, next) {
|
||||
if (req.user) {
|
||||
authTeamsnap(req.user);
|
||||
teamsnap.loadCollections(function (err) {
|
||||
if (err) {
|
||||
alert("Error loading TeamSnap SDK");
|
||||
return;
|
||||
}
|
||||
teamsnap.loadTeams(function onTeamsLoad(err, teams) {
|
||||
teams = teams.sort((a, b) => b.seasonName - a.seasonName);
|
||||
res.render("home", { req: req, teams: teams });
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.render("home", { req: req });
|
||||
}
|
||||
});
|
||||
|
||||
router.get(
|
||||
"/teams",
|
||||
ensureLoggedIn,
|
||||
function (req, res, next) {
|
||||
console.log("teamsnap authed?: ", teamsnap.isAuthed());
|
||||
console.log("user is", req.user);
|
||||
|
||||
authTeamsnap(req.user);
|
||||
teamsnap.loadCollections(function (err) {
|
||||
if (err) {
|
||||
alert("Error loading TeamSnap SDK");
|
||||
return;
|
||||
}
|
||||
teamsnap.loadTeams(function onTeamsLoad(err, teams) {
|
||||
teams = teams.sort((a, b) => b.seasonName - a.seasonName);
|
||||
res.render("teams", { teams: teams });
|
||||
});
|
||||
});
|
||||
|
||||
next();
|
||||
},
|
||||
function (req, res, next) {
|
||||
// res.send(`${me.firstName} ${me.lastName}`);
|
||||
}
|
||||
);
|
||||
|
||||
router.get("/:team_id([0-9]+)", ensureLoggedIn, function (req, res, next) {
|
||||
authTeamsnap(req.user);
|
||||
team_id = req.params.team_id;
|
||||
console.log("team_id", team_id);
|
||||
teamsnap.loadCollections(function (err) {
|
||||
if (err) {
|
||||
alert("Error loading TeamSnap SDK");
|
||||
return;
|
||||
}
|
||||
teamsnap.enablePersistence();
|
||||
|
||||
teamsnap.bulkLoad(
|
||||
team_id,
|
||||
["team", "member", "event", "opponent", "availability_summary"],
|
||||
function onBulkLoad(err, items) {
|
||||
team = items.find((i) => (i.type == "team") & (i.id == team_id));
|
||||
console.log(team);
|
||||
res.set("Content-Type", "text/html");
|
||||
res.render("team", { team: team });
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
router.get(
|
||||
"/:team_id/event/:event_id",
|
||||
ensureLoggedIn,
|
||||
function (req, res, next) {
|
||||
authTeamsnap(req.user);
|
||||
var team_id = req.params.team_id;
|
||||
var event_id = req.params.event_id;
|
||||
teamsnap.loadCollections(function (err) {
|
||||
console.log();
|
||||
teamsnap.enablePersistence();
|
||||
|
||||
teamsnap.bulkLoad(
|
||||
team_id,
|
||||
["team", "event", "availabilitySummary"],
|
||||
function (err, items) {
|
||||
if (err) {
|
||||
res.code = 500;
|
||||
res.send(err);
|
||||
}
|
||||
|
||||
availabilitySummaries = items.filter(
|
||||
(i) => i.type == "availabilitySummary" && i.id == event_id
|
||||
);
|
||||
events = items.filter((i) => i.type == "event" && i.id == event_id);
|
||||
|
||||
if (events) {
|
||||
event = events[0];
|
||||
availabilitySummary = availabilitySummaries[0];
|
||||
console.log("A_S", availabilitySummaries);
|
||||
res.render("event", {
|
||||
event: event,
|
||||
team_id: team_id,
|
||||
team: items.find((i) => i.type == "team" && i.id == team_id),
|
||||
availabilitySummary: availabilitySummary,
|
||||
});
|
||||
} else {
|
||||
res.code = 500;
|
||||
res.send("error");
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/:team_id/event/:event_id/gamecard",
|
||||
ensureLoggedIn,
|
||||
function (req, res, next) {
|
||||
authTeamsnap(req.user);
|
||||
team_id = req.params.team_id;
|
||||
event_id = req.params.event_id;
|
||||
teamsnap.loadCollections((err) => {
|
||||
teamsnap.enablePersistence();
|
||||
var events;
|
||||
teamsnap
|
||||
.bulkLoad(team_id, [
|
||||
"team",
|
||||
"member",
|
||||
// "member_photos",
|
||||
"event",
|
||||
"opponent",
|
||||
"availability_summary",
|
||||
])
|
||||
.then((items) => {
|
||||
events = items
|
||||
.filter((i) => i.type == "event")
|
||||
.sort((a, b) => a.startDate - b.startDate);
|
||||
event = events.find((i) => i.id == event_id);
|
||||
events_past = events.slice(
|
||||
events.findIndex((e) => e == event) - 4,
|
||||
events.findIndex((e) => e == event)
|
||||
);
|
||||
events_future = events.slice(
|
||||
events.findIndex((e) => e == event) + 1,
|
||||
events.findIndex((e) => e == event) + 5
|
||||
);
|
||||
events = events_past.concat(event).concat(events_future);
|
||||
})
|
||||
.then((items) => {
|
||||
return teamsnap.loadAvailabilities({
|
||||
eventId: events.map((e) => e.id),
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
return teamsnap.collections["eventLineups"]
|
||||
.queryItems("search", {
|
||||
eventId: events.map((e) => e.id),
|
||||
})
|
||||
.then((event_lineups) => {
|
||||
return Promise.all(
|
||||
event_lineups.map((elu) => elu.loadItem("eventLineupEntries"))
|
||||
);
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
items = teamsnap.getAllItems();
|
||||
events = items.filter((i) => i.type == "event");
|
||||
current_event_index = events.findIndex((e) => e.id == event_id);
|
||||
|
||||
context = {
|
||||
team_id: req.params.team_id,
|
||||
event_id: req.params.event_id,
|
||||
current_event_index: current_event_index,
|
||||
events: items.filter((a) => a.type == "event"),
|
||||
availabilitySummaries: items.filter(
|
||||
(i) => i.type == "availabilitySummary"
|
||||
),
|
||||
event: items.find((e) => e.type == "event" && e.id == event_id),
|
||||
events_past: events_past,
|
||||
events_future: events_future,
|
||||
members: items.filter((a) => a.type == "member"),
|
||||
availabilities: items
|
||||
.filter((i) => i.type == "availability")
|
||||
.sort(availabilitiesSort),
|
||||
all_lineup_entries: items.filter(
|
||||
(i) => i.type == "eventLineupEntry"
|
||||
),
|
||||
event_lineup_entries_offense: items
|
||||
.filter(
|
||||
(i) =>
|
||||
i.type == "eventLineupEntry" &&
|
||||
i.eventId == event_id &&
|
||||
!i.label.includes("[PO]")
|
||||
)
|
||||
.sort((a, b) => a.sequence - b.sequence),
|
||||
event_lineup_entries: items
|
||||
.filter(
|
||||
(i) => i.type == "eventLineupEntry" && i.eventId == event_id
|
||||
)
|
||||
.sort((a, b) => a.sequence - b.sequence),
|
||||
};
|
||||
|
||||
res.render("gamecard", context);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/:team_id/event/:event_id/lineup",
|
||||
ensureLoggedIn,
|
||||
function (req, res, next) {
|
||||
authTeamsnap(req.user);
|
||||
team_id = req.params.team_id;
|
||||
event_id = req.params.event_id;
|
||||
teamsnap.loadCollections((err) => {
|
||||
teamsnap.enablePersistence();
|
||||
var events;
|
||||
teamsnap
|
||||
.bulkLoad(team_id, [
|
||||
"team",
|
||||
"member",
|
||||
// "member_photos",
|
||||
"event",
|
||||
"opponent",
|
||||
"availability_summary",
|
||||
])
|
||||
.then((items) => {
|
||||
events = items
|
||||
.filter((i) => i.type == "event")
|
||||
.sort((a, b) => a.startDate - b.startDate);
|
||||
event = events.find((i) => i.id == event_id);
|
||||
events_past = events.slice(
|
||||
events.findIndex((e) => e == event) - 4,
|
||||
events.findIndex((e) => e == event)
|
||||
);
|
||||
events_future = events.slice(
|
||||
events.findIndex((e) => e == event) + 1,
|
||||
events.findIndex((e) => e == event) + 5
|
||||
);
|
||||
events = events_past.concat(event).concat(events_future);
|
||||
})
|
||||
.then((items) => {
|
||||
return teamsnap.loadAvailabilities({
|
||||
eventId: events.map((e) => e.id),
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
return teamsnap.collections["eventLineups"]
|
||||
.queryItems("search", {
|
||||
eventId: events.map((e) => e.id),
|
||||
})
|
||||
.then((event_lineups) => {
|
||||
return Promise.all(
|
||||
event_lineups.map((elu) => elu.loadItem("eventLineupEntries"))
|
||||
);
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
items = teamsnap.getAllItems();
|
||||
events = items.filter((i) => i.type == "event");
|
||||
current_event_index = events.findIndex((e) => e.id == event_id);
|
||||
|
||||
context = {
|
||||
team_id: req.params.team_id,
|
||||
event_id: req.params.event_id,
|
||||
current_event_index: current_event_index,
|
||||
events: items.filter((a) => a.type == "event"),
|
||||
availabilitySummaries: items.filter(
|
||||
(i) => i.type == "availabilitySummary"
|
||||
),
|
||||
event: items.find((e) => e.type == "event" && e.id == event_id),
|
||||
events_past: events_past,
|
||||
events_future: events_future,
|
||||
members: items.filter((a) => a.type == "member"),
|
||||
availabilities: items
|
||||
.filter((i) => i.type == "availability")
|
||||
.sort(availabilitiesSort),
|
||||
all_lineup_entries: items.filter(
|
||||
(i) => i.type == "eventLineupEntry"
|
||||
),
|
||||
event_lineup_entries_offense: items
|
||||
.filter(
|
||||
(i) =>
|
||||
i.type == "eventLineupEntry" &&
|
||||
i.eventId == event_id &&
|
||||
!i.label.includes("[PO]")
|
||||
)
|
||||
.sort((a, b) => a.sequence - b.sequence),
|
||||
event_lineup_entries: items
|
||||
.filter(
|
||||
(i) => i.type == "eventLineupEntry" && i.eventId == event_id
|
||||
)
|
||||
.sort((a, b) => a.sequence - b.sequence),
|
||||
};
|
||||
|
||||
res.render("lineup", context);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
router.get("/:team_id/events", ensureLoggedIn, function (req, res, next) {
|
||||
authTeamsnap(req.user);
|
||||
team_id = req.params.team_id;
|
||||
event_id = req.params.event_id;
|
||||
teamsnap.loadCollections(function (err) {
|
||||
teamsnap
|
||||
.bulkLoad(team_id, ["team", "event", "availability_summary"])
|
||||
.then((items) => {
|
||||
res.set("Content-Type", "text/html");
|
||||
res.render("events", {
|
||||
team: items.find((i) => i.type == "team" && i.id == team_id),
|
||||
events: items.filter((i) => i.type == "event"),
|
||||
availabilitySummaries: items.filter(
|
||||
(i) => i.type == "availabilitySummary"
|
||||
),
|
||||
team_id: team_id,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user