updated files to ES6

This commit is contained in:
2023-08-17 12:28:32 -04:00
parent 80237f76e6
commit 41325f7a55
8 changed files with 287 additions and 300 deletions
+71 -72
View File
@@ -1,14 +1,13 @@
'use strict';
const objValidate = require('../utils/object_validate');
const Table = require('../utils/redis_model');
const {Token, InviteToken} = require('./token');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const User = require('../utils/redis_model')({
_name: 'user',
_key: 'username',
_keyMap: {
class User extends Table{
static _key = 'username';
static _keyMap = {
'created_by': {isRequired: true, type: 'string', min: 3, max: 500},
'created_on': {default: function(){return (new Date).getTime()}},
'updated_by': {default:"__NONE__", isRequired: false, type: 'string',},
@@ -17,91 +16,91 @@ const User = require('../utils/redis_model')({
'password': {isRequired: true, type: 'string', min: 3, max: 500},
'backing': {default:"redis", isRequired: false, type: 'string',},
}
});
User.backing = "redis";
static backing = 'redis'
static async add(data) {
try{
data['password'] = await bcrypt.hash(data['password'], saltRounds);
data['backing'] = data['backing'] || 'redis';
User.add = async function(data) {
try{
data['password'] = await bcrypt.hash(data['password'], saltRounds);
data['backing'] = data['backing'] || 'redis';
return await super.add(data)
console.log('set password', data)
return this.__proto__.add(data);
}catch(error){
throw error;
}catch(error){
throw error;
}
}
};
User.addByInvite = async function(data){
try{
let token = await InviteToken.get(data.token);
static async addByInvite(data){
try{
let token = await InviteToken.get(data.token);
if(!token.is_valid){
let error = new Error('Token Invalid');
error.name = 'Token Invalid';
error.message = `Token is not valid or as allready been used. ${data.token}`;
error.status = 401;
if(!token.is_valid){
let error = new Error('Token Invalid');
error.name = 'Token Invalid';
error.message = `Token is not valid or as allready been used. ${data.token}`;
error.status = 401;
throw error;
}
let user = await this.add(data);
if(user){
await token.consume({claimed_by: user.username});
return user;
}
}catch(error){
throw error;
}
let user = await this.add(data);
};
if(user){
await token.consume({claimed_by: user.username});
return user;
async setPassword(data){
try{
data['password'] = await bcrypt.hash(data['password'], saltRounds);
return this.update(data);
}catch(error){
throw error;
}
}catch(error){
throw error;
}
};
async invite(){
try{
let token = await InviteToken.add({created_by: this.username});
return token;
User.setPassword = async function(data){
try{
data['password'] = await bcrypt.hash(data['password'], saltRounds);
return this.__proto__.update(data);
}catch(error){
throw error;
}
};
User.invite = async function(){
try{
let token = await InviteToken.add({created_by: this.username});
return token;
}catch(error){
throw error;
}
};
User.login = async function(data){
try{
let user = await User.get(data);
let auth = await bcrypt.compare(data.password, user.password);
if(auth){
return user
}else{
throw this.errors.login();
}catch(error){
throw error;
}
}catch(error){
if (error == 'Authentication failure'){
throw this.errors.login()
}
throw error;
}
};
static async login(data){
try{
let user = await User.get(data);
let auth = await bcrypt.compare(data.password, user.password);
if(auth){
return user
}else{
throw this.errors.login();
}
}catch(error){
if (error == 'Authentication failure'){
throw this.errors.login()
}
throw error;
}
};
}
module.exports = {User};
module.exports = {User};