Saving token used and time joined

This commit is contained in:
metacryst
2025-11-24 00:56:15 -06:00
parent 40e7987ca2
commit 6299e80268
7 changed files with 81 additions and 62 deletions

View File

@@ -13,34 +13,61 @@ export default class Members extends OrderedObject {
schema = z.object({
email: z.string().email(),
firstName: z.string(),
lastName: z.number().int().min(0),
lastName: z.string(),
password: z.string(),
address: this.addressSchema
tokenUsed: z.string().regex(
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
"Invalid UUID"
),
joined: z.string(),
address: this.addressSchema,
})
isHashed = (s) => {return s.startsWith("$argon2")}
async add(newMember) {
console.log("adding ", newMember)
let id = `MEMBER-${newMember.email}`
if(this.schema.safeParse(newMember)) {
if(!this.isHashed(newMember.password)) {
const hash = await argon2.hash(newMember.password);
newMember.password = hash
}
save(member) {
let id = `MEMBER-${member.email}`
let result = this.schema.safeParse(member)
if(result.success) {
try {
super.add(id, newMember)
super.add(id, member)
} catch(e) {
console.error(e)
throw e
}
} else {
throw new global.ServerError(400, "Invalid Member Data!");
console.error("Failed parsing member: ", result.error)
throw new global.ServerError(400, "Invalid Member Data!: ");
}
global.db.saveData()
}
async add(newMember, tokenID) {
newMember.tokenUsed = tokenID
const hash = await argon2.hash(newMember.password);
newMember.password = hash
newMember.joined = this.currentTime()
this.save(newMember)
}
getByEmail(email) {
return super.get(`MEMBER-${email}`)
}
currentTime() {
const now = new Date();
const month = String(now.getMonth() + 1).padStart(2, "0");
const day = String(now.getDate()).padStart(2, "0");
const year = now.getFullYear();
let hours = now.getHours();
const ampm = hours >= 12 ? "pm" : "am";
hours = hours % 12 || 12; // convert to 12-hour format
const minutes = String(now.getMinutes()).padStart(2, "0");
const seconds = String(now.getSeconds()).padStart(2, "0");
const ms = String(now.getMilliseconds()).padStart(4, "0"); // 4-digit like "5838"
return `${month}.${day}.${year}-${hours}:${minutes}:${seconds}${ms}${ampm}`;
}
}