Login / Authentication with AngularJs and PHP

6

Well, I'm doing WebApp where I have a login page before accessing the app and inside the app I have some areas with restrictions. WebApp is running all right, but I ended up getting stuck in the process of logging in and authenticating.

WebApp is based only on AngularJs and PHP with database on MYSQL .

What I have:

So far I'm using a simple login with authentication through PHP. I can log in and view WebApp only if it is logged in. Okay. But I can not go any further than that. A brief example of the code I'm using:

index.php

<?php session_start();
    include ('dist/php/config.php');

    if(isset($_GET['out'])){
        session_destroy();
        back("#");
    }
    if((!empty($_POST['user'])) && (!empty($_POST['password']))){

        $p = ['user'=>$_POST['user'], 'password'=>$_POST['password']];
        $r = sql("select * from users where user= :user and password = :password",$p);
        if($r != 0){
            foreach($r as $ln){
                $_SESSION['loggedin']=$ln['name_user'];
            }
        } else {
            $msg = "<div class='login_fb'><p>User or password incorrect</p></div>";
        }
    }

    if(!empty($_SESSION['loggedin'])){
        include "system.php";
    } else { ?>

<head>
    <meta charset="UTF-8" />
    [... resto do head ...]
</head>
<body>
     [... resto do body com form de login ...]
</body>
<?php } ?>

The route system I use in the app is ui-router .

The problem I have faced for now is in doing user authentication and keeping this data for future use. Data such as the user name, id, and permission category that is entered. Even keep this data even after a refresh of the page.

My goal

What I intend is the following:

  • Log in to WebApp ;
  • Keep the user's data (as previously mentioned) for future use in the other pages and in the various actions that the user can do;
  • It is possible to refresh the page or close the tab and return later without losing the login (nor the user data);
  • Control user access. Ex: release page X only if the user is of the category "Super Admin";
  • Impossible to access without going through the form. Example: You can not access the page by typing directly into the URL - Return with "You do not have permission" or "You must be logged in.";

I even found some examples and tutorials on the internet, but they are either too complex, or lack clear documentation to follow, or in some cases, have some pretty harmful flaws. The best content I got was this one , but the explanation already starts at a more advanced point . I needed more guidance in the initial steps.

For example, how to validate the login based on the data of the form and then keep this in cache / cookis (would this?) and then proceed with some areas described in the above link guide.

I also found a reference recommending the use of $cookie ($ cookieStore was deprecated, second docs ) this way:

app.run(['loginService', function(loginService){
    var username = $cookieStore.get('username');
    var password = $cookieStore.get('password');
    loginService.login(username, password);
}]);

But is it correct / advisable to use this way? Because we are manipulating the password variable within the angular, can not this bring security to the user's login? As far as I know, it's not ideal.

What I need to know:

  • Is this login template (using that php code) I am using as recommended? Is there any better or more suitable for my purpose?
  • How can I prevent the user from accessing any page of WebApp without having to go through the login process? (I believe it will be automatically answered if I can handle the login with the data cache for use in future sessions.)
  • How can I store this client data (as well as login) so that when it returns to the page, stay logged in?
  • This data needs to be kept in all page change / exit scenarios, as long as there is no exit through Logout.

Finally, that's it. I believe with the information to make the initial handle I get a north to give procedure to WebApp.

    
asked by anonymous 23.09.2015 / 04:18

1 answer

3

I suggest you study more about data-driven applications. You do not have to worry about route security on the frontend because it does not exist .

AngularJS is an excellent tool to create Single Page Applications , ie you do not have to be generating new GET requests directly by the browser and mixing PHP with HTML / Javascript.

Begin the basics and try to evolve little by little.

  • Write HTML with login form
  • Write a controller for this HTML that makes an Ajax request and receives a 200 OK response in case of successful authentication or 4xx error in case of authentication failure.
  • Try to move the controller's request logic to a service / factory in Angular.
  • Write a function that makes a request to the server to check for valid authentication (session or token). This way, every time the user loads the application for the first time, you can check if there is a logged-in user.
  • Store basic logged-in (name, email) data in $ cookies or $ ngStorage.
  • Write your first page Hello World, {{usuário}} to the logged in user.

After achieving these steps, you may notice that you can access the Hello World page even if you are not authenticated, but this is a question of usability rather than security. Security should be contained on your server (backend application) because frontend security is easily breakable. That is, first worry about reaching the secure basics (even if the frontend introduces you to hello world without authenticating), but do not show the username without authenticating (secure). In a second moment you will be able to study on how to guarantee security of the routes in AngularJS. This will not bring security to your application, but rather better usability for non-malicious users.

Want a practical example of what I'm talking about? Go to link and after loading the page, delete the #login url and type #transactions (the URL will look like this: link ). Note that you have full access to the frontend application even though you are not authenticated, however there is no information in that application. Data from all customers is secure and frontend security is more of a usability issue than really security.

    
05.01.2016 / 15:28