Skip to content

Commit

Permalink
INFRA15 - Add the first Organizer account using the seeding script
Browse files Browse the repository at this point in the history
  • Loading branch information
Camillo Bucciarelli authored and camillobucciarelli committed Apr 22, 2024
1 parent 3376156 commit 590efa3
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions scripts/create-account-organizer.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require("dotenv").config({ path: "../.env" });

const app = require("firebase-admin").initializeApp({
credential: require("firebase-admin").credential.cert(
JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT || "{}"),
),
});

const rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout,
});

rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted) rl.output.write("*");
else rl.output.write(stringToWrite);
};

const setCustomClaim = (claim, uid) => {
return app.auth().setCustomUserClaims(uid, { [claim]: true });
};

const createOrganizerAccount = async () => {
console.log(
"\x1b[33m",
`
██╗ ██╗███████╗██████╗ ██╗ ██╗██╗ ██████╗ ███████╗███████╗███████╗██████╗ ███████╗
██║ ██║██╔════╝██╔══██╗██║ ██║██║██╔════╝ ██╔════╝██╔════╝██╔════╝██╔══██╗██╔════╝
███████║█████╗ ██║ ██║██║ █╗ ██║██║██║ ███╗ ███████╗█████╗ █████╗ ██║ ██║███████╗
██╔══██║██╔══╝ ██║ ██║██║███╗██║██║██║ ██║ ╚════██║██╔══╝ ██╔══╝ ██║ ██║╚════██║
██║ ██║███████╗██████╔╝╚███╔███╔╝██║╚██████╔╝ ███████║███████╗███████╗██████╔╝███████║
╚═╝ ╚═╝╚══════╝╚═════╝ ╚══╝╚══╝ ╚═╝ ╚═════╝ ╚══════╝╚══════╝╚══════╝╚═════╝ ╚══════╝
CREATE ORGANIZER ACCOUNT
`,
"\x1b[0m",
);
const email = await new Promise((resolve) => {
rl.question("Enter email: ", resolve);
});
const displayName = await new Promise((resolve) => {
rl.question("Enter display name: ", resolve);
});
const password = await new Promise((resolve) => {
rl.question("Enter password: ", resolve);
rl.stdoutMuted = true;
});

try {
const user = await app.auth().createUser({
email,
password,
displayName,
});

await setCustomClaim("organizer", user.uid);

const verifyEmailLink = await app
.auth()
.generateEmailVerificationLink(email);

console.log(`
\x1b[32m
🎉 Organizer account configured successfully! 🎉
\x1b[0m
You can verify your email by clicking on the link below:\x1b[0m
\x1b[36m${verifyEmailLink}
`);
} catch (error) {
console.error(
"\x1b[31m",
`
🚫 Error while configuring account: ${error.message} 🚫
`,
"\x1b[0m",
);
} finally {
rl.close();
}
};

createOrganizerAccount();

0 comments on commit 590efa3

Please sign in to comment.