2021-08-17 15:22:30 +00:00
|
|
|
import { UserProfile } from 'app/common/UserAPI';
|
2021-09-29 15:39:56 +00:00
|
|
|
import { GristLoginSystem, GristServer } from 'app/server/lib/GristServer';
|
(core) make Grist easier to run with a single server
Summary:
This makes many small changes so that Grist is less fussy to run as a single instance behind a reverse proxy. Some users had difficulty with the self-connections Grist would make, due to internal network setup, and since these are unnecessary in any case in this scenario, they are now optimized away. Likewise some users had difficulties related to doc worker urls, which are now also optimized away. With these changes, users should be able to get a lot further on first try, at least far enough to open and edit documents.
The `GRIST_SINGLE_ORG` setting was proving a bit confusing, since it appeared to only work when set to `docs`. This diff
adds a check for whether the specified org exists, and if not, it creates it. This still depends on having a user email to make as the owner of the team, so there could be remaining difficulties there.
Test Plan: tested manually with nginx
Reviewers: jarek
Reviewed By: jarek
Differential Revision: https://phab.getgrist.com/D3299
2022-03-02 19:07:26 +00:00
|
|
|
import { fromCallback } from 'app/server/lib/serverUtils';
|
2021-08-17 15:22:30 +00:00
|
|
|
import { Request } from 'express';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a login system that supports a single hard-coded user.
|
|
|
|
*/
|
2021-09-29 15:39:56 +00:00
|
|
|
export async function getMinimalLoginSystem(): Promise<GristLoginSystem> {
|
2021-08-17 15:22:30 +00:00
|
|
|
// Login and logout, redirecting immediately back. Signup is treated as login,
|
|
|
|
// no nuance here.
|
|
|
|
return {
|
2021-09-29 15:39:56 +00:00
|
|
|
async getMiddleware(gristServer: GristServer) {
|
|
|
|
return {
|
|
|
|
async getLoginRedirectUrl(req: Request, url: URL) {
|
|
|
|
await setSingleUser(req, gristServer);
|
|
|
|
return url.href;
|
|
|
|
},
|
|
|
|
async getLogoutRedirectUrl(req: Request, url: URL) {
|
|
|
|
return url.href;
|
|
|
|
},
|
|
|
|
async getSignUpRedirectUrl(req: Request, url: URL) {
|
|
|
|
await setSingleUser(req, gristServer);
|
|
|
|
return url.href;
|
|
|
|
},
|
|
|
|
async addEndpoints() {
|
|
|
|
// If working without a login system, make sure default user exists.
|
|
|
|
const dbManager = gristServer.getHomeDBManager();
|
|
|
|
const profile = getDefaultProfile();
|
|
|
|
const user = await dbManager.getUserByLoginWithRetry(profile.email, profile);
|
|
|
|
if (user) {
|
|
|
|
// No need to survey this user!
|
|
|
|
user.isFirstTimeUser = false;
|
|
|
|
await user.save();
|
|
|
|
}
|
|
|
|
return "no-logins";
|
|
|
|
},
|
|
|
|
};
|
2021-08-17 15:22:30 +00:00
|
|
|
},
|
2021-09-29 15:39:56 +00:00
|
|
|
async deleteUser() {
|
|
|
|
// nothing to do
|
2021-08-17 15:22:30 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the user in the current session to the single hard-coded user.
|
|
|
|
*/
|
|
|
|
async function setSingleUser(req: Request, gristServer: GristServer) {
|
|
|
|
const scopedSession = gristServer.getSessions().getOrCreateSessionFromRequest(req);
|
(core) make Grist easier to run with a single server
Summary:
This makes many small changes so that Grist is less fussy to run as a single instance behind a reverse proxy. Some users had difficulty with the self-connections Grist would make, due to internal network setup, and since these are unnecessary in any case in this scenario, they are now optimized away. Likewise some users had difficulties related to doc worker urls, which are now also optimized away. With these changes, users should be able to get a lot further on first try, at least far enough to open and edit documents.
The `GRIST_SINGLE_ORG` setting was proving a bit confusing, since it appeared to only work when set to `docs`. This diff
adds a check for whether the specified org exists, and if not, it creates it. This still depends on having a user email to make as the owner of the team, so there could be remaining difficulties there.
Test Plan: tested manually with nginx
Reviewers: jarek
Reviewed By: jarek
Differential Revision: https://phab.getgrist.com/D3299
2022-03-02 19:07:26 +00:00
|
|
|
// Make sure session is up to date before operating on it.
|
|
|
|
// Behavior on a completely fresh session is a little awkward currently.
|
|
|
|
const reqSession = (req as any).session;
|
|
|
|
if (reqSession?.save) {
|
|
|
|
await fromCallback(cb => reqSession.save(cb));
|
|
|
|
}
|
2021-10-01 14:24:23 +00:00
|
|
|
await scopedSession.operateOnScopedSession(req, async (user) => Object.assign(user, {
|
2021-08-17 15:22:30 +00:00
|
|
|
profile: getDefaultProfile()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultProfile(): UserProfile {
|
|
|
|
return {
|
|
|
|
email: process.env.GRIST_DEFAULT_EMAIL || 'you@example.com',
|
|
|
|
name: 'You',
|
|
|
|
};
|
|
|
|
}
|