7

I'm trying to make a global variable called theUser which contains the userinfo if logged in.

I have tried putting this in my SPA php file:

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

And it works but only after refreshing page. (Login happens via angularjs logic + a $http request to auth/login which returns userinfo if logged in)

In my angular auth app, I have done this:

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

And that works, but only when the user logs in. If I refresh, theUser is empty. And I can't get these two solutions to work together. I probably need another approach.

All I want is an user variable that I can access from anywhere in my app, which is set if the user logs in, or have been logged in before. Using Laravel 5.1.

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

How can I make this work?

2
  • Currently unable to check if it helps your specific situation but with the help of scotch.io/tutorials/… I've created a small intranet application with a similar user object that stays up to date after logging in but retrieving it every time a user visits the settings page. Commented Aug 24, 2015 at 12:45
  • just reload the page after logging in. Commented Aug 25, 2015 at 12:16

4 Answers 4

4
+25

Why dont you use PHP-Vars-To-Js-Transformer by Laracasts. Then whenever a User logs in, you could set a Session variable auth_user and then get 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 the User logs out: \Session::forget('auth_user');

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

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

Comments

3

on top of the page under script tag

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

and

app.value('theUser',window.user);

app.run(function ($rootScope, theUser) {

       $rootScope.theUser = theUser;

});

Comments

2

For accessing the logged in user from your JavaScript files, you may try something like this (Create a view composer, also 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 (So User variable will be available as 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 need. Also, you may use a namespace like App.user. I wrote an article about this lasr year, you may check it here. Btw, it was for Laravel 4.x.

.

Comments

2

We have made use of html5 local storage to overcome this once the user is logged in, you just put the user's info on html5's local storage (works on all browsers, even mobile).

It has some drawbacks which you have to overcome, and also have some control on your routes filters to avoid someone loading page they shouldn't be allowed to see.

But I'm afraid my answer applies better to our solution and I don't think this answer is perfect but might guide you in a better solution. Our app publishes an API for angular's use and this angular is somewhat empowered with some extensions to ease routing.

2 Comments

How do you make sure that the session is not exoired.
That's one of the best points on using API calls to a rest endpoint and implemented in laravel. In angular just have to check if the http response has an HTTP status of 401, in laravel I just check if the user is Auth'ed and if not, response with that 401 status code.Then manage it by diverting the angular routing to the login page again (and do some session clean up)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.