(core) when redirecting, use protocol in APP_HOME_URL if available

Summary:
Currently, Grist behind a reverse proxy will generate many
needless redirects via `http`, and can't be used with only
port 443. This diff centralizes generation of these redirects
and uses the protocol in APP_HOME_URL if it is set.

Test Plan:
manually tested by rebuilding grist-core and
doing a reverse proxy deployment that had no support for
port 80. Prior to this change, there are lots of problems;
after, the site works as expected.

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3400
This commit is contained in:
Paul Fitzpatrick
2022-04-27 12:07:08 -04:00
parent 6f00106d7c
commit 4de5928396
6 changed files with 31 additions and 14 deletions

View File

@@ -21,6 +21,7 @@
import type {Express, NextFunction, Request, RequestHandler, Response} from 'express';
import type {RequestWithLogin} from 'app/server/lib/Authorizer';
import {expressWrap} from 'app/server/lib/expressWrap';
import {getOriginUrl} from 'app/server/lib/requestUtils';
import * as crypto from 'crypto';
const DISCOURSE_CONNECT_SECRET = process.env.DISCOURSE_CONNECT_SECRET;
@@ -65,8 +66,8 @@ function discourseConnect(req: Request, resp: Response) {
throw new Error('User is not authenticated');
}
if (!req.query.user && mreq.users && mreq.users.length > 1) {
const origUrl = new URL(req.originalUrl, `${req.protocol}://${req.get('host')}`);
const redirectUrl = new URL('/welcome/select-account', `${req.protocol}://${req.get('host')}`);
const origUrl = new URL(req.originalUrl, getOriginUrl(req));
const redirectUrl = new URL('/welcome/select-account', getOriginUrl(req));
redirectUrl.searchParams.set('next', origUrl.toString());
return resp.redirect(redirectUrl.toString());
}