#52 Fixed login flash messages
parent
c71c1fd1c1
commit
b9fe85c07f
|
@ -93,6 +93,8 @@ module.exports = (passport)=>{
|
||||||
user.save()
|
user.save()
|
||||||
.then( ()=>{
|
.then( ()=>{
|
||||||
console.info(`🗂️ Lazily updated schema for ${user.name}.`);
|
console.info(`🗂️ Lazily updated schema for ${user.name}.`);
|
||||||
|
req.session.flashType = 'success';
|
||||||
|
req.session.flashMessage = "You have been logged in. ";
|
||||||
return done(null, user);
|
return done(null, user);
|
||||||
})
|
})
|
||||||
.catch( (err)=>{
|
.catch( (err)=>{
|
||||||
|
@ -103,7 +105,8 @@ module.exports = (passport)=>{
|
||||||
|
|
||||||
// No such user
|
// No such user
|
||||||
else {
|
else {
|
||||||
req.flash('warning',`There's no user for that ${service} account. `);
|
req.session.flashType = 'warning';
|
||||||
|
req.session.flashMessage = `There's no user for that ${service} account. `;
|
||||||
return done();
|
return done();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +119,8 @@ module.exports = (passport)=>{
|
||||||
|
|
||||||
// No googleId either
|
// No googleId either
|
||||||
else {
|
else {
|
||||||
req.flash('warning',`There's no user for that ${service} account. `);
|
req.session.flashType = 'warning';
|
||||||
|
req.session.flashMessage = `There's no user for that ${service} account. `;
|
||||||
return done();
|
return done();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,6 +128,8 @@ module.exports = (passport)=>{
|
||||||
// Successfull social login
|
// Successfull social login
|
||||||
else {
|
else {
|
||||||
// console.log(`Found user: ${user}`);
|
// console.log(`Found user: ${user}`);
|
||||||
|
req.session.flashType = 'success';
|
||||||
|
req.session.flashMessage = "You have been logged in.";
|
||||||
return done(null, user);
|
return done(null, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +142,7 @@ module.exports = (passport)=>{
|
||||||
|
|
||||||
// Intent to connect account
|
// Intent to connect account
|
||||||
else {
|
else {
|
||||||
// console.log(`Connecting ${service} account...`);
|
// console.log(`Attempting to connect ${service} account...`);
|
||||||
|
|
||||||
// Check for unique profileId
|
// Check for unique profileId
|
||||||
User.findOne(query)
|
User.findOne(query)
|
||||||
|
@ -144,7 +150,9 @@ module.exports = (passport)=>{
|
||||||
|
|
||||||
// Social account already in use
|
// Social account already in use
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
req.flash('warning',`Another user is already connected to that ${service} account. `);
|
// console.log(`${service} account already in use.`);
|
||||||
|
req.session.flashType = 'warning';
|
||||||
|
req.session.flashMessage = `Another user is already connected to that ${service} account. `;
|
||||||
return done();
|
return done();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,7 +162,8 @@ module.exports = (passport)=>{
|
||||||
req.user.auth[service] = profileId;
|
req.user.auth[service] = profileId;
|
||||||
req.user.save()
|
req.user.save()
|
||||||
.then( ()=>{
|
.then( ()=>{
|
||||||
req.flash('success', `${mw.capitalize(service)} account connected. `);
|
req.session.flashType = 'success';
|
||||||
|
req.session.flashMessage = `${mw.capitalize(service)} account connected. `;
|
||||||
return done(null,req.user);
|
return done(null,req.user);
|
||||||
} )
|
} )
|
||||||
.catch( (err)=>{
|
.catch( (err)=>{
|
||||||
|
@ -167,7 +176,7 @@ module.exports = (passport)=>{
|
||||||
.catch( (err)=>{
|
.catch( (err)=>{
|
||||||
mw.throwErr(err,req);
|
mw.throwErr(err,req);
|
||||||
return done(err);
|
return done(err);
|
||||||
})
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,9 @@ module.exports = (app, passport) => {
|
||||||
},
|
},
|
||||||
loginCallback = (req,res)=>{
|
loginCallback = (req,res)=>{
|
||||||
// console.log(`Login callback called... redirecting to ${req.session.next}`);
|
// console.log(`Login callback called... redirecting to ${req.session.next}`);
|
||||||
req.flash('success',"You have been logged in.");
|
req.flash(req.session.flashType,req.session.flashMessage);
|
||||||
|
req.session.flashType = undefined;
|
||||||
|
req.session.flashMessage = undefined;
|
||||||
res.redirect( req.session.next || '/map' );
|
res.redirect( req.session.next || '/map' );
|
||||||
},
|
},
|
||||||
appLoginCallback = (req,res)=>{
|
appLoginCallback = (req,res)=>{
|
||||||
|
@ -84,86 +86,84 @@ module.exports = (app, passport) => {
|
||||||
|
|
||||||
// Check if somebody already has that email
|
// Check if somebody already has that email
|
||||||
User.findOne({'email':req.body.email})
|
User.findOne({'email':req.body.email})
|
||||||
.then( (user)=>{
|
.then( (user)=>{
|
||||||
|
|
||||||
|
// User already exists
|
||||||
|
if (user && user.auth.password) {
|
||||||
|
req.flash('warning','A user with that email already exists! If you forgot your password, you can <a href="/login/forgot">reset it here</a>.');
|
||||||
|
res.redirect('/login#login');
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
// User exists but hasn't created a password yet
|
||||||
|
else if (user) {
|
||||||
|
// Send another token (or the same one if it hasn't expired)
|
||||||
|
sendToken(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create user
|
||||||
|
else {
|
||||||
|
|
||||||
// User already exists
|
user = new User();
|
||||||
if (user && user.auth.password) {
|
user.created = Date.now();
|
||||||
req.flash('warning','A user with that email already exists! If you forgot your password, you can <a href="/login/forgot">reset it here</a>.');
|
user.email = req.body.email;
|
||||||
res.redirect('/login#login');
|
user.slug = slug(user.email.substring(0, user.email.indexOf('@')));
|
||||||
next();
|
|
||||||
}
|
|
||||||
|
|
||||||
// User exists but hasn't created a password yet
|
// Generate unique slug
|
||||||
else if (user) {
|
let slug = new Promise((resolve,reject) => {
|
||||||
// Send another token (or the same one if it hasn't expired)
|
(function checkSlug(s,cb){
|
||||||
sendToken(user);
|
|
||||||
}
|
User.findOne({slug:s})
|
||||||
|
.then((existingUser)=>{
|
||||||
// Create user
|
|
||||||
else {
|
|
||||||
|
|
||||||
user = new User();
|
|
||||||
user.created = Date.now();
|
|
||||||
user.email = req.body.email;
|
|
||||||
user.slug = slug(user.email.substring(0, user.email.indexOf('@')));
|
|
||||||
|
|
||||||
// Generate unique slug
|
|
||||||
let slug = new Promise((resolve,reject) => {
|
|
||||||
(function checkSlug(s,cb){
|
|
||||||
|
|
||||||
User.findOne({slug:s})
|
// Slug in use: generate a random one and retry
|
||||||
.catch((err)=>{
|
if (existingUser){
|
||||||
mw.throwErr(err,req);
|
crypto.randomBytes(6, (err,buf)=>{
|
||||||
})
|
if (err) { mw.throwErr(err,req); }
|
||||||
.then((existingUser)=>{
|
s = buf.toString('hex');
|
||||||
|
checkSlug(s,cb);
|
||||||
// Slug in use: generate a random one and retry
|
});
|
||||||
if (existingUser){
|
}
|
||||||
crypto.randomBytes(6, (err,buf)=>{
|
|
||||||
if (err) { mw.throwErr(err,req); }
|
|
||||||
s = buf.toString('hex');
|
|
||||||
checkSlug(s,cb);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unique slug: proceed
|
|
||||||
else { cb(s); }
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
})(user.slug, (newSlug)=>{
|
// Unique slug: proceed
|
||||||
user.slug = newSlug;
|
else { cb(s); }
|
||||||
resolve();
|
|
||||||
});
|
})
|
||||||
});
|
.catch((err)=>{
|
||||||
|
|
||||||
// Generate sk32
|
|
||||||
let sk32 = new Promise((resolve,reject) => {
|
|
||||||
crypto.randomBytes(32, (err,buf)=>{
|
|
||||||
if (err) { mw.throwErr(err,req); }
|
|
||||||
user.sk32 = buf.toString('hex');
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Save user and send the token by email
|
|
||||||
Promise.all([slug, sk32])
|
|
||||||
.then( ()=> {
|
|
||||||
user.save();
|
|
||||||
}).then( ()=>{
|
|
||||||
sendToken(user);
|
|
||||||
}).catch( (err)=>{
|
|
||||||
mw.throwErr(err,req);
|
mw.throwErr(err,req);
|
||||||
res.redirect('/login#signup');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
})(user.slug, (newSlug)=>{
|
||||||
|
user.slug = newSlug;
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
})
|
// Generate sk32
|
||||||
.catch( (err)=>{
|
let sk32 = new Promise((resolve,reject) => {
|
||||||
mw.throwErr(err,req);
|
crypto.randomBytes(32, (err,buf)=>{
|
||||||
res.redirect('/signup');
|
if (err) { mw.throwErr(err,req); }
|
||||||
});
|
user.sk32 = buf.toString('hex');
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Save user and send the token by email
|
||||||
|
Promise.all([slug, sk32])
|
||||||
|
.then( ()=>{ user.save(); })
|
||||||
|
.then( ()=>{ sendToken(user); })
|
||||||
|
.catch( (err)=>{
|
||||||
|
mw.throwErr(err,req);
|
||||||
|
res.redirect('/login#signup');
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
.catch( (err)=>{
|
||||||
|
mw.throwErr(err,req);
|
||||||
|
res.redirect('/signup');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -228,10 +228,10 @@ module.exports = (app, passport) => {
|
||||||
// Android
|
// Android
|
||||||
app.get('/login/app/', passport.authenticate('local'), appLoginCallback);
|
app.get('/login/app/', passport.authenticate('local'), appLoginCallback);
|
||||||
|
|
||||||
// Token-based
|
// Token-based (android social)
|
||||||
app.get(['/login/app/google','/auth/google/idtoken'], passport.authenticate('google-token'), appLoginCallback);
|
app.get(['/login/app/google','/auth/google/idtoken'], passport.authenticate('google-token'), appLoginCallback);
|
||||||
app.get('/login/app/facebook', passport.authenticate('facebook-token'), appLoginCallback);
|
app.get('/login/app/facebook', passport.authenticate('facebook-token'), appLoginCallback);
|
||||||
app.get('/login/app/twitter', passport.authenticate('twitter-token'), appLoginCallback);
|
app.get('/login/app/twitter', passport.authenticate('twitter-token'), appLoginCallback);
|
||||||
|
|
||||||
// Social
|
// Social
|
||||||
app.get('/login/:service', (req,res,next)=>{
|
app.get('/login/:service', (req,res,next)=>{
|
||||||
|
@ -266,17 +266,8 @@ module.exports = (app, passport) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
app.get('/login/google/cb',
|
app.get('/login/google/cb', passport.authenticate('google',loginOutcome), loginCallback );
|
||||||
passport.authenticate('google',loginOutcome),
|
app.get('/login/facebook/cb', passport.authenticate('facebook',loginOutcome), loginCallback );
|
||||||
loginCallback
|
app.get('/login/twitter/cb', passport.authenticate('twitter',loginOutcome), loginCallback );
|
||||||
);
|
|
||||||
app.get('/login/facebook/cb',
|
|
||||||
passport.authenticate('facebook',loginOutcome),
|
|
||||||
loginCallback
|
|
||||||
);
|
|
||||||
app.get('/login/twitter/cb',
|
|
||||||
passport.authenticate('twitter',loginOutcome),
|
|
||||||
loginCallback
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -82,7 +82,7 @@ const
|
||||||
// Path for redirects
|
// Path for redirects
|
||||||
let nextPath = ( req.path.substring(0, req.path.indexOf('#')) || req.path );
|
let nextPath = ( req.path.substring(0, req.path.indexOf('#')) || req.path );
|
||||||
if ( nextPath.substring(0,6)!=='/login' && nextPath.substring(0,7)!=='/logout' ){
|
if ( nextPath.substring(0,6)!=='/login' && nextPath.substring(0,7)!=='/logout' ){
|
||||||
console.log(`Setting redirect path to "${nextPath}#"`);
|
// console.log(`Setting redirect path to ${nextPath}#`);
|
||||||
req.session.next = nextPath+'#';
|
req.session.next = nextPath+'#';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue