From fdf43281bfd8051b37a2fb2c9afceb12af0d0c71 Mon Sep 17 00:00:00 2001 From: Keith Irwin Date: Fri, 14 Jul 2017 18:12:06 -0400 Subject: [PATCH] Improved debugging for passport --- CHANGELOG.md | 2 ++ README.md | 2 ++ config/passport.js | 28 ++++++++++++++++------------ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d4ac3c..268fe2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Tracman Server Changelog ###### v 0.7.3 +* Improved debugging + #### v0.7.3 * Fixed [#102](https://github.com/Tracman-org/Server/issues/102) issue creating account diff --git a/README.md b/README.md index f75104f..6dc679a 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,8 @@ Tracman will be updated according to [this branching model](http://nvie.com/post [view full changelog](CHANGELOG.md) +* Improved debugging + #### v0.7.3 * Fixed [#102](https://github.com/Tracman-org/Server/issues/102) issue creating account diff --git a/config/passport.js b/config/passport.js index 045699b..5b19619 100644 --- a/config/passport.js +++ b/config/passport.js @@ -74,13 +74,13 @@ module.exports = (passport)=>{ // Social login function socialLogin(req, service, profileId, done) { - debug(`socialLogin() called`); + debug(`socialLogin() called for ${service} account ${profileId}`); let query = {}; query['auth.'+service] = profileId; // Intent to log in if (!req.user) { - debug(`Logging in with ${service}...`); + debug(`Searching for user with query ${query}...`); User.findOne(query) .then( (user)=>{ @@ -89,16 +89,17 @@ module.exports = (passport)=>{ // Lazy update from old googleId field if (service==='google') { - User.findOne({ 'googleID': parseInt(profileId) }) + User.findOne({ 'googleID': parseInt(profileId,10) }) .then( (user)=>{ // User exists with old schema if (user) { + debug(`User ${user.id} exists with old schema. Lazily updating...`); user.auth.google = profileId; user.googleId = undefined; user.save() .then( ()=>{ - console.info(`🗂️ Lazily updated schema for ${user.name}.`); + debug(`Lazily updated ${user.id}...`); req.session.flashType = 'success'; req.session.flashMessage = "You have been logged in. "; return done(null, user); @@ -112,13 +113,14 @@ module.exports = (passport)=>{ // No such user else { + debug(`User with ${service} account of ${profileId} not found.`); req.flash('warning', `There's no user for that ${service} account. `); return done(); } }) .catch ( (err)=>{ - debug(`Failed to search for user with old googleID field. `); + debug(`Failed to search for user with old googleID of ${profileId}. `); mw.throwErr(err,req); return done(err); }); @@ -126,7 +128,7 @@ module.exports = (passport)=>{ // No googleId either else { - debug(`Couldn't find ${service} user.`); + debug(`Couldn't find ${service} user with profileID ${profileId}.`); req.flash('warning', `There's no user for that ${service} account. `); return done(); } @@ -134,7 +136,7 @@ module.exports = (passport)=>{ // Successfull social login else { - debug(`Found user: ${user}`); + debug(`Found user: ${user.id}; logging in...`); req.session.flashType = 'success'; req.session.flashMessage = "You have been logged in."; return done(null, user); @@ -150,16 +152,16 @@ module.exports = (passport)=>{ // Intent to connect account else { - debug(`Attempting to connect ${service} account...`); + debug(`Attempting to connect ${service} account to ${req.user.id}...`); // Check for unique profileId - debug(`Checking for unique profileId...`); + debug(`Checking for unique account with query ${query}...`); User.findOne(query) .then( (existingUser)=>{ // Social account already in use if (existingUser) { - debug(`${service} account already in use.`); + debug(`${service} account already in use with user ${existingUser.id}`); req.session.flashType = 'warning'; req.session.flashMessage = `Another user is already connected to that ${service} account. `; return done(); @@ -167,22 +169,24 @@ module.exports = (passport)=>{ // Connect to account else { - debug(`Connecting ${service} account.`); + debug(`${service} account (${profileId}) is unique; Connecting to ${req.user.id}...`); req.user.auth[service] = profileId; req.user.save() .then( ()=>{ + debug(`Successfully connected ${service} account to ${req.user.id}`); req.session.flashType = 'success'; req.session.flashMessage = `${mw.capitalize(service)} account connected. `; return done(null,req.user); } ) .catch( (err)=>{ + debug(`Failed to connect ${service} account to ${req.user.id}!`); return done(err); } ); } }) .catch( (err)=>{ - debug(`Failed to check for unique profileId!`); + debug(`Failed to check for unique ${service} profileId of ${profileId}!`); mw.throwErr(err,req); return done(err); });