2022-05-18 10:25:14 +00:00
|
|
|
import {UserProfile} from 'app/common/UserAPI';
|
|
|
|
import {GristLoginSystem, GristServer, setUserInSession} from 'app/server/lib/GristServer';
|
|
|
|
import {Request} from 'express';
|
2021-08-17 15:22:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2022-05-18 10:25:14 +00:00
|
|
|
async function getLoginRedirectUrl(req: Request, url: URL) {
|
2022-04-04 21:50:40 +00:00
|
|
|
await setUserInSession(req, gristServer, getDefaultProfile());
|
|
|
|
return url.href;
|
2022-05-18 10:25:14 +00:00
|
|
|
}
|
2021-09-29 15:39:56 +00:00
|
|
|
return {
|
2022-04-04 21:50:40 +00:00
|
|
|
getLoginRedirectUrl,
|
|
|
|
getSignUpRedirectUrl: getLoginRedirectUrl,
|
2021-09-29 15:39:56 +00:00
|
|
|
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();
|
2022-04-11 18:08:44 +00:00
|
|
|
const user = await dbManager.getUserByLoginWithRetry(profile.email, {profile});
|
2021-09-29 15:39:56 +00:00
|
|
|
if (user) {
|
|
|
|
// No need to survey this user!
|
|
|
|
user.isFirstTimeUser = false;
|
|
|
|
await user.save();
|
|
|
|
}
|
2022-05-18 10:25:14 +00:00
|
|
|
return 'no-logins';
|
2021-09-29 15:39:56 +00:00
|
|
|
},
|
|
|
|
};
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultProfile(): UserProfile {
|
|
|
|
return {
|
|
|
|
email: process.env.GRIST_DEFAULT_EMAIL || 'you@example.com',
|
|
|
|
name: 'You',
|
|
|
|
};
|
|
|
|
}
|