Laravel Passport:发送令牌请求时的附加模型条件
我正在使用为移动应用程序构建一个API。当用户尝试登录到应用程序时,客户端发送访问令牌的请求 用户可能没有使用发送到其电子邮件的链接验证其帐户。我希望向查询中添加一个附加条件,并相应地提供错误响应。目前,由于Passport管理令牌部分,我无法直接执行Laravel Passport:发送令牌请求时的附加模型条件,laravel,laravel-5,oauth-2.0,laravel-passport,Laravel,Laravel 5,Oauth 2.0,Laravel Passport,我正在使用为移动应用程序构建一个API。当用户尝试登录到应用程序时,客户端发送访问令牌的请求 用户可能没有使用发送到其电子邮件的链接验证其帐户。我希望向查询中添加一个附加条件,并相应地提供错误响应。目前,由于Passport管理令牌部分,我无法直接执行 如何解决这个问题?如果用户帐户存在任何问题,我如何深入令牌请求并发送自定义响应?然后继续发送令牌。在将输入数据发送到身份验证服务提供商之前,请添加一个中间件,并检查是否有来自您数据库的电子邮件。如果电子邮件已被验证,则通过将其发送给auth se
如何解决这个问题?如果用户帐户存在任何问题,我如何深入令牌请求并发送自定义响应?然后继续发送令牌。在将输入数据发送到身份验证服务提供商之前,请添加一个中间件,并检查是否有来自您数据库的电子邮件。如果电子邮件已被验证,则通过将其发送给auth service provider继续请求,否则将返回错误响应,其中包含您想要的任何自定义消息 我看到人们仍然在寻找这个答案,并出现在这一页上。定义您自己的oauth/token路由可能是一种方法,但如果您使用中间件在将数据发送到控制器之前进行更改,则更符合逻辑 制作中间件:
php工匠制作:中间件MyMiddleware
在内核中注册中间件,并使其优先于身份验证中间件
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,//auth middleare
'your_middleware_identifier'=>\App\Http\Middleware\MyMiddleware::class,
];
/**
* The application's middleware priority array.
*
* These middlewares will be executed in way they are listed.
*
* @var array
*/
protected $middlewarePriority = [
\App\Http\Middleware\MyMiddleware::class,
\Illuminate\Auth\Middleware\Authenticate::class,
];
最后,您可以在AuthServiceProvider.php文件中使用您的中间件,其中您的passport路由是:
in boot()函数
我希望它有助于在将输入数据发送到身份验证服务提供商之前添加一个中间件,并检查数据库中的电子邮件。如果电子邮件已被验证,则通过将其发送给auth service provider继续请求,否则将返回错误响应,其中包含您想要的任何自定义消息 我看到人们仍然在寻找这个答案,并出现在这一页上。定义您自己的oauth/token路由可能是一种方法,但如果您使用中间件在将数据发送到控制器之前进行更改,则更符合逻辑 制作中间件:
php工匠制作:中间件MyMiddleware
在内核中注册中间件,并使其优先于身份验证中间件
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,//auth middleare
'your_middleware_identifier'=>\App\Http\Middleware\MyMiddleware::class,
];
/**
* The application's middleware priority array.
*
* These middlewares will be executed in way they are listed.
*
* @var array
*/
protected $middlewarePriority = [
\App\Http\Middleware\MyMiddleware::class,
\Illuminate\Auth\Middleware\Authenticate::class,
];
最后,您可以在AuthServiceProvider.php文件中使用您的中间件,其中您的passport路由是:
in boot()函数
我希望这有助于拉威尔撰稿人的回答: 制作自己的oauth/token路由,并将其放入/routes内的oauth.php文件中:
Route::post('/oauth/token', [
'uses' => 'Auth\CustomAccessTokenController@issueUserToken'
]);
创建一个CustomAccessTokenController.php
<?php
namespace App\Http\Controllers\Auth;
use Psr\Http\Message\ServerRequestInterface;
use Laravel\Passport\Http\Controllers\AccessTokenController;
class CustomAccessTokenController extends AccessTokenController
{
/**
* Hooks in before the AccessTokenController issues a token
*
*
* @param ServerRequestInterface $request
* @return mixed
*/
public function issueUserToken(ServerRequestInterface $request)
{
$httpRequest = request();
// 1.
if ($httpRequest->grant_type == 'password') {
// 2.
$user = \App\User::where('email', $httpRequest->username)->first();
// Perform your validation here
// If the validation is successfull:
return $this->issueToken($request);
}
}
}
来自Laravel撰稿人的回答:
制作自己的oauth/token路由,并将其放入/routes内的oauth.php文件中:
Route::post('/oauth/token', [
'uses' => 'Auth\CustomAccessTokenController@issueUserToken'
]);
创建一个CustomAccessTokenController.php
<?php
namespace App\Http\Controllers\Auth;
use Psr\Http\Message\ServerRequestInterface;
use Laravel\Passport\Http\Controllers\AccessTokenController;
class CustomAccessTokenController extends AccessTokenController
{
/**
* Hooks in before the AccessTokenController issues a token
*
*
* @param ServerRequestInterface $request
* @return mixed
*/
public function issueUserToken(ServerRequestInterface $request)
{
$httpRequest = request();
// 1.
if ($httpRequest->grant_type == 'password') {
// 2.
$user = \App\User::where('email', $httpRequest->username)->first();
// Perform your validation here
// If the validation is successfull:
return $this->issueToken($request);
}
}
}
您可以在您的用户模型中定义findForPassport
方法,如文档所述:
您可以在您的用户模型中定义findForPassport
方法,如文档所述:
如果我的回答对您有帮助,请将其标记为已接受以关闭此线程。谢谢。如果我的回答对您有帮助,请将其标记为已接受以关闭此线程。谢谢。我不知道你的代码是什么。Laravel passport已随时间发生更改,您现在可以重写此方法-这将返回错误“调用文件中null上的成员函数respondToAccessTokenRequest()”。我不确定您的代码是什么。Laravel passport已随时间发生更改,您现在可以重写此方法-这将返回错误“调用文件中null上的成员函数respondToAccessTokenRequest()”