Creating a registered custom variable in AngularJS + laravel

I am trying to make a global variable called user that contains userinfo if you are logged in.

I tried putting this in my php php file:

app.value('theUser', '<?php echo Auth::user(); ?>'); 

And it works, but only after refreshing the page. (Login occurs via angularjs logic + $ http request for authorization / login, which userinfo returns when entering the system)

In my angular auth application, I did this:

 var login = $http.post("auth/login", sanitizeCredentials(credentials)).success(function(data) { theUser = data; $rootScope.theUser = theUser; }); 

And it works, but only when the user logs in. If I upgrade, then the user is Empty. And I cannot get these two solutions to work together. I probably need a different approach.

All I want is a user variable, access to which I can get from anywhere in my application, which is set if the user logs in or has previously been registered. Using Laravel 5.1.

Here is my aph app js service: http://pastebin.com/HcdLaZcD

Can anyone deal with me? Thanks.

+7
angularjs authentication laravel
source share
4 answers

Why don't you use Laracasts' PHP-Vars-To-Js-Transformer . Then, when a User logged in, you can set the Session auth_user variable, and then pass that variable to JavaScript, as shown below (in your Laravel AuthController@login ):

 ... \Session::put('auth_user', \Auth::user()); JavaScript::put([ 'theUser' => \Session::get('auth_user') ]); ... 

And when User exits: \Session::forget('auth_user');

theUser variable will be available anywhere in your JavaScript (or you can also use the namespace, check out the Github link above).

+4
source share

at the top of the page under the script tag

 window.user = <?php echo Auth::user(); ?> 

and

 app.value('theUser',window.user); app.run(function ($rootScope, theUser) { $rootScope.theUser = theUser; }); 
+3
source share

To access the registered user from your JavaScript files, you can try something like this (create a view composition , layouts.master dictates layouts/master.blade.php ):

 View::composer('layouts.master', function($view) { $user = null; if(Auth::check()) { $user = Auth::user(); } $view->with('authUser', $user); }); 

In the master layout, try this variable (So User will be available as a JS object if the user is logged in):

 <head> <script>var User = {{ $authUser or 'undefined' }}</script> </head> 

This is just an idea; implement it according to your needs. Alternatively, you can use a namespace like App.user . I wrote an article about this year, you can check here . Btw, this was for Laravel 4.x 4.x.

.

+2
source share

We used the html5 local storage to overcome this, as soon as the user is logged in, you just put the user information in the html5 local storage (it works in all browsers, even mobile).

This has some disadvantages that you must overcome, as well as have some control over the route filters to avoid loading a page that they should not see.

But I am afraid that my answer will be better applied to our solution, and I do not think this answer is perfect, but it may help you in a better solution. Our application publishes an API for using angular, and this angular is somewhat empowered with some extensions that make routing easier.

+2
source share

All Articles