我在laravel 5.2框架中使用tymon/jwt-auth
包进行身份验证
但是当我运行我的登录API时,我收到了错误:
ErrorException in /var/www/vendor/composer/ClassLoader.php line 317:
Uninitialized string offset: 0
登录代码:
try
{
$this->validate($request, [
'email' => 'required|email|max:255', 'password' => 'required',
]);
}
catch (HttpResponseException $e)
{
return response()->json([
'error' => [
'message' => 'Invalid auth',
'status_code' => IlluminateResponse::HTTP_BAD_REQUEST
]],
IlluminateResponse::HTTP_BAD_REQUEST,
$headers = []
);
}
try
{
if ( ! $token = JWTAuth::attempt(['email' => $request->get('email'), 'password' => $request->get('password'), 'status' => 'activate']))
{
return response()->json(['error' => 'invalid_credentials'], 401);
}
}
catch (JWTException $e)
{
return response()->json(['error' => 'could_not_create_token'], 500);
}
$user = User::where('email',$request->get('email'))->where('status','=','activate')->get(['_id','user_type']);
$this->operator->sign_in($user[0]['_id']);
$headers = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE, PATCH',
'Access-Control-Allow-Headers'=> 'Content-Type, Origin,Accept,X-Requested-With, Authorization',
'Access-Control-Expose-Headers' => 'Content-Type, Origin,Accept,X-Requested-With, Authorization',
'Access-Control-Allow-Credentials' => true,
'Authorization'=> 'Bearer' . $token
];
$this->activities->post('login');
return response()->json(['_id'=> $user[0]['_id']], 200, $headers);
在config文件夹中的auth.php中我有:
'defaults' => [
'guard' => env('AUTH_GUARD', 'web'),
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => app('config')->get('jwt.user'),
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
注意:当我在默认部分中将AUTH_GUARD
更改为api
时,我收到错误:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\TokenGuard' does not have a method 'once'