Merge pull request #5526 from FreeCodeCamp/fix/commit-signin-redirect
Fix commit redirect for non signed user.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { Observable } from 'rx';
|
import { Observable } from 'rx';
|
||||||
import uuid from 'node-uuid';
|
import uuid from 'node-uuid';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
import dedent from 'dedent';
|
||||||
import debugFactory from 'debug';
|
import debugFactory from 'debug';
|
||||||
|
|
||||||
import { saveUser, observeMethod } from '../../server/utils/rx';
|
import { saveUser, observeMethod } from '../../server/utils/rx';
|
||||||
@@ -91,9 +92,10 @@ module.exports = function(User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
req.flash('error', {
|
req.flash('error', {
|
||||||
msg:
|
msg: dedent`
|
||||||
`The ${req.body.email} email address is already associated with an account.
|
The ${req.body.email} email address is already associated with an account.
|
||||||
Try signing in with it here instead.`
|
Try signing in with it here instead.
|
||||||
|
`
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.redirect('/email-signin');
|
return res.redirect('/email-signin');
|
||||||
@@ -171,10 +173,14 @@ module.exports = function(User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return req.logIn({ id: accessToken.userId.toString() }, function(err) {
|
return req.logIn({ id: accessToken.userId.toString() }, function(err) {
|
||||||
if (err) {
|
if (err) { return next(err); }
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
debug('user logged in');
|
debug('user logged in');
|
||||||
|
|
||||||
|
if (req.session && req.session.returnTo) {
|
||||||
|
return res.redirect(req.session.returnTo);
|
||||||
|
}
|
||||||
|
|
||||||
req.flash('success', { msg: 'Success! You are logged in.' });
|
req.flash('success', { msg: 'Success! You are logged in.' });
|
||||||
return res.redirect('/');
|
return res.redirect('/');
|
||||||
});
|
});
|
||||||
|
@@ -56,9 +56,12 @@ module.exports = function(app) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// send welcome email to new camper
|
// send welcome email to new camper
|
||||||
User.afterRemote('create', function(ctx, user, next) {
|
User.afterRemote('create', function({ req, res }, user, next) {
|
||||||
debug('user created, sending email');
|
debug('user created, sending email');
|
||||||
if (!user.email) { return next(); }
|
if (!user.email) { return next(); }
|
||||||
|
const redirect = req.session && req.session.returnTo ?
|
||||||
|
req.session.returnTo :
|
||||||
|
'/';
|
||||||
|
|
||||||
var mailOptions = {
|
var mailOptions = {
|
||||||
type: 'email',
|
type: 'email',
|
||||||
@@ -81,13 +84,13 @@ module.exports = function(app) {
|
|||||||
debug('sending welcome email');
|
debug('sending welcome email');
|
||||||
Email.send(mailOptions, function(err) {
|
Email.send(mailOptions, function(err) {
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
ctx.req.logIn(user, function(err) {
|
req.logIn(user, function(err) {
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
|
|
||||||
ctx.req.flash('success', {
|
req.flash('success', {
|
||||||
msg: [ "Welcome to Free Code Camp! We've created your account." ]
|
msg: [ "Welcome to Free Code Camp! We've created your account." ]
|
||||||
});
|
});
|
||||||
ctx.res.redirect('/');
|
res.redirect(redirect);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -22,11 +22,18 @@ import {
|
|||||||
ifNoUserRedirectTo
|
ifNoUserRedirectTo
|
||||||
} from '../utils/middleware';
|
} from '../utils/middleware';
|
||||||
|
|
||||||
const sendNonUserToFront = ifNoUserRedirectTo('/');
|
const sendNonUserToSignIn = ifNoUserRedirectTo(
|
||||||
|
'/login',
|
||||||
|
'You must be signed in to commit to a nonprofit.',
|
||||||
|
'info'
|
||||||
|
);
|
||||||
|
|
||||||
const sendNonUserToCommit = ifNoUserRedirectTo(
|
const sendNonUserToCommit = ifNoUserRedirectTo(
|
||||||
'/commit',
|
'/commit',
|
||||||
'Must be signed in to update commit'
|
'You must be signed in to update commit',
|
||||||
|
'info'
|
||||||
);
|
);
|
||||||
|
|
||||||
const debug = debugFactory('freecc:commit');
|
const debug = debugFactory('freecc:commit');
|
||||||
|
|
||||||
function findNonprofit(name) {
|
function findNonprofit(name) {
|
||||||
@@ -52,7 +59,7 @@ export default function commit(app) {
|
|||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
'/commit/pledge',
|
'/commit/pledge',
|
||||||
sendNonUserToFront,
|
sendNonUserToSignIn,
|
||||||
pledge
|
pledge
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@@ -16,7 +16,8 @@ const pathsWhiteList = [
|
|||||||
'news',
|
'news',
|
||||||
'challenges',
|
'challenges',
|
||||||
'map',
|
'map',
|
||||||
'news'
|
'news',
|
||||||
|
'commit'
|
||||||
];
|
];
|
||||||
|
|
||||||
const pathsOfNoReturnRegex = new RegExp(pathsOfNoReturn.join('|'), 'i');
|
const pathsOfNoReturnRegex = new RegExp(pathsOfNoReturn.join('|'), 'i');
|
||||||
@@ -35,7 +36,7 @@ export default function addReturnToUrl() {
|
|||||||
) {
|
) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
req.session.returnTo = req.path;
|
req.session.returnTo = req.originalUrl;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,11 @@
|
|||||||
export function ifNoUserRedirectTo(url, message) {
|
export function ifNoUserRedirectTo(url, message, type = 'errors') {
|
||||||
return function(req, res, next) {
|
return function(req, res, next) {
|
||||||
const { path } = req;
|
const { path } = req;
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
req.flash('errors', {
|
req.flash(type, {
|
||||||
msg: message || `You must be signed to go to ${path}`
|
msg: message || `You must be signed to go to ${path}`
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user