Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions .beads/issues.jsonl

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .beads/last-touched
Original file line number Diff line number Diff line change
@@ -1 +1 @@
perception-8c0
perception-s9t.1
4 changes: 4 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"functions": {
"source": "functions",
"runtime": "nodejs22"
},
"hosting": {
"public": "dashboard/dist",
"ignore": [
Expand Down
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
59 changes: 59 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { logger } = require("firebase-functions");

/**
* Notify Slack when a new user signs up.
*
* Uses the v1 Auth onCreate trigger which fires on every
* Firebase Auth signup event.
*/

const functions = require("firebase-functions");

/** Mask email for logging: "user@example.com" → "us***@example.com" */
function maskEmail(email) {
if (!email) return "no email";
const [local, domain] = email.split("@");
if (!domain) return "***";
const visible = local.slice(0, 2);
return `${visible}***@${domain}`;
}

exports.onNewUser = functions
.runWith({ secrets: ["SLACK_WEBHOOK_URL"] })
.auth.user()
.onCreate(async (user) => {
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
if (!webhookUrl) {
logger.error("SLACK_WEBHOOK_URL secret not configured");
return;
}

const createdAt = user.metadata.creationTime || new Date().toISOString();
const maskedEmail = maskEmail(user.email);
const shortUid = user.uid.slice(0, 8);

const payload = JSON.stringify({
text: [
":new: *New Perception user signup*",
`*Email:* ${maskedEmail}`,
`*UID:* \`${shortUid}...\``,
`*Provider:* ${user.providerData?.[0]?.providerId || "email"}`,
`*Time:* ${createdAt}`,
].join("\n"),
});

const response = await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: payload,
signal: AbortSignal.timeout(10000),
});

if (!response.ok) {
const body = await response.text();
logger.error(`Slack webhook failed: ${response.status} ${body}`);
throw new Error(`Slack webhook failed: ${response.status}`);
}

logger.info(`Slack notified for new user: ${maskedEmail}`);
});
12 changes: 12 additions & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "perception-functions",
"description": "Firebase Cloud Functions for Perception",
"engines": {
"node": "22"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^12.7.0",
"firebase-functions": "^6.3.0"
}
}