Support the ongoing development of Laravel.io →
Authentication Requests Packages

I use tymon/jwt-auth package for authentication in laravel 5.2 framework But when i run my login api i got error :

 ErrorException in /var/www/vendor/composer/ClassLoader.php line 317:
 Uninitialized string offset: 0

Login code :

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

And in auth.php in config folder i have :

 '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',
    // ],
  ],

Notice : when i change AUTH_GUARD to api in defaults section i got error :

        call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\TokenGuard' does not have a method 'once'
Last updated 3 years ago.
0

please help me friends

0

Sign in to participate in this thread!

Eventy

Your banner here too?

amiralii amiralii Joined 15 Aug 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.