module.exports = function(db) {
    const AUTH_KEY_LENGTH = 60,
        AUTH_KEY_CHARS = 'qwertyuiopasdfghjklzxcvbnmWERTYUIOPASDFGHJKLZXCVBNM';

    function validate(user) {

    }

    function generateAuthKey(uniquePart) {
        var authKey = uniquePart,
            index;
        while (authKey.length < AUTH_KEY_LENGTH) {
            index = Math.floor(Math.random() * AUTH_KEY_CHARS.length);
            authKey += AUTH_KEY_CHARS[index];
        }
        return authKey;
    }

    function get(req, res) {
        var user = req.user;
        if (!user) {
            res.status(401)
                .json('Unauthorized user!');
            return;
        }
        var users = db('users')
            .map(function(user) {
                return {
                    username: user.username,
                    id: user.id
                };
            });

        res.json({
            result: users
        });
    }

    function post(req, res) {

        const user = req.body;
        if (!user || typeof user.username !== 'string' || typeof user.passHash !== 'string') {
            res.status(400)
                .json('Invalid user');
            return;
        }
        const error = validate(user);

        if (error) {
            res.status(400)
                .json(error.message);
            return;
        }

        const dbUserCursor = db.collection('users').findOne({
            usernameToLower: user.username.toLowerCase()
        }, function(e, dbUser) {
            if (dbUser) {
                res.status(400)
                    .json('Duplicated user');
                return;
            }
            user.usernameToLower = user.username.toLowerCase();
            db.collection('users').insert(user);
            res.status(201)
                .json({
                    result: {
                        username: user.username
                    }
                });
        });

    }

    function put(req, res) {

        const reqUser = req.body;

        const dbUserCursor = db.collection('users').findOne({
            usernameToLower: reqUser.username.toLowerCase()
        }, function(e, user) {
            console.log(reqUser);
            console.log(user);

            if (!user || user.passHash !== reqUser.passHash) {
                res.status(404)
                    .json('Invalid username or password');
                return;
            }
            //  if (!user.authKey) {
            //      user.authKey = generateAuthKey(user.id);
            //      // TO DO update    db.save();
            //  }

            res.json({
                result: {
                    username: user.username,
                    authKey: user.authKey
                }
            });
        });
    }

    return {
        get: get,
        post: post,
        put: put
    };
};

@@ -522,152 +522,75 @@
     
-var user = req.user;%0A        if (!user) %7B%0A            res.status(401)%0A                .json('Unauthorized user!');%0A            return;%0A        %7D
+db.collection('users').find().toArray(function(e, userCollection) %7B
 %0A   
@@ -594,19 +594,23 @@
         
-var
+    let
  users =
@@ -614,32 +614,22 @@
 s = 
-db('users')%0A            
+userCollection
 .map
@@ -784,32 +784,36 @@
    %7D);%0A%0A        
+    
 res.json(%7B%0A     
@@ -799,32 +799,36 @@
      res.json(%7B%0A
+    
             resu
@@ -837,35 +837,52 @@
 : users%0A        
-%7D);
+    %7D);%0A        %7D);%0A
 %0A    %7D%0A%0A    func

fix get all users