Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AngularFire 1.2 - firebaseRefProvider and $firebaseAuthService #703

Merged
merged 15 commits into from
Mar 22, 2016

Conversation

davideast
Copy link
Contributor

Addresses proposal #684.

cc: @jamestalmage, @katowulf, @jwngr

Introduces two new services: firebaseRefProvider and $firebaseAuthService.

firebaseRefProvider

The firebaseRefProvider makes sure you never have to write new Firebase("...") ever again. This makes dependency injection with Firebase references much easier.

app.config(function(firebaseRefProvider) {
  firebaseRefProvider.registerUrl('https://<my-firebase-app>.firebaseio.com');
});

Multiple urls are also supported, but a default must be provided, and you cannot use a Firebase Ref property like path or then.

app.constant('FirebaseUrl', 'https://<my-firebase-app>.firebaseio.com/');
app.config(function(FirebaseUrl, firebaseRefProvider) {
  firebaseRefProvider.registerUrl({
    default: FirebaseUrl,
    messages: FirebaseUrl + 'messages'
  });
});

Then in a controller/service/factory you can inject the firebaseRef:

app.controller(function($scope, firebaseRef, $firebaseArray) {
   $scope.messages = $firebaseArray(firebaseRef.messages);
});

$firebaseAuthService

Once you have configured a default reference using firebaseRefProvider, the $firebaseAuthService will work as a automatically configured $firebaseAuth instance.

angular.module('app', ['ngRoute', 'firebase'])
  .constant('FirebaseUrl', 'https://<my-firebase-app>.firebaseio.com/')
  .controller('MyCtrl', MyController)
  .config(ApplicationConfig);

function ApplicationConfig(firebaseRefProvider, FirebaseUrl, $routeProvider) {
  firebaseRefProvider.registerUrl({
      default: FirebaseUrl,
      messages: FirebaseUrl + 'messages'
  });

  $routeProvider.when('/', {
    controller: 'MyCtrl as ctrl',
    template: '<h1> {{ ctrl.user }} </h1>',
    resolve: {
      user: function($firebaseAuthService) {
        return $firebaseAuthService.$waitForAuth();
      }
    }
  });
}

function MyController(user) {
  console.log(user);
}

@davideast davideast changed the title firebaseRefProvider and $firebaseAuthService AngularFire 1.2 - firebaseRefProvider and $firebaseAuthService Feb 22, 2016
@jwngr
Copy link

jwngr commented Feb 24, 2016

I'm not going to be able to give this PR the time and consideration it deservers. Leaving this for review by someone else.

@katowulf
Copy link
Contributor

katowulf commented Mar 3, 2016

Overall, this looks solid. How will queries and order by criteria work here? It looks like I would use firebaseRef.messages.orderByChild(...) and similar? I feel like more thought should be given to these and whether they need any special consideration.

function FirebaseAuthService($firebaseAuth, firebaseRef) {
return $firebaseAuth(firebaseRef);
}
FirebaseAuthService.$inject = ['$firebaseAuth', 'firebaseRef'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use $inject anywhere else. Note that I don't dislike this approach and I even find it more readable, but it's inconsistent, and if we're going to change the strategy, we should do that in a sep changelist.

The current format is: angular.module('firebase').factory('$firebaseAuthService', ["$firebaseAuth", "firebaseRef", FirebaseAuthService])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the proposed structure in the Angular Styleguide, which is supported by the core team.

I would like for us to move that way eventually. I think starting here is fine because of how small and isolated it is from the rest of the codebase.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@katowulf
Copy link
Contributor

Thanks, David! LGTM with one exception (firebaseRef vs $firebaseRef). Please look over the comments and we'll get this merged!

@katowulf katowulf merged commit d13a3b7 into master Mar 22, 2016
@katowulf katowulf deleted the de-provider branch March 22, 2016 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants