0

I'm tring to implement an JWT for login with Angular 1.5.8 + CodeIgniter 3.1 on localhost, and I have a problem, console throwing token as 'undefined', from the server still {"error":"Wrong mail or password"}, but data is correct with database. Any suggestions, help, anything.

    //-------------------User.php----------------------------
public function login()
{
    $email = $this->input->post( 'email' );
    $password = $this->input->post( 'password' );
    $password = crypt( $password , config_item( 'encryption_key' ) );

    $login = $this->User_model->login( $email , $password );

    if ( !$login )
    {
        $output['error'] = 'Wrong mail or password';
    }
    else
    {
        $token = $this->jwt->encode( array(
            'userId' => $login->id,
            'name' => $login->name,
            'email' => $login->email,
            'role' => $login->role
        ) , config_item( 'encryption_key' ) );

        $output['token'] = $token;
    }

    echo json_encode( $output );

}


//----------------User_model.php-------------------------
public function login( $email , $password )
{
    $this->db->where( 'email' , $email );
    $q = $this->db->get( 'users' );
    $result = $q->row();

    if ( empty( $result ) || $password != $result->password )
    {
        $output = false;
    }
    else
    {
        $output = $result;
    }

    return $output;

}


//----------------controler-user.js-------------------------
myCtrlsUser.controller( 'userLogin' , [ '$scope', '$http', 'store' , function( $scope, $http, store ){


    $scope.user = {};

    $scope.formLogin = function ( user ) {

        $http.post( 'api/site/user/login/' , {
            email : user.email,
            password : user.password
        }).success( function( data ){

            $scope.submit = true;
            $scope.error = data.error;

            if ( !data.error )
            {
                store.set( 'token' , data.token );
            }
            console.log( store.get( 'token' ) );

        }).error( function(){
            console.log( 'Error #020: Problem with log in' );
        });
    };



}]);
  • You should get your data from the input stream, not from the `$_POST` Check this answer http://stackoverflow.com/a/23805260/4203289 – Alexander Popov Oct 20 '16 at 18:16
  • I used in parent::__construct $post = file_get_contents( 'php://input' ); $_POST = json_decode( $post, true ); I tried method from your answer but still the same problem – Przemyslaw Krzywda Oct 20 '16 at 18:47
  • Ok, after few hours other methods I found my mistake, in HTML I used user.name instead user.password, and also I changed one IF instruction . Now working properly. Thx all – Przemyslaw Krzywda Oct 21 '16 at 09:32

0 Answers0