0

I am using AngluarJS since 2 days and i am now trying to build my authentication system.

I have several controllers within i need to reflect connected user credentials. Something like:

<body>
  <div ng-controller="topController">
    <h1>{{User.name}}</h1>
  </div>
  <div ng-controller="contentController">
    <h1>{{User.age}}</h1>
  </div>
  <div ng-controller="bottomController">
    <h1>{{User.email}}</h1>
  </div>
</body>

So since it's not appropriate to load the User into a controller, i guess the best way is to work with an external service and get it back on the different controllers's callbacks.

But by doing that, i am wondering how (where) could we load the session at the fisrt loading of the page (let's imagine i just have a single view to ignore the router mechanism) ? I am looking for a kind of "main" point.

I am using the facebook authentication so my question is not really about how to use the cookies with angular, but more about the "global logic" to set up.

If someone could give me an idea about how to do that, or give me some explanation about a best way to think the authentication mechanism, it would be nice !

2
  • Look at this blog.brunoscopelliti.com/… and this ninjatronic.github.io/ngFacebook Commented Dec 31, 2013 at 16:15
  • @Chandermani thanks, i have already seen the blog of brunoscopelliti, it's focus on the service itself and not on how to use it in the app context, i still don't have a clue about how to fetch the user session on the first loading page after reading its article (i mean how/where to call the service). Commented Dec 31, 2013 at 16:21

1 Answer 1

1

You generally do global initialization code by passing an initialization function to the run method of your main app module like so:

angular.module('myApp', [/* some dependencies */])
  .run(function(/* some dependencies */) {
    // Do anything you want to do after all modules load
    // but before your DOM/directives are processed by Angular.
  });

See here: http://code.angularjs.org/1.2.6/docs/api/angular.Module#methods_run

This would seem to be a good place to do any session stuff you want to do with Facebook. If your Facebook interactions are asynchronous, then you'll probably need to delay your main app controllers ("topController", "contentController" etc) from doing their thing until your figure out what's happening with the session (since it won't happen synchronously in this run function). One solution to this could be to set up routing so that you don't get routed to the view with those main app controllers until the session logic finishes. In the run initialization function, when your asynchronous FB session stuff is done (and you've stored the session info in a Service or something), you can make a call to change routes so you load the main app view, and at that point whatever Service you're storing the session in should be all set with your FB session.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.