first commit
This commit is contained in:
113
pb_hooks/main.pb.js
Normal file
113
pb_hooks/main.pb.js
Normal 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)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user