Files
Krow-workspace/backend/unified-api/scripts/ensure-v2-demo-users.mjs

129 lines
3.5 KiB
JavaScript

import { applicationDefault, getApps, initializeApp } from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';
import { V2DemoFixture as fixture } from '../../command-api/scripts/v2-demo-fixture.mjs';
const ownerUid = fixture.users.businessOwner.id;
const ownerEmail = fixture.users.businessOwner.email;
const staffUid = fixture.users.staffAna.id;
const staffEmail = fixture.users.staffAna.email;
const staffPhone = process.env.V2_DEMO_STAFF_PHONE || fixture.staff.ana.phone;
const staffBenUid = fixture.users.staffBen.id;
const staffBenEmail = fixture.users.staffBen.email;
const staffBenPhone = process.env.V2_DEMO_STAFF_BEN_PHONE || fixture.staff.ben.phone;
const ownerPassword = process.env.V2_DEMO_OWNER_PASSWORD || 'Demo2026!';
const staffPassword = process.env.V2_DEMO_STAFF_PASSWORD || 'Demo2026!';
const staffBenPassword = process.env.V2_DEMO_STAFF_BEN_PASSWORD || 'Demo2026!';
function ensureAdminApp() {
if (getApps().length === 0) {
initializeApp({ credential: applicationDefault() });
}
}
function getAdminAuth() {
ensureAdminApp();
return getAuth();
}
async function getUserByPhoneNumber(phoneNumber) {
if (!phoneNumber) return null;
try {
return await getAdminAuth().getUserByPhoneNumber(phoneNumber);
} catch (error) {
if (error?.code === 'auth/user-not-found') return null;
throw error;
}
}
async function getUserByEmail(email) {
try {
return await getAdminAuth().getUserByEmail(email);
} catch (error) {
if (error?.code === 'auth/user-not-found') return null;
throw error;
}
}
async function ensureManagedUser({ uid, email, password, displayName, phoneNumber }) {
const auth = getAdminAuth();
const existingByEmail = await getUserByEmail(email);
if (existingByEmail && existingByEmail.uid !== uid) {
await auth.deleteUser(existingByEmail.uid);
}
const existingByPhone = await getUserByPhoneNumber(phoneNumber);
if (existingByPhone && existingByPhone.uid !== uid) {
await auth.deleteUser(existingByPhone.uid);
}
try {
await auth.updateUser(uid, {
email,
password,
displayName,
...(phoneNumber ? { phoneNumber } : {}),
emailVerified: true,
disabled: false,
});
} catch (error) {
if (error?.code !== 'auth/user-not-found') {
throw error;
}
await auth.createUser({
uid,
email,
password,
displayName,
...(phoneNumber ? { phoneNumber } : {}),
emailVerified: true,
disabled: false,
});
}
const user = await auth.getUser(uid);
return {
uid: user.uid,
email: user.email,
phoneNumber: user.phoneNumber,
displayName: user.displayName,
created: true,
};
}
async function main() {
const owner = await ensureManagedUser({
uid: ownerUid,
email: ownerEmail,
password: ownerPassword,
displayName: fixture.users.businessOwner.displayName,
});
const staff = await ensureManagedUser({
uid: staffUid,
email: staffEmail,
password: staffPassword,
displayName: fixture.users.staffAna.displayName,
phoneNumber: staffPhone,
});
const staffBen = await ensureManagedUser({
uid: staffBenUid,
email: staffBenEmail,
password: staffBenPassword,
displayName: fixture.users.staffBen.displayName,
phoneNumber: staffBenPhone,
});
// eslint-disable-next-line no-console
console.log(JSON.stringify({
owner,
staff,
staffBen,
}, null, 2));
}
main().catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
});