添加认证中间件后Laravel路由不调用控制器的函数



添加auth:api中间件后,我的路由突然停止调用它应该调用的函数。当我删除中间件时,它又开始工作了。

api.php路线

Route::middleware('auth:api')->get('/addloancontroller', [AppHttpControllersAddLoan::class, 'store'])
->name('addLoan');

AddLoan.php控制器
<?php
namespace AppHttpControllers;
use AppModelsEarningsRecord;
use AppModelsLender;
use AppModelsLoanLenders;
use AppModelsLoans;
use AppModelsLogin;
use AppProvidersRouteServiceProvider;
use IlluminateHttpRequest;
use Auth;
use IlluminateSupportFacadesDB;
class AddLoan extends Controller
{
public function store(Request $request) {
$request->validate([
'amount' => 'required',
'startDate' => 'required',
'endDate' => 'required',
'dateLoanSigned' => 'required',
'interestRate' => 'required',
'interestPaymentPeriod' => 'required',
'interest_only_period' => 'required',
'active' => 'required',
'loanType' => 'required',
'fName' => 'required',
'lName' => 'required',
'earnings' => 'required',
'irdNum' => 'required',
'bankAccNum' => 'required',
'address' => 'required',
'phone' => 'required',
]);
$company_id = $request->user();
dd($company_id);
$loan = Loans::create([
'companies_id' => $company_id,
'amount' => $request->amount,
'startDate' => $request->startDate,
'endDate' => $request->endDate,
'dateCreated' => $request->dateLoanSigned,
'interestRate' => $request->interestRate,
'interestPaymentPeriod' => $request->interestPaymentPeriod,
'interestOnlyPeriod' => $request->interestOnlyPeriod,
'active' => $request->active,
'refinanced' => 0,
'loanType' => $request->loanType
]);
$loan->save();

$lender = Lender::create([
'fName' => $request->fName,
'lName' => $request->lName,
'annualEarnings' => $request->earnings,
'irdNum' => $request->irdNum,
'bankAccNum' => $request-> bankAccNum,
'address' => $request-> address,
'phone' => $request->phone,
'loginID' => null
]);
$lender->save();
$taxAmount = DB::table('tax_rates')
->where('minAmount', '<', $request->earnings)
->where('maxAmount', '>', $request->earnings);
$loanLender = LoanLenders::create([
'loansID' => $loan->id,
'lenderID' => $lender->id,
'companyID' => $company_id,
'taxAmount' => $taxAmount->id
]);
$loanLender->save();
$earningsReport = EarningsRecord::create([
'lenderID' => $lender->id,
'annualEarnings' => $lender->annualEarnings,
'dateRecorded' => date('d-m-Y')
]);
$earningsReport->save();
return redirect(RouteServiceProvider::HOME);
}
}

该路由以动作的形式被调用:

<form method="GET" action="{{ route('addLoan') }}">

我所有的验证只是寻找'required'的原因是因为我想确保它不是验证失败,只是不产生错误。

当我提交表单时会发生什么?

当我提交表单并且路由附加了中间件时,它只是将我重定向回主屏幕。

我已经运行了:

  • php artisan cache:clear
  • composer dump-autoload

身份验证标记

正如laravel文档中所解释的,我已经将api_token列添加到User的迁移文件中(在我的例子中,我使用Login模型进行身份验证)。下面是我在迁移文件中添加该列的行:

$table->string('api_token', 80)->unique()->nullable()->default(null);

这是文档中的一行:

Schema::table('users', function ($table) {
$table->string('api_token', 80)->after('password')
->unique()
->nullable()
->default(null);
});

当我创建模型对象时,我通过这样做来创建经过身份验证的令牌:

'api_token' => Str::random(60)

当我检查数据库时,我看到api_token已成功添加,因此我不确定问题可能是什么。

(我使用的文档链接在这里)

您已经在您的api路由文件中定义了一条路由,并使用auth:api中间件(通常是Passport)保护它,但是您正在从HTMLform调用路由,这不是您通常如何消费api

我假设你只是想保护在你的应用程序中通过网页访问的路由,因此我建议你研究一个基于web的认证机制,如Fortify,或者如果你不想滚动自己的视图,要么JetstreamBreeze

令牌与api一起使用,您目前实现的工作流(web表单)是web工作流,因此不建议使用api tokens

只要user被认证,Authfacade在控制器中可用,并且同时适用于webapi守卫。

如果你安装BreezeJetstream,如果你刚开始,我会推荐Fortify,你将能够使用auth中间件来保护你的路由,需要users来验证自己(登录),以便访问受保护的路由。这就意味着,当一个user已经通过身份验证,并且你在控制器中处理一个请求时,Auth::id()将返回经过身份验证的userid

我建议在控制器中重构你的代码,这是我第一次为你的方法创建FormRequest,并将你的验证代码和规则移动到那里。然后在FormRequest方法authorize我写这样的代码:

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth('auth:api')->check(); // Method check return false/true.
}
public function rules()
{
return [
'amount' => ['required'],
'startDate' => ['required'],
'endDate' => ['required'],
'dateLoanSigned' => ['required'],
'interestRate' => ['required'],
'interestPaymentPeriod' => ['required'],
'interest_only_period' => ['required'],
'active' => ['required'],
'loanType' => ['required'],
'fName' => ['required'],
'lName' => ['required'],
'earnings' => ['required'],
'irdNum' => ['required'],
'bankAccNum' => ['required'],
'address' => ['required'],
'phone' => ['required']
];
}

另外,我建议将HTTP方法GET更改为POST

相关内容

  • 没有找到相关文章