lundi 23 novembre 2015

How to pass data from angular to laravel

I am following this tutorial :-

 http://ift.tt/1WXG0hn
 http://ift.tt/1OdmVCe

In this tutorial the angular files reside in laravel's public directory and its working fine for me. But I want a separate folder for angular frontend and laravel backend. When I set this up, I am able to register the user (the data gets stored in database) but I am unable to login properly as the tokens are not generated.

I think that the credentials are not being passed and thus laravel fails to generate tokens. Also, I think the problem lies in my controller somewhere here :-

$auth.login(credentials).then(function(){
            return $http.post('http://localhost:8000/api/authenticate/user');
        }

authcontroller.js

 reglogApp.controller('AuthController', function($scope, $http, $auth, $rootScope, $state){
$scope.email='';
$scope.password='';
$scope.newUser={};
$scope.loginError=false;
$scope.loginErrorText='';


$scope.login = function(){
    var credentials = {
        email: $scope.email,
        password: $scope.password
    }

    console.log('Entered Login Function', credentials);

    $auth.login(credentials).then(function(){

            return $http.post('http://localhost:8000/api/authenticate/user');

        }, function(error){
            $scope.loginError = true;
            $scope.loginErrorText = error.data.error;
            console.log('Login Error', $scope.loginErrorText);
        })
        .then(function(response){
            $rootScope.currentUser = response.data.user;
            $scope.loginError = false;
            $scope.loginErrorText = '';
            console.log('Current User', $rootScope.currentUser);
            $state.go('dashboard');
        });
}

$scope.register = function(){
    $scope.name = $scope.newUser.name;
    $scope.email = $scope.newUser.email;
    $scope.password = $scope.newUser.password;
    console.log($scope.name, $scope.email, $scope.password);
    $http.post('http://localhost:8000/api/register', $scope.newUser).success(function(data){
        console.log('Registered');
        $scope.email = $scope.newUser.email;
        $scope.password = $scope.newUser.password;
        $scope.login();
    });
}
});

Routes.php

Route::post('/api/register', 'RegisterController@register');
Route::post('api/authenticate', 'LoginController@authenticate');
Route::post('api/authenticate/user/', 'LoginController@getAuthenticatedUser');

RegisterController.php

public function register(Request $request)
{
    $newuser= $request->all();
    $password=Hash::make($request->input('password'));

    $newuser['password'] = $password;
    return Register::create($newuser);
}

LoginController.php

public function authenticate(Request $request){
    $credentials = $request->only('email', 'password');
    try{
        if(! $token = JWTAuth::attempt($credentials)){
            return \Response::json(['error' => 'invalid_credentials'], 401);
        }
    }
    catch(JWTException $e){
        return \Response::json(['error' => 'could_not_create_token'], 500);
    }

    return \Response::json(compact('token'));
}



public function getAuthenticatedUser(){
    try{
        if(! $user = JWTAuth::parseToken()->authenticate()){
            return \Response::json(['user_not_found'], 404);
        }
    }
    catch(Tymon\JWTAuth\Exceptions\TokenExpiredException $e){
        return \Response::json(['token_expired'], $e->getStatusCode());
    }
    catch(Tymon\JWTAuth\Exceptions\TokenInvalidException $e){
        return \Response::json(['token_invalid'], $e->getStatusCode());
    }
    catch(Tymon\JWTAuth\Exceptions\JWTException $e){
        return \Response::json(['token_absent'], $e->getStatusCode());
    }

    return \Response::json(compact('user'));

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire