improved globals and added status and spectrum
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
			
		||||
import basicAuth from 'basic-auth';
 | 
			
		||||
import bcrypt from 'bcryptjs';
 | 
			
		||||
import UserModel from '../models/user';
 | 
			
		||||
import globals from '../globals';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// appends req.auth(res, ['levels'], method = 'all')
 | 
			
		||||
@@ -64,7 +65,12 @@ function basic (req, next): any {  // checks basic auth and returns changed user
 | 
			
		||||
          bcrypt.compare(auth.pass, data[0].pass, (err, res) => {  // check password
 | 
			
		||||
            if (err) return next(err);
 | 
			
		||||
            if (res === true) {  // password correct
 | 
			
		||||
              resolve({level: data[0].level, name: data[0].name, id: data[0]._id.toString(), location: data[0].location});
 | 
			
		||||
              resolve({
 | 
			
		||||
                level: Object.entries(globals.levels).find(e => e[1] === data[0].level)[0],
 | 
			
		||||
                name: data[0].name,
 | 
			
		||||
                id: data[0]._id.toString(),
 | 
			
		||||
                location: data[0].location
 | 
			
		||||
              });
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
              resolve(null);
 | 
			
		||||
@@ -88,7 +94,12 @@ function key (req, next): any {  // checks API key and returns changed user obje
 | 
			
		||||
      UserModel.find({key: req.query.key}).lean().exec( (err, data: any) => {  // find user
 | 
			
		||||
        if (err) return next(err);
 | 
			
		||||
        if (data.length === 1) {  // one user found
 | 
			
		||||
          resolve({level: data[0].level, name: data[0].name, id: data[0]._id.toString(), location: data[0].location});
 | 
			
		||||
          resolve({
 | 
			
		||||
            level: Object.entries(globals.levels).find(e => e[1] === data[0].level)[0],
 | 
			
		||||
            name: data[0].name,
 | 
			
		||||
            id: data[0]._id.toString(),
 | 
			
		||||
            location: data[0].location
 | 
			
		||||
          });
 | 
			
		||||
          if (!/^\/api/m.test(req.url)){
 | 
			
		||||
            delete req.query.key;  // delete query parameter to avoid interference with later validation
 | 
			
		||||
          }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,12 @@
 | 
			
		||||
import globals from '../globals';
 | 
			
		||||
 | 
			
		||||
export default function flatten (data, keepArray = false) {  // flatten object: {a: {b: true}} -> {a.b: true}
 | 
			
		||||
  const result = {};
 | 
			
		||||
  function recurse (cur, prop) {
 | 
			
		||||
    if (Object(cur) !== cur || Object.keys(cur).length === 0) {
 | 
			
		||||
      result[prop] = cur;
 | 
			
		||||
    }
 | 
			
		||||
    else if (prop === 'spectrum.dpt') {
 | 
			
		||||
      console.log('dpt');
 | 
			
		||||
    else if (prop === `${globals.spectrum.spectrum}.${globals.spectrum.dpt}`) {
 | 
			
		||||
      result[prop + '.labels'] = cur.map(e => e[0]);
 | 
			
		||||
      result[prop + '.values'] = cur.map(e => e[1]);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user