Laravel Passport使用自己的API失败
我正在和Vue建一个水疗中心。我的前端和后端(Laravel)在同一个代码库中。我想通过Laravel Passport中间件CreateFreshApitonk接近我的API(在我的后端)。我正在通过web.php接近AuthController中的登录方法 我的问题: 一旦我通过登录方法成功登录,我希望Passport此时会创建laravel_令牌cookie。事实并非如此。cookie是在页面刷新后创建的。但正如我所说,我正在建一个水疗中心,这就是为什么我不想刷新页面 我想要什么: 我想通过我的登录方法登录,然后使用Passport CreateFreshApitonk中间件。之后,我想使用(刚刚在中间件中创建的)laravel_令牌cookie,这样我就可以在SPA的登录部分正确、安全地与自己的API对话 更多信息: Kernel.php AuthController.php Login.vue 出了什么问题?此: CreateFreshApiToken.phpLaravel Passport使用自己的API失败,laravel,api,vue.js,single-page-application,laravel-passport,Laravel,Api,Vue.js,Single Page Application,Laravel Passport,我正在和Vue建一个水疗中心。我的前端和后端(Laravel)在同一个代码库中。我想通过Laravel Passport中间件CreateFreshApitonk接近我的API(在我的后端)。我正在通过web.php接近AuthController中的登录方法 我的问题: 一旦我通过登录方法成功登录,我希望Passport此时会创建laravel_令牌cookie。事实并非如此。cookie是在页面刷新后创建的。但正如我所说,我正在建一个水疗中心,这就是为什么我不想刷新页面 我想要什么: 我想通
我想这是可能的,我想实现的权利?如果是,怎么做?我也有同样的问题,决定坚持客户的秘密方式。我想这与您现在的情况无关,但我找到了两种不刷新即可接收laravel令牌的方法: 1) 使用axios或$http发送虚拟get请求,无论您使用什么-令牌将附加到响应
2) 在CreateFreshApiToken.php中更改requestShouldReceiveFreshToken方法-replace
return$request->isMethod('GET')&&&$request->user($this->guard)代码>返回($request->isMethod('GET')| |$request->isMethod('POST'))&&$request->user($this->guard)代码>我也有同样的问题,决定坚持客户的秘密方式。我想这与您现在的情况无关,但我找到了两种不刷新即可接收laravel令牌的方法:
1) 使用axios或$http发送虚拟get请求,无论您使用什么-令牌将附加到响应
2) 在CreateFreshApiToken.php中更改requestShouldReceiveFreshToken方法-replacereturn$request->isMethod('GET')&&&$request->user($this->guard)代码>返回($request->isMethod('GET')| |$request->isMethod('POST'))&&$request->user($this->guard)代码>有人吗?我真的卡住了…有人能帮我吗?我什么都试过了。。。我也在Laracasts上问过,但还是没有答案……有人吗?我真的卡住了…有人能帮我吗?我什么都试过了。。。我也在Laracasts上问过,但仍然没有答案…你能解释一下如何实现和使用代码吗?你能解释一下如何实现和使用代码吗。
// Code...
protected $middlewareGroups = [
'web' => [
// other middlewares...
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],
];
// Code...
// Code...
public function login()
{
if (Auth::attempt(['email' => Input::get('email'), 'password' => Input::get('password')], true)) {
return response()->json([
'user' => Auth::user(),
'authenticated' => auth()->check(),
]);
}
return response()->json(['authenticated' => false], 401);
}
// Code...
// Code...
methods: {
login: function (event) {
event.preventDefault();
this.$http.post(BASE_URL + '/login', {
email: this.email,
password: this.password,
})
.then(function (response) {
localStorage.user_id = response.body.user.id;
router.push({
name: 'home'
});
});
},
},
// Code...
// Code...
public function handle($request, Closure $next, $guard = null)
{
$this->guard = $guard;
$response = $next($request);
// I'm signed in at this point
if ($this->shouldReceiveFreshToken($request, $response)) { // returns false unless you refresh the page. That's why it won't create the laravel_token cookie
$response->withCookie($this->cookieFactory->make(
$request->user($this->guard)->getKey(), $request->session()->token()
));
}
return $response;
}
protected function shouldReceiveFreshToken($request, $response)
{
// both methods below return false
return $this->requestShouldReceiveFreshToken($request) &&
$this->responseShouldReceiveFreshToken($response);
}
protected function requestShouldReceiveFreshToken($request)
{
// $request->isMethod('GET') - returns false because it's a POST request
// $request->user($this->guard) - returns true as expected
return $request->isMethod('GET') && $request->user($this->guard);
}
protected function responseShouldReceiveFreshToken($response)
{
// $response instanceof Response - returns false
// ! $this->alreadyContainsToken($response) - returns false as expected
return $response instanceof Response &&
! $this->alreadyContainsToken($response);
}
// Code...
function consumeOwnApi($uri, $method = 'GET', $parameters = array())
{
$req = \Illuminate\Http\Request::create($uri, $method, $parameters, $_COOKIE);
$req->headers->set('X-CSRF-TOKEN', app('request')->session()->token());
return app()->handle($req)->getData();
}