简体   繁体   中英

Firebase simple login for authentication with the REST api

I want to create a web app with AngularJS and Firebase but only some parts of the app need the 3-way binding Firebase offers. The other parts would work just finding binding only once to the current data without constantly updating it. In other words, a normal REST API would suffice.

The Thinkster AngularJS-Firebase tutorial shows how to use $resource to access this Firebase REST API. This approach does not require to open a concurrent connection to your Firebase and only fetches the current data without updating:

app.factory('Post', function ($resource) {

return $resource('https://FIREBASE-URL.firebaseIO.com/posts/:id.json');
});

Would it be possible to do these kind of requests after logging in to Firebase using the simple login service, but without keeping a Firebase connection open. For example:

//Does this already open a connection to FireBase?
var ref = new Firebase('https://FIREBASE-URL.firebaseIO.com');

//Login to FireBase
ref.authWithPassword({
  email    : "bobtony@firebase.com",
  password : "correcthorsebatterystaple"
}, function(error, authData) {
  if (error === null) {
    // Can we close the connection now while still leaving the user authenticated?
    Firebase.goOffline();
  } else {
    console.log("Error authenticating user:", error);
  }
});

If I would make requests to the REST API now:

  1. Will Firebase recognize the auth variable?
  2. Does the authorization stay valid?
  3. Can I use this method to prevent the connection to stay open and use Firebase as a normal REST API?

To make things a little bit more clear:

  • I do not need a real-time backend, a simple REST backend will suffice
  • I would like to use Firebase because it provides easy authentication methods
  • If possible, I want to avoid using the Firebase SDK and only interact with my Firebase using the REST API

In other words my question would be:

Is it possible to authenticate users client side to use the security rules of Firebase while using Firebase as a REST API without using the SDK?

This is a case of extremely premature optimization, and it's also detrimental more than helpful.

There's no advantage to using the REST API and SDK in tandem. If you already have the SDK downloaded and have already established a socket connection, it's going to be more efficient, cheaper, and less complex to re-use the existing connection. The SDK is also heavily optimized over the REST API and much better at dealing with temporary disconnections and networking errors. By utilizing the REST API, you'll essentially be making your life more difficult for zero gain.

If your goal is to download data exactly once without listening for updates, simply use once in place of on.

app.controller('ctrl', function($scope) {
  var ref = new Firebase('https://FIREBASE-URL.firebaseIO.com');
  ref.once('value', function(snap) {
     $scope.data = snap.val();
     $scope.apply();
  });
});

Creating a ref() does not immediately create a connection. The first time you authenticate, write, or read data, the connection is established.

Authenticating via the SDK does not help at all for REST queries. You need to pass ?auth=TOKEN into the URL, where TOKEN is an auth token that you create manually (not at all connected to the SDK simple login auth event).

If you don't want real-time capabilities, don't use the SDK at all. Simply utilize $http or $resource and connect via the .json URLs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM