0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/frontend/services/routing/controllers/unsubscribe.js
Rishabh 2c2099b87f Added homepage redirect for unsubscribe urls
refs https://github.com/TryGhost/Team/issues/1495

With multiple newsletters, members are allowed to manage their newsletter pref via email unsubscribe link with member uuid. Since Portal is now taking over handling unsubscribe for members, we don't need to keep the current `/unsubscribe` page as Portal can load the member's newsletter pref on site home page directly.
The redirect change is only enabled behind the `multipleNewslettersUI` flag as its in beta.
2022-04-28 11:49:20 +01:00

48 lines
1.5 KiB
JavaScript

const debug = require('@tryghost/debug')('services:routing:controllers:unsubscribe');
const path = require('path');
const url = require('url');
const urlUtils = require('../../../../shared/url-utils');
const megaService = require('../../../../server/services/mega');
const renderer = require('../../rendering');
const labs = require('../../../../shared/labs');
module.exports = async function unsubscribeController(req, res) {
debug('unsubscribeController');
if (labs.isSet('multipleNewslettersUI')) {
const {query} = url.parse(req.url, true);
if (!query || !query.uuid) {
res.writeHead(400);
return res.end('Email address not found.');
}
const redirectUrl = new URL(urlUtils.urlFor('home', true));
redirectUrl.searchParams.append('uuid', query.uuid);
if (query.newsletter) {
redirectUrl.searchParams.append('newsletter', query.newsletter);
}
redirectUrl.searchParams.append('action', 'unsubscribe');
return res.redirect(302, redirectUrl.href);
}
let data = {};
try {
data.member = await megaService.mega.handleUnsubscribeRequest(req);
} catch (err) {
data.error = err.message;
}
const templateName = 'unsubscribe';
res.routerOptions = {
type: 'custom',
templates: templateName,
defaultTemplate: path.resolve(__dirname, '../../../views/', templateName)
};
return renderer.renderer(req, res, data);
};