我正在尝试获取 laravel 的多重身份验证设置。我已经从我的consumers
表工作了一个消费者登录。businesses
但现在我需要从表中设置业务登录。(目前业务登录正在从consumers
表中提取数据。
我试图让它返回 JSON (JWT) 与返回登录刀片,我发现大多数在线信息都已完成。
企业迁移
class CreateBusinessTable extends Migration
{
public function up()
{
Schema::create('businesses', function (Blueprint $table) {
$table->increments('bus_id', 11);
$table->string('bus_name', 50);
$table->string('bus_address', 50);
$table->string('bus_city', 50);
$table->string('bus_prov', 50);
$table->string('bus_postal', 50);
$table->string('bus_phone', 50);
$table->string('email', 50);
$table->string('password', 20);
$table->double('max_donatable', 10, 2);
$table->integer('cashback_percent');
$table->binary('bus_cover_photo');
$table->binary('bus_profile_pic');
$table->timestamps();
$table->rememberToken();
$table->engine = 'InnoDB';
});
}
商业模式
namespace App;
use Illuminate\Contracts\Auth\CanResetPassword;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Business extends Authenticatable implements JWTSubject//Model
{
use Notifiable;
protected $table = 'businesses';
protected $primaryKey = 'bus_id';
protected $gaurd = 'business';
protected $fillable = [ 'bus_id', 'bus_name', 'bus_address', 'bus_city', 'bus_prov', 'bus_postal', 'bus_phone', 'email', 'password', 'cashback_percent', 'bus_cover_photo', 'bus_profile_pic'];
protected $hidden = [
'password', 'remember_token',
];
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
业务负责人
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
class businessAuthController extends Controller
{
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = /*auth()*/Auth::guard('business')->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
}
Handler.php 未认证函数
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
$guard = array_get($exception->guards(), 0);
switch ($guard) {
case 'business':
$login = 'business.login';
break;
default:
$login = 'login';
break;
}
return redirect()->guest(route($login));
}
内核.php
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'business' => \App\Http\Middleware\Business::class,
];
}
提供者
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Consumer::class,
],
'businesses' => [
'driver' => 'eloquent',
'model' => App\Business::class,
],
],
高德
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
'business' => [
'driver' => 'session',
'provider' => 'businesses',
],
'business-api' => [
'driver' => 'jwt',
'provider' => 'businesses',
],
],