gristlabs_grist-core/app/server/lib/MinimalLogin.ts
Paul Fitzpatrick f7f76fb5e7
A set of tweaks to simplify electron packaging (#421)
* Replace `ormconfig.js` with a newer mechanism of configuring
    TypeORM that can be included in the source code properly.
    The path to `ormconfig.js` has always been awkward to handle,
    and eliminating the file makes building different Grist setups
    a bit simpler.
  * Remove `electron` package. It is barely used, just for some old
    remnants of an older attempt at electron packaging. It was used
    for two types, which I left at `any` for now. More code pruning is
    no doubt possible here, but I'd rather do it when Electron packaging
    has solidified.
  * Add a hook for replacing the login system, and for adding some
    extra middleware the login system may need.
  * Add support for some more possible locations of Python, which
    arise when a standalone version of it is included in the Electron
    package. This isn't very general purpose, just configurations
    that I found useful.
  * Support using grist-core within a yarn workspace - the only tweak
    needed was webpack related.
  * Allow an external ID to be optionally associated with documents.
2023-02-13 15:52:17 -05:00

49 lines
1.5 KiB
TypeScript

import {UserProfile} from 'app/common/UserAPI';
import {GristLoginSystem, GristServer, setUserInSession} from 'app/server/lib/GristServer';
import {Request} from 'express';
/**
* Return a login system that supports a single hard-coded user.
*/
export async function getMinimalLoginSystem(): Promise<GristLoginSystem> {
// Login and logout, redirecting immediately back. Signup is treated as login,
// no nuance here.
return {
async getMiddleware(gristServer: GristServer) {
async function getLoginRedirectUrl(req: Request, url: URL) {
await setUserInSession(req, gristServer, getDefaultProfile());
return url.href;
}
return {
getLoginRedirectUrl,
getSignUpRedirectUrl: getLoginRedirectUrl,
async getLogoutRedirectUrl(req: Request, url: URL) {
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';
},
};
},
async deleteUser() {
// nothing to do
},
};
}
export function getDefaultProfile(): UserProfile {
return {
email: process.env.GRIST_DEFAULT_EMAIL || 'you@example.com',
name: 'You',
};
}