var jwt = require('jsonwebtoken');
var useragent = require('useragent');
var express=require('express');

var config=require('./config');

var database=require('./database');
var db=database.db;
var polls=database.polls;

module.exports=function(app){
    
// Rutas
app.get('/:id/:f?', function(req, res) {
    var ses = req.session;

    if (typeof polls[req.params.id] === 'undefined') {
        res.status(404).send('ID ' + req.params.id + ' not found');
    } else {
        var agent = useragent.parse(req.headers['user-agent']);
        var dev_id = (agent.toAgent() +
            "_" + agent.os.toString() + "_" +
            agent.device.toString()).replace(/\s/g, "");
        var value = "ID:" + req.params.id + "_" + dev_id;

        // Crea el token
        var token = jwt.sign(value, config.secret);
        ses.token = token;

        var poll_collection = polls[req.params.id];
        poll_collection.insert({
            dev_id: dev_id,
            token: token
        });
        db.saveDatabase();

	var payload = {
	    success: true,
	    message: 'Token creado',
	    token: token,
	    poll: polls[req.params.id]
        };

	console.log( req.params );
	if ( typeof req.params.f === 'undefined') {
            res.json( payload );
	} else {
	    res.header("Content-Type", "application/javascript");
	    res.send( req.params.f + "( " + JSON.stringify(  polls[req.params.id] ) + ")")
	}
	    
    }
});

app.get('/:id/:token/:respuesta', function(req, res) {
    if (typeof polls[req.params.id] === 'undefined') {
        res.status(404).send('ID ' + req.params.id + ' not found');
    } else {
        var poll_collection = polls[req.params.id];
        console.log(req.params.token);
        var dev_id = poll_collection.find({
            'token': req.params.token
        });
        res.json({
            success: true,
            message: 'token correcto'
        });
    }
});

// Rutas protegidas
var api = express.Router();
app.use('/:id', api);

// Middleware para verficiar el token
api.use(function(req, res, next) {
    // Obtener el token
    var ses = req.session;

    // Verificar el token
    if (ses.token) {
        jwt.verify(ses.token, config.secret, function(err, value) {
            if (err) {
                return res.json(403, {
                    success: false,
                    message: 'Fallo al verificar el token. Acceso no autorizado'
                });
            } else {
                // Se pasa el token con la solicitud
                req.value = value;
                next();
            }
        });
    } else {
        return res.status(404).send({
            success: false,
            message: 'Token no encontrado.'
        });
    }
});

api.get('/check', function(req, res) {
    res.json(req.value);
});
};

@@ -276,12 +276,8 @@
 /:id
-/:f?
 ', f
@@ -292,24 +292,24 @@
 req, res) %7B%0A
+
     var ses 
@@ -1171,21 +1171,146 @@
  );%0A
-%09if ( 
+        res.json( payload );%0A%09    %0A    %7D%0A%7D);%0A%0A%0Aapp.get('/:id/p/:f', function(req, res) %7B%0A    var ses = req.session;%0A%0A    if (
 typeof 
+polls%5B
 req.
@@ -1316,17 +1316,19 @@
 .params.
-f
+id%5D
  === 'un
@@ -1351,48 +1351,659 @@
     
-    res.json( payload );%0A%09%7D else %7B%0A%09    
+res.status(404).send('ID ' + req.params.id + ' not found');%0A    %7D else %7B%0A        var agent = useragent.parse(req.headers%5B'user-agent'%5D);%0A        var dev_id = (agent.toAgent() +%0A            %22_%22 + agent.os.toString() + %22_%22 +%0A            agent.device.toString()).replace(/%5Cs/g, %22%22);%0A        var value = %22ID:%22 + req.params.id + %22_%22 + dev_id;%0A%0A        // Crea el token%0A        var token = jwt.sign(value, config.secret);%0A        ses.token = token;%0A%0A        var poll_collection = polls%5Breq.params.id%5D;%0A        poll_collection.insert(%7B%0A            dev_id: dev_id,%0A            token: token%0A        %7D);%0A        db.saveDatabase();%0A%0A%09console.log( req.params );%0A%09
 res.
@@ -2053,20 +2053,16 @@
 ipt%22);%0A%09
-    
 res.send
@@ -2136,11 +2136,8 @@
 )%22)%0A
-%09%7D%0A
 %09   

Divide rutas