2

I'm looking for some inspiration (help), in order to do a good login system for my application based my needs, so the scenario goes like this:

I have a backend application which when logged, created a session and the only thing I can see it set on my browser is a PHPSESSID cookie.

So, I have my application done and define like this.

 var app = angular.module('myApp', []);

As far as I know, I would have to do something like, check the login controller, if successful, create the line of code I copied and change the view, else, do not created.

So, I thought in creating a controller for this, however, should I create another app only for this? so my ng-app would have 2 applications?, or no matter is logged or not, instantiate the app and hide the home buttons as long as the login is not successful?

I'm kind of lost in here, I would appreciate any help that points me on the right direction.

1

1 Answer 1

3

There are many approach to achieve it. I am only talking the approach that I usually used. Let's say, in the pictures below, user able to view the blog without login. After user is login, the black navigation bar on the top will appear. How to achieve it by using AngularJS?

enter image description here

In the article.html

<div ny-app='myapp' ng-controller='mycontroller'>
   <user-navbar ng-if="user" user-data='user'><user-navbar>
   <article-content></article-content>
</div>
<script src="angularjs....."></script>
<script>
angular.module('myapp',[]).controller('mycontroller',['$scope','$http', 
    function($scope,$http){
        $http.get('/getuserdata.php').success(function(user){
             // you will get user data only when session is valid.
             $scope.user = user;
        });
    }
]);
</script>

In getuserdata.php

<?php

     if(!empty($_SESSION['user'])){
         echo json_encode($_SESSION['user']);
     }
     echo null;
?>

The code above shows that when user is login and session is set in user key. $_SESSION['user'] will contain the information of your user and you just response it to frontend AngularJS by echo.

Then, $http will receive the user data that requested and assign to $scope.user and navigation bar will shows if $scope.user is not null. You don't have to due with PHPSESSID. PHP session will handle everything for you if you coding correctly.

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

2 Comments

If you have any question, please feel free to ask.
@edisonthk, Suppose in our app landing page is login page, and once user logged in then he will navigate to app dashboard. In short, without login nothing can be accessed...How can i do that? I have two types of templates one for guest user(without logged in) another for logged in(Registered User).

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.