 
 
 module.exports.remove = (req, res, next) ->
-    # Remove device :
-    #       * remove device access
-    #       * remove device document
-    removeDevice = (device, cb) =>
-        id = device.id
-        # Remove Access
-        clientDS.del "access/#{id}/", (err, result, body) ->
-            if err?
-                error = new Error err
-                error.status = 400
-                cd error
-            else
-                # Remove Device
-                clientDS.del "data/#{id}/", (err, result, body) ->
-                    if err?
-                        error = new Error err
-                        error.status = 400
-                        cd error
-                    else
-                        cb null
 
     authenticator = passport.authenticate 'local', (err, user) ->
         if err

            error.status = 401
            next error
        else
            # Check if name is correctly declared
            device = req.body
            if not device?.login?
                error = new Error "Name isn't defined in req.body.login"
                error.status = 400
                next error
            else
                # Create device
                device.docType = "Device"

                # Check if an other device hasn't the same name
                deviceExists device.login, (err, exist) ->
                    if err
                        next err
                    else if exist
                        error = new Error "This name is already used"
                        error.status = 400
                        next error
                    else
                        # Create device
                        createDevice device, (err, data) ->
                            if err?
                                next err
                            else
                                res.send 201, data


    initAuth req, (user) ->

    req.headers['authorization'] = undefined
    cb user



# Create device :
#       * create device document
#       * create device access
createDevice = (device, cb) =>
    device.docType = "Device"
    # Create device document
    clientDS.post "data/", device, (err, result, docInfo) ->
        return cb(err) if err?
        # Create access for this device
        access =
            login: device.login
            password: randomString 32
            app: docInfo._id
            permissions: device.permissions or defaultPermissions
        clientDS.post 'access/', access, (err, result, body) ->
            return cb(err) if err?
            data =
                password: access.password
                login: device.login
                permissions: access.permissions
            # Return access to device
            cb null, data


# Update device :
#       * update device access
updateDevice = (oldDevice, device, cb) =>
    path = "request/access/byApp/"
    clientDS.post path, key: oldDevice.id, (err, result, accesses) ->
        # Update access for this device
        access =
            login: device.login
            password: randomString 32
            app: oldDevice.id
            permissions: device.permissions or defaultPermissions
        path = "access/#{accesses[0].id}/"
        clientDS.put path, access, (err, result, body) ->
            if err?
                console.log err
                error = new Error err
                cb error
            else
                data =
                    password: access.password
                    login: device.login
                    permissions: access.permissions
                # Return access to device
                cb null, data


# Remove device :
#       * remove device access
#       * remove device document
removeDevice = (device, cb) =>
    id = device.id
    # Remove Access
    clientDS.del "access/#{id}/", (err, result, body) ->
        if err?
            error = new Error err
            error.status = 400
            cd error
        else
            # Remove Device
            clientDS.del "data/#{id}/", (err, result, body) ->
                if err?
                    error = new Error err
                    error.status = 400
                    cd error
                else
                    cb null




## Controller actions

module.exports.create = (req, res, next) ->

    # Check if user is authenticated
    authenticator = passport.authenticate 'local', (err, user) ->
