1

im working on an ionic project to create android and ios app. im new in ionic and need help to understand how to make a login trough restful services hosted on my server.

the restful uses this auth for every POST or PUT request:

username: admin
pass: 123123

so i am testing a POST request. i have no idea how to authenticate. the serverside is set up and running. and the api had been tested and no bug. (tested using cocoa restclient)

ok. this is my app.js

angular.module('tapp', ['ionic'])
.config(function($stateProvider, $urlRouterProvider){
  $stateProvider
   .state('testapp', {
     url: "/tapp",
     abstract: true,
     templateUrl: "template/menu.html",
     controller: 'MenuCtrl'
})
.state('tapp.home', {
      url: '/home',
      views: {
          'tappMenu': {
              templateUrl: 'template/home.html',
              controller: 'HomeCtrl'
          }
      }
})
.state('tapp.dash', {
      url: '/dash',
      views: {
          'tappMenu': {
              templateUrl: 'template/dash.html',
              controller: 'DashCtrl'
          }
      }
})

$urlRouterProvider.otherwise('/tapp/home');

here is my controller:

.controller('HomeCtrl', function($scope, $http){
    $scope.formData = [] // just testing post data
    $scope.submit = function(){
      var data = $scope.formData.push($scope.inputData);
      $http.post('http://localhost/tapp-server/posted', data);
   };
}

my php function (using codeigniter rest server)

public function posted_post()
{
    $this->response(json_encode($_POST), 200);
}

the issue here is, i dont know how to apply the basic restful authentication whenever the app will post the data to the php because whenever it post the data, i get error as "not authorized".

for other data such as getting the data using GET, i receive the data and processed flawlessly. only on this POST part i am in confusion.

faiz
  • 123
  • 5
  • 16

3 Answers3

0

Check this http://vitalflux.com/angularjs-post-data-using-ajax-spring-mvc-example/, it might help you to send post message instead of options. Otherwise, attach the response you get from the server so we can help you more

Omer72
  • 83
  • 1
  • 6
0

yes. i got it. $http is a low level request. for high level request i can use $resource.

found something in angular docs.

https://docs.angularjs.org/api/ngResource/service/$resource

can use this too:

https://github.com/mgonto/restangular#table-of-contents

faiz
  • 123
  • 5
  • 16
0

Also, please verify on server side since the API code working on localhost behaves different in production server depending on server type and auth mode.

If you facing problem, you should change your .htaccess as below:

OR add SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 in your current .htaccess file:

    <IfModule mod_rewrite.c>

        Options +FollowSymLinks
        RewriteEngine on
        SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
        #RewriteRule ^(.*)$ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
        #RewriteRule ^(.*)$ - [E=REMOTE_USER:%{HTTP:Authorization},L]
        #RewriteCond %{HTTP:Authorization} ^(.*)
        #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php|asset|robots\.txt)
        RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]

    </IfModule>

If you still has problem, change code of libraries\REST_Controller.php

Find this in your code:

    elseif ($this->input->server('HTTP_AUTHENTICATION')) 
    { 
      if (strpos(strtolower($this->input->server('HTTP_AUTHENTICATION')), 'basic') === 0) 
      {
          list($username, $password) = explode(':', base64_decode(substr($this->input->server('HTTP_AUTHORIZATION'), 6)));
      }
    }

and replace with:

    elseif ( $this->input->server('HTTP_AUTHENTICATION') || $this->input->server('REDIRECT_REMOTE_USER') || $this->input->server('REDIRECT_HTTP_AUTHORIZATION') )
                  {

                    $HTTP_SERVER_AUTH = ($this->input->server('HTTP_AUTHENTICATION')) ? $this->input->server('HTTP_AUTHENTICATION') : $this->input->server('REDIRECT_HTTP_AUTHORIZATION'); 
                   if(!$HTTP_SERVER_AUTH)
                   {
                     $HTTP_SERVER_AUTH = $this->input->server('REDIRECT_REMOTE_USER'); 
                   }


                    if (strpos(strtolower($HTTP_SERVER_AUTH),'basic') === 0)
                    {
                        list($username,$password) = explode(':',base64_decode(substr($HTTP_SERVER_AUTH, 6)));
                    }

                }
silwalprabin
  • 637
  • 7
  • 20