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
###### v 0.7.3
* Improved debugging
#### v0.7.3
* 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)
* Improved debugging
#### v0.7.3
* Fixed [#102](https://github.com/Tracman-org/Server/issues/102) issue creating account

View File

@ -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);
});