fix(i18n): redirect based on Referer header (#40512)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Mrugesh Mohapatra
2020-12-21 22:23:06 +05:30
committed by Mrugesh Mohapatra
parent a3b1b52cdd
commit 77c46dba2c
10 changed files with 162 additions and 78 deletions

View File

@ -12,12 +12,15 @@ import { ObjectID } from 'mongodb';
import isNumeric from 'validator/lib/isNumeric';
import isURL from 'validator/lib/isURL';
import { homeLocation } from '../../../config/env';
import { ifNoUserSend } from '../utils/middleware';
import { dasherize } from '../../../utils/slugs';
import { fixCompletedChallengeItem } from '../../common/utils';
import { getChallenges } from '../utils/get-curriculum';
import {
getParamsFromReq,
getRedirectBase,
normalizeParams
} from '../utils/get-return-to';
const log = debug('fcc:boot:challenges');
@ -29,7 +32,9 @@ export default async function bootChallenge(app, done) {
await getChallenges()
);
const redirectToCurrentChallenge = createRedirectToCurrentChallenge(
challengeUrlResolver
challengeUrlResolver,
normalizeParams,
getParamsFromReq
);
api.post(
@ -59,7 +64,6 @@ export default async function bootChallenge(app, done) {
app.use(router);
done();
}
const learnURL = `${homeLocation}/learn`;
const jsProjects = [
'aaa48de84e1ecc7c742e1124',
@ -327,15 +331,22 @@ function backendChallengeCompleted(req, res, next) {
.subscribe(() => {}, next);
}
// TODO: extend tests to cover www.freecodecamp.org/language and
// chinese.freecodecamp.org
export function createRedirectToCurrentChallenge(
challengeUrlResolver,
{ _homeLocation = homeLocation, _learnUrl = learnURL } = {}
normalizeParams,
getParamsFromReq
) {
return async function redirectToCurrentChallenge(req, res, next) {
const { user } = req;
const { origin, pathPrefix } = normalizeParams(getParamsFromReq(req));
const redirectBase = getRedirectBase(origin, pathPrefix);
if (!user) {
return res.redirect(_learnUrl);
return res.redirect(redirectBase + '/learn');
}
const challengeId = user && user.currentChallengeId;
const challengeUrl = await challengeUrlResolver(challengeId).catch(next);
if (challengeUrl === '/learn') {
@ -346,6 +357,6 @@ export function createRedirectToCurrentChallenge(
db may not be properly seeded.
`);
}
return res.redirect(`${_homeLocation}${challengeUrl}`);
return res.redirect(`${redirectBase}${challengeUrl}`);
};
}