A
A
Artur Bekerov2014-01-21 10:07:01
JavaScript
Artur Bekerov, 2014-01-21 10:07:01

How to send authorization data to Angular?

Hello.
How to send authorization data?
Slim framework backend:

$app->get('/goods',authorize('user'), 'getGoods');
function login() {

    if(!empty($_POST['email']) && !empty($_POST['password'])) {
        if($_POST['email'] == 'admin' && $_POST['password'] == 'admin') {
            $user = array("email"=>"admin", "firstName"=>"admin", "lastName"=>"admin", "role"=>"user");
            $_SESSION['user'] = $user;
            echo fix_json(json_encode($user));
        }
        else {
            echo '{"error":{"text":"Неверный логин-пароль"}}';
        }
    }
    else {
        echo '{"error":{"text":"Необходимо авторизоваться. need login()"}}';
    }
}


services.js
appServices.factory('Login', ['$resource',
  function($resource){
    return $resource('api/login', {}, {
      post: {method:'POST',params:{email:'admin',password:'admin'}}
    });
  }]);

controllers.js
function LoginCtrl(Login, $location, $scope, $http) {
    Login.post();

}
LoginCtrl.$inject = ['Login', '$location', '$scope', '$http'];


After sending, Angular receives some kind of cookie, it is not clear from where, although according to the logic of slim, it is not given until we enter the correct login and password.
How to send authorization data to Angular?

Everything works on jquery.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2014-01-21
@krekerov

This is probably because Angular sends the request as application/json, not www-form-urlencoded.
Try writing something like this at the beginning of your PHP script:

if (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') === 0) {
    $_POST = @json_decode(file_get_contents('php://input'), true);
    if (!is_array($_POST)) $_POST = [];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question