Of all the answers here, I like @alknows best suited. However, like other answers that suggest sending a request to the server to receive current user data, there are several problems that I take with me:
- You must deal with race conditions as a result of an AJAX call ($ http).
- You send an unnecessary request to the server after it has already displayed your index.html
I tried the @alknow approach, and it worked for me after I was able to resolve many race conditions that arose as a result of using my application controllers and wiki angular, which need the current user to do their job. I try my best to avoid race conditions when appropriate, so I was a little reluctant to continue this approach. So I thought of a more efficient approach: send the current user data down with index.html and save it locally.
My approach: insert currentUser in index.html and save locally on the client
In the index.html on your server, make a script tag to store any data that you want to pass to the client:
`` ``
<script id="server-side-rendered-client-data" type="text/javascript"> var __ssr__CData = { currentUser: { id: '12345', username: 'coolguy', etc: 'etc.' } } </script>
`` ``
Then, as suggested by @alknows, in app.js or where you start your angular application, add app.run(..., () => {...}) . In app.run (), you'll want to grab the server-side client data object, which I named obscurely __ssr_CData , so Iām less likely to encounter name conflicts in the global namespace later in my other javascript:
var myAngularApp = angular.module("mainApp", ['ngRoute']); myAngularApp.run(function ($rootScope) { const currentUserFromServer = __ssr__CData.currentUser const currentUserAccessTokenFromServer = __ssr__CData.accessToken const currentUser = CurrentUser.set(currentUserAccessTokenFromServer, currentUserFromServer) $rootScope.currentUser = currentUser });
As you know, app.run() will be called whenever the page does a full reload. CurrentUser is a global class for managing my current user of an angular application in a single page environment. Therefore, when I call CurrentUser.set(...) , it saves the current user data in a place that I can get later in my angular application by calling CurrentUser.get() . Therefore, in any of your angular application controllers, you can now get the current user provided by the server simply by doing this:
myAngularApp.controller('loginController',function($scope, $rootScope, $http){ //check if the user is already logged in: var currentUser = CurrentUser.get() if(currentUser) { alert("HEY! You're already logged in as " +currentUser.username) return $window.location.href = "/"; } //there is no current user, so let user log in //... }
In this example, I used CurrentUser.get() , which I explained above, to get the previously saved current user from the server. I could also get this current user by contacting $rootScope.currentUser because I saved it too. It depends on you.
myAngularApp.controller('signupController',function($scope, $rootScope, $http){ //check if the user is already logged in: var currentUser = CurrentUser.get() if(currentUser) { alert("HEY! You're already logged in as " +currentUser.username) return $window.location.href = "/"; } //there is no current user, so let user signup //... you run your signup code after getting form data $http({method:'POST',url:'/signup',data:jdata}) .success(function(data,status,headers,config){ //signup succeeded! //set the current user locally just like in app.js CurrentUser.set(data.newUser) //send user to profile return $window.location.href = "/profile"; }) .error(function(data,status,headers,config){ //something went wrong console.log(data) }); }
Now, after the new user has registered, your server has returned the new user from the AJAX call. We set the new user as the current user by calling CurrentUser.set(...) and sending the user to our profile. Now you can get the current user in the profile controller just like you to check if the current user exists in the login and registration controllers.
I hope this helps anyone who comes across this. For your reference, I use the client session module to handle sessions on my server.