Мульти аутентичный API токен laravel 5.4 - PullRequest
0 голосов
/ 02 мая 2018

Я хочу создать Multi auth, состоящий из двух частей (user и pemandu_bas). но когда я тестировал POST токен pemandu_bas для почтальона, мой pemandu_bas не работает как API. Это показывает ошибку, как это this. Это мой config / auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

    'pemandu_bas' => [
        'driver' => 'token',
        'provider' => 'pemandu_bas',
    ],
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Penjaga::class,
    ],

    'pemandu_bas' => [
        'driver' => 'eloquent',
        'model' =>  App\Driver::class,
    ],
],

это мой контроллер 'pemandu_bas'

<?php

namespace App\Http\Controllers\api;

use Validator;
use \App\Driver;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ApiPemanduBasController extends Controller{

public function loginPemandu(Request $request)
{

    $this->validate($request, [
        'username' => 'required',
        'password' => 'required'
    ]);


    if (Auth::attempt(['username' => $request->username, 'password' => $request->password])) {
        // Authentication passed
       $pemandu_bas = Auth::guard('pemandu_bas')->user();
       $pemandu_bas->api_token = str_random(60);
       $pemandu_bas->save();
    }else
    {
// Authentication failed
        return response()->json([
        'error' => true,
        'message' => 'Login failed wrong user credentials',
    ], 400);
    }
    // Return token
    return response()->json([
        'error' => false,
        'message' => 'login successful',
        'token' => $pemandu_bas->api_token,
    ], 200);

}

}

это мой api.php

Route::post('loginPemandu', 'Api\ApiPemanduBasController@loginPemandu');

а это мой Driver.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\Driver as Authenticatable;

class Driver extends Authenticatable
{
use Notifiable;

protected $guard = 'pemandu_bas';

protected $fillable 
 ['pemandu_nama','pemandu_tel','username','password',
 'pemandu_noPlat','sekolah_id'];


/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password',
    'remember_token',
    'api_token',
    'created_at',
    'updated_at'
];

public function School(){
  return $this->hasMany('App\School');
}


 }
...