0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added optional data-attribute to enable and disable auto redirection. (#15335)

closes https://github.com/TryGhost/Ghost/issues/15104 https://github.com/TryGhost/Team/issues/1800

- On custom sign up and login forms, creators often wouldn't want their members to be redirected to that page after signing in.
- This takes a new data-attribute value (eg `data-members-autoredirect="false"`) that can be set on [custom sign up / login forms](https://ghost.org/docs/themes/members/#signup-forms) into account before parsing the referrer on the magic link URL that gets sent to the member for login.
This commit is contained in:
Ronald Langeveld 2022-09-06 14:36:06 +02:00 committed by GitHub
parent b226b03f09
commit 1f177e1c17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 3 deletions

View file

@ -55,6 +55,54 @@ describe('sendMagicLink', function () {
});
});
it('Creates a valid magic link from custom signup with redirection', async function () {
const customSignupUrl = 'http://localhost:2368/custom-signup-form-page';
const email = 'newly-created-user-magic-link-test@test.com';
await membersAgent
.post('/api/send-magic-link')
.header('Referer', customSignupUrl)
.body({
email,
emailType: 'signup',
autoRedirect: true
})
.expectEmptyBody()
.expectStatus(201);
const mail = await mockManager.assert.sentEmail({
to: email,
subject: /Complete your sign up to Ghost!/
});
const [url] = mail.text.match(/https?:\/\/[^\s]+/);
const parsed = new URL(url);
const redirect = parsed.searchParams.get('r');
should(redirect).equal(customSignupUrl);
});
it('Creates a valid magic link from custom signup with redirection disabled', async function () {
const customSignupUrl = 'http://localhost:2368/custom-signup-form-page';
const email = 'newly-created-user-magic-link-test@test.com';
await membersAgent
.post('/api/send-magic-link')
.header('Referer', customSignupUrl)
.body({
email,
emailType: 'signup',
autoRedirect: false
})
.expectEmptyBody()
.expectStatus(201);
const mail = await mockManager.assert.sentEmail({
to: email,
subject: /Complete your sign up to Ghost!/
});
const [url] = mail.text.match(/https?:\/\/[^\s]+/);
const parsed = new URL(url);
const redirect = parsed.searchParams.get('r');
should(redirect).equal(null);
});
it('triggers email alert for free member signup', async function () {
const email = 'newly-created-user-magic-link-test@test.com';
await membersAgent.post('/api/send-magic-link')

View file

@ -389,7 +389,11 @@ module.exports = class RouterController {
}
async sendMagicLink(req, res) {
const {email, emailType} = req.body;
const {email, emailType, autoRedirect} = req.body;
let referer = req.get('referer');
if (autoRedirect === false){
referer = null;
}
if (!email) {
res.writeHead(400);
return res.end('Bad Request.');
@ -400,7 +404,7 @@ module.exports = class RouterController {
const member = await this._memberRepository.get({email});
if (member) {
const tokenData = {};
await this._sendEmailWithMagicLink({email, tokenData, requestedType: emailType, referrer: req.get('referer')});
await this._sendEmailWithMagicLink({email, tokenData, requestedType: emailType, referrer: referer});
}
} else {
const tokenData = _.pick(req.body, ['labels', 'name', 'newsletters']);
@ -410,7 +414,7 @@ module.exports = class RouterController {
// Save attribution data in the tokenData
tokenData.attribution = this._memberAttributionService.getAttribution(req.body.urlHistory);
await this._sendEmailWithMagicLink({email, tokenData, requestedType: emailType, referrer: req.get('referer')});
await this._sendEmailWithMagicLink({email, tokenData, requestedType: emailType, referrer: referer});
}
res.writeHead(201);
return res.end('Created.');