Improved debugging for passport

master
Keith Irwin 2017-07-14 18:12:06 -04:00
parent 86c537ab37
commit fdf43281bf
No known key found for this signature in database
GPG Key ID: 378933C743E2BBC0
3 changed files with 20 additions and 12 deletions

View File

@ -1,6 +1,8 @@
# Tracman Server Changelog # Tracman Server Changelog
###### v 0.7.3 ###### v 0.7.3
* Improved debugging
#### v0.7.3 #### v0.7.3
* Fixed [#102](https://github.com/Tracman-org/Server/issues/102) issue creating account * Fixed [#102](https://github.com/Tracman-org/Server/issues/102) issue creating account

View File

@ -52,6 +52,8 @@ Tracman will be updated according to [this branching model](http://nvie.com/post
[view full changelog](CHANGELOG.md) [view full changelog](CHANGELOG.md)
* Improved debugging
#### v0.7.3 #### v0.7.3
* Fixed [#102](https://github.com/Tracman-org/Server/issues/102) issue creating account * Fixed [#102](https://github.com/Tracman-org/Server/issues/102) issue creating account

View File

@ -74,13 +74,13 @@ module.exports = (passport)=>{
// Social login // Social login
function socialLogin(req, service, profileId, done) { function socialLogin(req, service, profileId, done) {
debug(`socialLogin() called`); debug(`socialLogin() called for ${service} account ${profileId}`);
let query = {}; let query = {};
query['auth.'+service] = profileId; query['auth.'+service] = profileId;
// Intent to log in // Intent to log in
if (!req.user) { if (!req.user) {
debug(`Logging in with ${service}...`); debug(`Searching for user with query ${query}...`);
User.findOne(query) User.findOne(query)
.then( (user)=>{ .then( (user)=>{
@ -89,16 +89,17 @@ module.exports = (passport)=>{
// Lazy update from old googleId field // Lazy update from old googleId field
if (service==='google') { if (service==='google') {
User.findOne({ 'googleID': parseInt(profileId) }) User.findOne({ 'googleID': parseInt(profileId,10) })
.then( (user)=>{ .then( (user)=>{
// User exists with old schema // User exists with old schema
if (user) { if (user) {
debug(`User ${user.id} exists with old schema. Lazily updating...`);
user.auth.google = profileId; user.auth.google = profileId;
user.googleId = undefined; user.googleId = undefined;
user.save() user.save()
.then( ()=>{ .then( ()=>{
console.info(`🗂️ Lazily updated schema for ${user.name}.`); debug(`Lazily updated ${user.id}...`);
req.session.flashType = 'success'; req.session.flashType = 'success';
req.session.flashMessage = "You have been logged in. "; req.session.flashMessage = "You have been logged in. ";
return done(null, user); return done(null, user);
@ -112,13 +113,14 @@ module.exports = (passport)=>{
// No such user // No such user
else { else {
debug(`User with ${service} account of ${profileId} not found.`);
req.flash('warning', `There's no user for that ${service} account. `); req.flash('warning', `There's no user for that ${service} account. `);
return done(); return done();
} }
}) })
.catch ( (err)=>{ .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); mw.throwErr(err,req);
return done(err); return done(err);
}); });
@ -126,7 +128,7 @@ module.exports = (passport)=>{
// No googleId either // No googleId either
else { 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. `); req.flash('warning', `There's no user for that ${service} account. `);
return done(); return done();
} }
@ -134,7 +136,7 @@ module.exports = (passport)=>{
// Successfull social login // Successfull social login
else { else {
debug(`Found user: ${user}`); debug(`Found user: ${user.id}; logging in...`);
req.session.flashType = 'success'; req.session.flashType = 'success';
req.session.flashMessage = "You have been logged in."; req.session.flashMessage = "You have been logged in.";
return done(null, user); return done(null, user);
@ -150,16 +152,16 @@ module.exports = (passport)=>{
// Intent to connect account // Intent to connect account
else { else {
debug(`Attempting to connect ${service} account...`); debug(`Attempting to connect ${service} account to ${req.user.id}...`);
// Check for unique profileId // Check for unique profileId
debug(`Checking for unique profileId...`); debug(`Checking for unique account with query ${query}...`);
User.findOne(query) User.findOne(query)
.then( (existingUser)=>{ .then( (existingUser)=>{
// Social account already in use // Social account already in use
if (existingUser) { if (existingUser) {
debug(`${service} account already in use.`); debug(`${service} account already in use with user ${existingUser.id}`);
req.session.flashType = 'warning'; req.session.flashType = 'warning';
req.session.flashMessage = `Another user is already connected to that ${service} account. `; req.session.flashMessage = `Another user is already connected to that ${service} account. `;
return done(); return done();
@ -167,22 +169,24 @@ module.exports = (passport)=>{
// Connect to account // Connect to account
else { else {
debug(`Connecting ${service} account.`); debug(`${service} account (${profileId}) is unique; Connecting to ${req.user.id}...`);
req.user.auth[service] = profileId; req.user.auth[service] = profileId;
req.user.save() req.user.save()
.then( ()=>{ .then( ()=>{
debug(`Successfully connected ${service} account to ${req.user.id}`);
req.session.flashType = 'success'; req.session.flashType = 'success';
req.session.flashMessage = `${mw.capitalize(service)} account connected. `; req.session.flashMessage = `${mw.capitalize(service)} account connected. `;
return done(null,req.user); return done(null,req.user);
} ) } )
.catch( (err)=>{ .catch( (err)=>{
debug(`Failed to connect ${service} account to ${req.user.id}!`);
return done(err); return done(err);
} ); } );
} }
}) })
.catch( (err)=>{ .catch( (err)=>{
debug(`Failed to check for unique profileId!`); debug(`Failed to check for unique ${service} profileId of ${profileId}!`);
mw.throwErr(err,req); mw.throwErr(err,req);
return done(err); return done(err);
}); });