first commit

This commit is contained in:
Kevand
2026-02-04 00:21:37 +00:00
commit 58925b1ac0
18 changed files with 1086 additions and 0 deletions

113
pb_hooks/main.pb.js Normal file
View File

@@ -0,0 +1,113 @@
/// <reference path="../pb_data/types.d.ts" />
onBootstrap((e) => {
e.next();
console.log("App initialized!")
})
onRecordCreate((e) => {
if (!e.record.get("token")) {
e.record.set('token', $security.randomString(32));
}
e.next();
}, 'invites')
onRecordCreate((e) => {
const token = e.record.get('invite_token');
const email = e.record.email();
if (!token) {
throw new BadRequestError('Registration requires a valid invite token');
}
try {
const invite = $app.findFirstRecordByData('invites', 'token', token);
if (invite.email() !== email) {
throw new BadRequestError('This invite is for a different email');
}
const createdUnix = invite.getDateTime('created').unix();
const nowUnix = new Date().getTime() / 1000;
const oneDaySeconds = 86400;
if ((nowUnix - createdUnix) > oneDaySeconds) {
throw new BadRequestError('This invite has expired');
}
} catch (err) {
if (err instanceof BadRequestError) {
throw err;
}
throw new BadRequestError('Invalid invite token');
}
e.next();
}, 'users')
onRecordAfterCreateSuccess((e) => {
const token = e.record.get('invite_token');
if (token) {
try {
const invite = $app.findFirstRecordByData('invites', 'token', token)
$app.delete(invite);
const user = e.record;
user.set('invite_token', '');
user.set('verified', true)
$app.save(user);
} catch (err) {
console.warn('Could not delete used invite:', err)
}
}
e.next();
}, 'users')
onRecordAfterCreateSuccess((e) => {
const email = e.record.email();
const token = e.record.get('token');
const appUrl = e.app.settings().meta.appURL;
const inviteLink = `${appUrl}/accept-invite?token=${token}`
const message = new MailerMessage({
from: {
address: e.app.settings().meta.senderAddress,
name: e.app.settings().meta.senderName
},
to: [{ address: email }],
subject: "Zapraszam przyjacielu",
html: `
<h1>Zapraszam serdecznie</h1>
<p>Naciśnij w linka by założyć konto (link wygaśnie za 24 godziny)</p>
<p><a href="${inviteLink}">${inviteLink}</a></p>
`
})
try {
e.app.newMailClient().send(message)
} catch (e) {
console.error('Failed to send invite email:', e);
}
e.next();
}, 'invites');
cronAdd("cleanup_invites", "0 0 * * *", () => {
console.log("Cleaning up invites...");
try {
$app.db().newQuery("DELETE FROM invites WHERE created < datetime('now', '-1 day')").execute()
} catch (err) {
console.error("Clean up failed:", err)
}
})

13
pb_hooks/util.js Normal file
View File

@@ -0,0 +1,13 @@
module.exports = {
uuidv4() {
const uuid = new Array(36);
for (let i = 0; i < 36; i++) {
uuid[i] = Math.floor(Math.random() * 16)
}
uuid[14] = 4;
uuid[19] = uuid[19] &= ~(1 << 2);
uuid[19] = uuid[19] |= (1 << 3);
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
return uuid.map(x => x.toString(16)).join('')
}
}