【发布时间】:2019-08-23 03:46:51
【问题描述】:
我想通过我的 Laravel Rest API 将我的身份验证服务(Passport)中的仅 http cookie 发送到位于世界任何地方的消费前端。但是,没有 cookie 被发送到前端。
我尝试使用 withCookie() 和 cookie() 设置带有 json 响应的 cookie。那没有用。
这位于 kernel.php 中,Laravel Passport 文档(Consuming Your API with Javascript 部分),但是,它不会随响应一起发送。
'web' => [
// Other middleware...
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],
这不会发送 cookie
public function login(Request $request)
{
$request->validate([
'email' => 'required|string|email',
'password' => 'required|string',
'remember_me' => 'boolean'
]);
$credentials = request(['email', 'password']);
if(!Auth::attempt($credentials))
return response()->json([
'message' => 'Unauthorized'
], 401);
$user = $request->user();
$tokenResult = $user->createToken('Personal Access Token');
$token = $tokenResult->token;
if ($request->remember_me)
$token->expires_at = Carbon::now()->addWeeks(1);
$token->save();
return response()->json([
'access_token' => $tokenResult->accessToken,
'token_type' => 'Bearer',
'expires_at' => Carbon::parse(
$tokenResult->token->expires_at
)->toDateTimeString()
]);
}
我希望它发送带有令牌的 http cookie,但是它不起作用。
【问题讨论】:
-
您不能为其他域设置 cookie。 Passport 使用 API 令牌,而不是 cookie。
-
是的,但我想将其存储在仅 http 的 cookie 中。我不知道还有其他安全方法吗?
-
客户端使用您的 API 将负责以某种方式存储令牌。
标签: php laravel rest api cookies