我想创建具有两部分(用户和 pemandu_bas)的多重身份验证。但是当我向邮递员测试 POST 'pemandu_bas' 令牌时,我的 'pemandu_bas' 不能用作 api。它一直显示这样的错误。这是我的 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');
}
}