Files
benchcoach/routes/auth.js
2023-08-17 06:51:08 -05:00

129 lines
4.3 KiB
JavaScript

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;