卫星警告"期望一个名为令牌的令牌"使用JWT进行laravel身份验证

时间:2015-11-19 07:29:39

标签: angularjs laravel laravel-5 jwt satellizer

我正在关注本教程 - https://www.codetutorial.io/laravel-and-angularjs-token-based-auth-part1/

我通过制作一个单独的laravel后端和另一个用于角度前端的文件夹来修改它。截至目前,我在localhost上运行它。

登录我的应用程序时,收到以下消息: -

Satellizer Warning Expecting a token named token

Current user is undefined.

我的登录控制器 -

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'));
}

routes.php文件

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

我的角色控制器: -

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

$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/lab/reglog/public/api/register', $scope.newUser).success(function(data){
        console.log('Registered');
        $scope.email = $scope.newUser.email;
        $scope.password = $scope.newUser.password;
        $scope.login();
    });
}

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

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

    $auth.login(credentials)
        .then(function(){
            return $http.get('http://localhost/lab/reglog/public/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');
        });
}
});

1 个答案:

答案 0 :(得分:0)

Satellizer期望你的帖子请求json中的顶级属性叫做" token",它不能在你的json中的另一个对象里面,它必须在顶层:

{
    token: "<your JWT token>",
    user:{
           ...
          }
}

这是我遇到此错误的唯一时间。 它也必须包含在API的每个经过身份验证的请求中(显然)。