import crypto from 'crypto';

export default class {

  constructor(auth) {
    this.auth = auth;
  }

  /*
  * First checks if user exists, if so -> login
  * If user doesn't exist -> try to create user
  */
  process(httpRequest, httpResponse) {
    let userInfo = {
      username: httpRequest.body.name,
      password: httpRequest.body.password,
      email: httpRequest.body.email,
      type: httpRequest.body.type
    };
    let userLoggedIn = false;
    let request;

    return new Promise((resolve, reject) => {
      request = this.loginUser(userInfo);

      request.then((result) => {
        if (result === true) {
          userLoggedIn = true;
          let token = this.generateToken(userInfo);
          httpResponse.status(201);
          httpResponse.send({
            token: token
          });
          resolve(userLoggedIn);
        }
        else {
          request = this.createUser(userInfo);
          request
            .then((result) => {
            if (result === true) {
              userLoggedIn = true;
              let token = this.generateToken(userInfo);
              httpResponse.status(201);
              httpResponse.send({
                token: token
              });
              resolve(userLoggedIn);
            } else {
              reject('Cannot create user');
            }
          });
        }
      });
    }).catch((error) => {
      httpResponse.send("401, " + error);
    });
  }

  createUser(userInfo) {
    return this.auth.userAdd(userInfo.username, userInfo.password, userInfo.email);
  }

  loginUser(userInfo) {
    return this.auth.userLogin(userInfo.username, userInfo.password, userInfo.email);
  }

  generateToken(userInfo) {
    let token = crypto.randomBytes(64).toString('hex');
    this.auth.addTokenToDB(userInfo.username, token);
    return token;
  }

}
@@ -659,36 +659,24 @@
 e;%0A         
- let token =
  this.genera
@@ -684,34 +684,53 @@
 eToken(userInfo)
-;%0A
+.then((token) =%3E %7B%0A  
           httpRe
@@ -741,32 +741,34 @@
 se.status(201);%0A
+  
           httpRe
@@ -785,32 +785,34 @@
 d(%7B%0A            
+  
 token: token%0A   
@@ -800,32 +800,34 @@
    token: token%0A
+  
           %7D);%0A  
@@ -826,32 +826,34 @@
   %7D);%0A          
+  
 resolve(userLogg
@@ -851,32 +851,46 @@
 (userLoggedIn);%0A
+          %7D);%0A
         %7D%0A      
@@ -1078,28 +1078,16 @@
         
- let token =
  this.ge
@@ -1099,33 +1099,50 @@
 eToken(userInfo)
-;
+.then((token) =%3E %7B
 %0A              h
@@ -1132,32 +1132,34 @@
 %7B%0A              
+  
 httpResponse.sta
@@ -1160,32 +1160,34 @@
 se.status(201);%0A
+  
               ht
@@ -1212,32 +1212,34 @@
                 
+  
 token: token%0A   
@@ -1241,36 +1241,40 @@
 n%0A              
+ 
+ 
 %7D);%0A
+  
               re
@@ -1286,32 +1286,50 @@
 (userLoggedIn);%0A
+              %7D);%0A
             %7D el
@@ -1759,24 +1759,64 @@
 userInfo) %7B%0A
+    return new Promise((resolve) =%3E %7B%0A  
     let toke
@@ -1859,24 +1859,26 @@
 'hex');%0A    
+  
 this.auth.ad
@@ -1923,20 +1923,32 @@
     
-return token
+  resolve(token);%0A    %7D)
 ;%0A  

Make auth-user-login route asynchronous