token generation

This commit is contained in:
Erik 2022-03-26 14:17:06 +02:00
parent 1b103c41a0
commit 75eab36889
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589

View File

@ -26,7 +26,7 @@ class Users extends EventEmitter {
const id = user.id || user; const id = user.id || user;
this.emit('debug', `User perms query for ${id}`); this.emit('debug', `User perms query for ${id}`);
const userPartial = await this.database.findOne(this.collection, { id }); const userPartial = await this.database.findOne(this.collection, { id });
user = { ...this.defaultPermissions, ...userPartial, ...user }; user = { permissions: this.defaultPermissions, ...userPartial, ...user };
user.tag = `${user.username}#${user.discriminator}`; user.tag = `${user.username}#${user.discriminator}`;
this.emit('debug', `Result for ${id}: ${JSON.stringify(userPartial)}`); this.emit('debug', `Result for ${id}: ${JSON.stringify(userPartial)}`);
if (userPartial) return user; if (userPartial) return user;
@ -38,6 +38,25 @@ class Users extends EventEmitter {
} }
async generateToken(user, permissions=[]) {
const id = Buffer.from(user.id).toString('base64');
const ts = Buffer.from(Date.now().toString()).toString('base64');
const perm = Buffer.from(permissions.join('.')).toString('base64');
const token = `${id}.${ts}.${perm}`;
user.token = token;
await this.database.updateOne(this.collection, { id: user.id }, { token });
return token;
}
checkToken(token) {
return this.database.findOne(this.collection, { token });
}
getAll() { getAll() {
return this.database.find(this.collection, {}); return this.database.find(this.collection, {});