我是Symfony的新手,我正在尝试将JWT对象传递给Cookie方法,一切看起来都不错,除非我不认为它正在登录我。我已经回复了一条消息,看看我是否我登录然后它应该回显登录。我能够重定向到索引页面,但消息没有显示我已登录。
我的doLogin.php看起来像吼叫
<?php
require_once __DIR__.'/../inc/bootstrap.php';
$user = findUserByEmail(request()->get('email'));
if(empty($user)){
redirect('login.php');
}
if(!password_verify( request()->get('password'), $user['password'])){
redirect('login.php');
}
$expTime = time() + 3600;
$jwt = \Firebase\JWT\JWT::encode( //header
[
'iss' => request()->getBaseUrl(), // who issued this claim, here will localhost
'sub' => "{$user['id']}", //who is the subject
'exp' => $expTime,
'iat' => time(), //issued at
'nbf' => time(), // not before
'is_admin' => $user['role_id'] == 1
],
getenv("SECRET_KEY"), //payload
'HS256');// signature
$accessToken = new Symfony\Component\HttpFoundation\Cookie('access_token', $jwt, $expTime, '../', getenv("COOKIE_DOMAIN"));
//var_dump($accessToken);
redirect('../index.php', ['cookies' => [$accessToken]]);
我的重定向功能如下所示
function redirect($path, $extra = []){
$response= \Symfony\Component\HttpFoundation\Response::Create(null, \Symfony\Component\HttpFoundation\Response::HTTP_FOUND, ['Location' => $path]);
if(key_exists('cookies', $extra)){
foreach($extra['cookies'] as $cookie){
$response->headers->setCookie($cookie);
}
}
$response->send();
exit;
}
下面的文件是我的index.php页面。
<?php
require_once __DIR__ . '/inc/head.php';
require_once __DIR__ . '/inc/nav.php';
require_once __DIR__ . '/inc/bootstrap.php';
//echo __DIR__ .'/inc';
?>
<div class="container">
<div class="well">
<h2>Book Voting System</h2>
<?php
if(request()->cookies->has('access_token')){
echo "logged in";
}
?>
<p>Welcome to the book voting system. Use this system to submit books you like and see if others like them as well
by letting them upvote it.</p>
</div>
</div>
<?php
require_once __DIR__ . '/inc/footer.php';
有人能告诉我哪里出错了吗?