Is it possible to restrict access to variable information with Angularjs?

Asked

Viewed 419 times

5

Hello,

I am new to development and would like to know if there is any way to hide information from variables in Angularjs , I am using a Mongolab API and did not want to make my apiKey visible to anyone who debug the code in the browser, I am running the application with Nodejs and Expressjs , the server code was generated through the ExpressJS CLI in that way: npm install express-generator -g and soon after express meuApp.

Follows code from controller:

app.controller("RBScontroller", function ($scope, $http) {

$scope.enviar = function enviar(){

    var ApiMongo = 'https://api.mongolab.com/api/1/databases/db_test/collections/users?apiKey=Chave_da_APi';  //Gostaria de esconder essa informação
    $scope.users = [];
    $scope.loading = true;

    $http.get(ApiMongo).success(function(data) {
        console.log(data);
        $scope.users = data;
        $scope.loading = false; 

    }).error(function(msg) {      

        angular.element($(function(){alert("Fail")}));
        $scope.loading = false;
    });
 }  
});

2 answers

7


The simple answer is nay, there is no way to hide from the client side. And, even if there was, the URL called could be intercepted (via debug tools).

However, you can use a different approach. Instead of calling via Angular, use a via function http.request in Nodejs, and return the result to your application.

  • Ah, you had already answered that there. The dynamic reading made it impossible for me to see the http.request. +1

  • 1

    Thanks man, I’ll research then how does it in Nodejs! ;)

3

Friend, there is no way to do this through javascript.

The only thing that can be done - and that’s what I would do if I had to hide that information - is to make a proxying.

Translating, you will send the information to your server, and your server in turn will make the request you need - rather than directly.

Exemplifying with PHP

#requisicao.php

$conteudo = file_get_contents('https://api.mongolab.com/api/1/databases/db_test/collections/users?apiKey=Chave_da_APi');

return Response::json($conteudo);

Javascript:

$scope.enviar = function enviar(){

    var ApiMongo = 'requisicao.php'; // A informação só é vista pelo programador que acessar requisicao.php
    $scope.users = [];
    $scope.loading = true;

    $http.get(ApiMongo).success(function(data) {
        console.log(data);
        $scope.users = data;
        $scope.loading = false; 

    }).error(function(msg) {      

        angular.element($(function(){alert("Fail")}));
        $scope.loading = false;
    });
 }  
});

In case, how are you using node.js from the server side, I believe you could do that.

  • +1, more detailed description. =)

  • Thanks Wallace, with Node do not know how it does , I think I will have to use some other language so, this example you showed me seems simple to do , I will try! ;)

  • What’s this, boy? How don’t you know? @Onosendai hinted: use http.request to capture the information

Browser other questions tagged

You are not signed in. Login or sign up in order to post.