在我的 Laravel 和 Reactjs 应用程序中,我想对后端使用 Laravel 基本身份验证,对前端使用 Jwt 身份验证。例如,在我的应用程序中,Admin 表是“User”,前端用户表是“boders”。因此,在这种情况下,当我以前端用户身份登录/任何请求时,它会给我“用户”表数据(“用户”表用于管理员);应该是“Boders”表数据。
Api.php
final String data = ...;
final List<dynamic> json = jsonDecode(data);
// Create a copy of json
final List<dynamic> json2 = List.from(json);
// For each {"group": ..., "person": ...}
json2.forEach((child) {
// Create a new key joining each person's name and age
child["newList"] = child["person"]?.map((child) => child["name"] + "_" + child["age"]).toList();
});
config/auth.php
Route::group(['middleware' => ['jwt.verify']], function() {
Route::get('jwt_user', 'UserController@getAuthenticatedUser');
}
Route::middleware('auth:api')->get('/basic-auth-user', function (Request $request) {
return $request->user();
});
Boder.php(模型)
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'boders',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'boders' => [
'driver' => 'eloquent',
'model' => App\Boder::class,
],
],
用户模型
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\Boder as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class Boder extends Authenticatable implements JWTSubject {
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function getJWTIdentifier()
return $this->getKey();
}
public function getJWTCustomClaims() {
return [];
}
}
UserController.php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable {
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
用户控制器返回用于管理员的用户表数据。