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QdD1E7CBEcLP_RuBvw8vSQ_CQAceJ9DguHo1GsOH0fuqKhVgpoU3xBRrx-p9_WyMtN7qCYvEv6JDH-Lp_TjmXvz9qBmVU_nDLgn3Oxw8XhZKg";
var miCredencial = "879204284051-ggu11jpg089n5voslom9s14knka0ca99.apps.googleusercontent.com";
var userId = "104128311395160365072";

// GET /usuarios/propiedades

exports.propiedades = function(req, res) {
	models.Usuarios.findAll().then(function(usuarios){

		console.log("[0] ", usuarios[0].dataValues);
		/*
		console.log("[1] ", usuarios[1]);
		console.log("[2] ", usuarios[2]);
		console.log("[3] ", usuarios[3]);
		console.log("[4] ", usuarios[4]);
		console.log("[5] ", usuarios[5]);
		*/

		res.render('usuarios/propiedades', usuarios[0].dataValues);


		/*
		res.render('usuarios/propiedades', 
			{
				firebase_id: usuarios[0],
				nombre: usuarios[1],
				username: usuarios[2],
				password: usuarios[3],
				creado: usuarios[4],
				actualizado: usuarios[5]
			});
			*/
	});
};

exports.signin_debug = function(req, res) {

	console.log("Singing in: --------------------------------------->");
	console.log("body: " + req.body);

};

exports.signin = function(req, res) {

	console.log("POST Singing in: ---> " + JSON.stringify(req.body));
	var datos = req.body;
	console.log("deviceID: " + datos.deviceId);

	/*
	var qs = JSON.stringify(req.body);
	console.log("###########query string: ", qs);

	console.log("******************req.body.tokenId: ", req.body.tokenId);

	*/
	var post_options = {
		url: 'https://www.googleapis.com/oauth2/v3/tokeninfo',
		qs: {id_token: req.body.tokenId},
		method: 'GET',
	};

	function callback(error, response, body) {

		if (!error && response.statusCode == 200) { 

			console.log("Signin OK!: " + response.statusMessage + " " + JSON.stringify(body));

			accountProfile = JSON.parse(body);

			if(accountProfile.aud === miCredencial)
			{
				console.log("Credencial OK!: " + response.statusMessage + " " + JSON.stringify(body));

				// guardar userId ==> accountProfile.sub
			}
			else {
				console.log("Credencial mismatch!!!: " + response.statusMessage + " " + JSON.stringify(body));
			}
		}
		else {
			console.log("Falló Signin: " + error + " " + response + " " + JSON.stringify(body));
		}




		/*
		// These six fields are included in all Google ID Tokens.
		accountProfile.iss": "https://accounts.google.com",
		accountProfile.sub": "110169484474386276334",
		accountProfile.azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
		accountProfile.aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
		accountProfile.iat": "1433978353",
		accountProfile.exp": "1433981953",

		// These seven fields are only included when the user has granted the "profile" and
		// "email" OAuth scopes to the application.
		accountProfile.email": "testuser@gmail.com",
		accountProfile.email_verified": "true",
		accountProfile.name" : "Test User",
		accountProfile.picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",
		accountProfile.given_name": "Test",
		accountProfile.family_name": "User",
		accountProfile.locale": "en"
		*/

		//console.log("Respuesta al signin: " + JSON.stringify(accountProfile));
		console.log("Respuesta al signin: OK");



		//res.send(JSON.stringify(accountProfile));
		res.send("OK");

		/*
		res.render('usuarios/signin', {
			// These six fields are included in all Google ID Tokens.
			iss: accountProfile.iss,
			sub: accountProfile.sub,
			azp: accountProfile.azp,
			aud: accountProfile.aud,
			iat: accountProfile.iat,
			exp: accountProfile.exp,//
			// These seven fields are only included when the user has granted the "profile" and
			// "email" OAuth scopes to the application.
			email: accountProfile.email,
			email_verified: accountProfile.email_verified,
			name: accountProfile.name,
			picture: accountProfile.picture,
			given_name: accountProfile.given_name,
			family_name: accountProfile.family_name,
			locale: accountProfile.locale
		});
		*/
	}

	//request(post_options, callback);

};
