我正在用Vue建立一个SPA。我的前端和后端(Laravel)属于相同的代码库。我想通过Laravel Passport Middleware CreateFreshApiToken 来处理我的API(即我的后端)。我正在通过 web.php 在我的 AuthController 中接近我的登录方法。
我的问题: 一旦我通过我的登录方法成功登录,我希望此时Passport创建了 laravel_token cookie 。不是这种情况。 cookie是在页面刷新后创建的。但正如我所说,我正在建立一个SPA,这就是为什么我不想让页面刷新。
我想要的是什么: 我想通过登录方法登录,然后使用Passport CreateFreshApiToken 中间件。之后我想使用(刚刚在中间件中创建) laravel_token cookie ,这样我就可以在SPA的登录部分正确安全地与我自己的API通信。
更多信息:
Kernel.php
// Code...
protected $middlewareGroups = [
'web' => [
// other middlewares...
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],
];
// Code...
AuthController.php
// 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...
Login.vue
// 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...
出了什么问题?这样:
CreateFreshApiToken.php
// 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...
我认为我想要实现的目标是正确的吗?如果是,怎么样?
答案 0 :(得分:0)
我有同样的问题,决定坚持使用client_secret方式。我想这对你现在没有意义,但是我找到了两种方法来接收laravel令牌而不刷新:
1)使用axios或$ http发送虚拟get请求,无论你使用什么 - 令牌都会附加到响应中;
2)在CreateFreshApiToken.php中更改requestShouldReceiveFreshToken方法 - 将return $request->isMethod('GET') && $request->user($this->guard);
替换为return ($request->isMethod('GET') || $request->isMethod('POST')) && $request->user($this->guard);
答案 1 :(得分:0)
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();
}