简体   繁体   中英

How to implement roles in AngularJs

I am working on application with angularJS and php backend, and I need to idientify 2 roles, client and admin, each one with a totally different space of the other. So I think a simple "if" condition in the authentication step can solve the problem.

To understand the code:

The authentication is done by checking the email and password of users from two tables: one admin table and one client table, each table contains an "admin" role column for the "admin" and "client" role for The "client" table.

The problem:

How to put the if condition on the result of the select, and then if the role is admin: it will enter the admin space, if it is the client, the reverse.

What I tried to do:

login.php

 <?php  

$data = json_decode(file_get_contents("php://input"));

 $connect = mysqli_connect("localhost", "root", "", "test");  

 if(count($data) > 0)  

 { 

$Email=mysqli_real_escape_string($connect, $data->Email);
$mdp=mysqli_real_escape_string($connect, $data->mdp);

$query = 'SELECT * FROM `client`,`admin` WHERE (EmailClient = "'.$Email.'" AND   mdp= "'.$mdp.'") OR (EmailaAdmin = "'.$Email.'" AND   MdpAdmin= "'.$mdp.'")';

$q = mysqli_query($connect , $query);

if(mysqli_num_rows($q) > 0 )
  { 
       $_SESSION["logged_in"] = true; 
       $_SESSION["naam"] = $Email; 
       $result['code'] = 200;
       $result['message'] ='Logged In';
  }
  else
  {
       $result['code'] = 603;
       $result['message'] ='The username or password are incorrect!';
  }

$resultstring = json_encode($result);
$resultstring = str_replace("null",'""',$resultstring);
echo $resultstring;
}

?>

app.js

app.controller('loginCtrl', function($scope, $location,$state,$http,$window){

    $scope.submit = function()
    {
        data = {
            'Email' : $scope.Email,
            'mdp' : $scope.mdp
        };

        $http.post('http://localhost/test/login.php', data)
        .success(function(data, status, headers, config,result)
        {
            console.log(data);
            if(data.code == 200){
              if(data.role=client){
                $state.go('client');
              }
              else{
                $state.go('admin');
              }
          }
          else{
            alert('incorrect');
          }
        })
        .error(function(data, status, headers, config, rsult)
        {   

            console.log('error');

        });
    }

});

Thanks in advance

A simple approach could be

<?php
    $data       = json_decode(file_get_contents("php://input"));
    $connect    = mysqli_connect("localhost", "root", "", "test");
    if(count($data) > 0)
    { 

        $Email      = mysqli_real_escape_string($connect, $data->Email);
        $mdp        = mysqli_real_escape_string($connect, $data->mdp);
        $query      = 'SELECT * FROM `admin` WHERE (EmailClient = "'.$Email.'" AND   mdp= "'.$mdp.'") OR (EmailaAdmin = "'.$Email.'" AND   MdpAdmin= "'.$mdp.'")';
        $q          = mysqli_query($connect , $query);
        if(mysqli_num_rows($q) > 0 )
        { 
            $_SESSION["logged_in"] = true; 
            $_SESSION["naam"] = $Email; 
            $result['code'] = 200;
            $result['message'] ='Logged In';
            $result['role'] ='admin';
            $resultstring = json_encode($result);
            $resultstring = str_replace("null",'""',$resultstring);
            echo $resultstring;
            exit;
        }
        $query2     = 'SELECT * FROM `client` WHERE (EmailClient = "'.$Email.'" AND   mdp= "'.$mdp.'") OR (EmailaAdmin = "'.$Email.'" AND   MdpAdmin= "'.$mdp.'")';
        $q2         = mysqli_query($connect , $query2);
        if(mysqli_num_rows($q2) > 0 )
        { 
            $_SESSION["logged_in"] = true; 
            $_SESSION["naam"] = $Email; 
            $result['code'] = 200;
            $result['message'] ='Logged In';
            $result['role'] ='client';
            $resultstring = json_encode($result);
            $resultstring = str_replace("null",'""',$resultstring);
            echo $resultstring;
            exit;
        }
        $result['code'] = 603;
        $result['message'] ='The username or password are incorrect!';
        $resultstring = json_encode($result);
        $resultstring = str_replace("null",'""',$resultstring);
        echo $resultstring;
        exit;
    }

And in angular check role via if(data.role=='client'){ //client or if(data.role=='admin'){ //admin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM