V
V
Vladislav2015-04-08 10:02:33
Angular
Vladislav, 2015-04-08 10:02:33

How to work with the user in Angular?

Now created a user based on $resource. But working with it is not very convenient, creating an instance in each controller. Is there a way to avoid having to .get() through the server in all controllers when there is only one user on the page who is already logged in?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2015-04-08
@RGV

I prefer server auth. The action plan is as follows:
- the backend checks if the user is authorized
- if not = we throw it on the authorization page
- if yes = we start the SPA
In the SPA itself, we render the user data with the backend before the scripts:

<script>
    window.$$userData = <?= json_encode($_SESSION['user']); ?>;
</script>
<script src="/path/to/angular.js"></script>
<script src="/path/to/app.js"></script>

We create a service, which can then be injected anywhere.
(do not forget that .constant is .value, which is available at the configuration stage and cannot be changed via $decorator - accordingly, you can safely change object fields and they will be updated everywhere)
app.config(['$provide', function($provide) {
  $provide.constant('userData', angular.copy(window.$$userData));
}]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question