switch to using couch_peruser - there were issues with dbs being created coming back to life after deletion ... weird stuff

This commit is contained in:
lingdocs 2021-08-23 07:39:18 +04:00
parent 328177c9ff
commit b673fa6e93
2 changed files with 38 additions and 39 deletions

View File

@ -111,8 +111,9 @@ export async function updateLingdocsUser(uuid: T.UUID, toUpdate:
});
}
export async function addCouchDbAuthUser(uuid: T.UUID): Promise<{ password: T.UserDbPassword }> {
export async function addCouchDbAuthUser(uuid: T.UUID): Promise<{ password: T.UserDbPassword, userDbName: T.WordlistDbName }> {
const password = generateWordlistDbPassword();
const userDbName = getWordlistDbName(uuid);
const usersDb = nano.db.use("_users");
const authUser: T.CouchDbAuthUser = {
_id: `org.couchdb.user:${uuid}`,
@ -122,40 +123,40 @@ export async function addCouchDbAuthUser(uuid: T.UUID): Promise<{ password: T.Us
password,
};
await usersDb.insert(authUser);
return { password };
return { password, userDbName };
}
export async function createWordlistDatabase(uuid: T.UUID, password: T.UserDbPassword): Promise<{ name: T.WordlistDbName, password: T.UserDbPassword }> {
// Instead of these functions, I'm using couch_peruser
// export async function createWordlistDatabase(uuid: T.UUID, password: T.UserDbPassword): Promise<{ name: T.WordlistDbName, password: T.UserDbPassword }> {
// const name = getWordlistDbName(uuid);
// // create wordlist database for user
// await nano.db.create(name);
// const securityInfo = {
// admins: {
// names: [uuid],
// roles: ["_admin"]
// },
// members: {
// names: [uuid],
// roles: ["_admin"],
// },
// };
// const userDb = nano.db.use(name);
// await userDb.insert(securityInfo as any, "_security");
// return { password, name };
// }
const name = getWordlistDbName(uuid);
// create wordlist database for user
await nano.db.create(name);
const securityInfo = {
admins: {
names: [uuid],
roles: ["_admin"]
},
members: {
names: [uuid],
roles: ["_admin"],
},
};
const userDb = nano.db.use(name);
await userDb.insert(securityInfo as any, "_security");
return { password, name };
}
export async function deleteWordlistDatabase(uuid: T.UUID): Promise<void> {
const name = getWordlistDbName(uuid);
try {
await nano.db.destroy(name);
} catch (e) {
// allow the error to pass if we're just trying to delete a database that never existed
if (e.message !== "Database does not exist.") {
throw new Error("error deleting database");
}
}
}
// export async function deleteWordlistDatabase(uuid: T.UUID): Promise<void> {
// const name = getWordlistDbName(uuid);
// try {
// await nano.db.destroy(name);
// } catch (e) {
// // allow the error to pass if we're just trying to delete a database that never existed
// if (e.message !== "Database does not exist.") {
// throw new Error("error deleting database");
// }
// }
// }
function generateWordlistDbPassword(): T.UserDbPassword {
function makeChunk(): string {

View File

@ -3,8 +3,6 @@ import {
deleteLingdocsUser,
getLingdocsUser,
updateLingdocsUser,
createWordlistDatabase,
deleteWordlistDatabase,
addCouchDbAuthUser,
deleteCouchDbAuthUser,
} from "../lib/couch-db";
@ -121,11 +119,11 @@ apiRouter.put("/user/upgrade", async (req, res, next) => {
return;
}
// add user to couchdb authentication db
const { password } = await addCouchDbAuthUser(userId);
// create user db
const { name } = await createWordlistDatabase(userId, password);
const { password, userDbName } = await addCouchDbAuthUser(userId);
// // create user db
// const { name } = await createWordlistDatabase(userId, password);
// update LingdocsUser
const u = await updateLingdocsUser(userId, { level: "student", wordlistDbName: name, userDbPassword: password });
const u = await updateLingdocsUser(userId, { level: "student", wordlistDbName: userDbName, userDbPassword: password });
const upgraded: T.UpgradeUserResponse = {
ok: true,
message: "user upgraded to student",
@ -143,7 +141,7 @@ apiRouter.put("/user/upgrade", async (req, res, next) => {
apiRouter.delete("/user", async (req, res, next) => {
try {
if (!req.user) throw new Error("user not found");
await deleteWordlistDatabase(req.user.userId);
// await deleteWordlistDatabase(req.user.userId);
await deleteCouchDbAuthUser(req.user.userId);
await deleteLingdocsUser(req.user.userId);
sendResponse(res, { ok: true, message: "user deleted" });