Slim 3如何在本地存储上保存JWT令牌并在我的路由中使用它进行身份验证

Slim 3如何在本地存储上保存JWT令牌并在我的路由中使用它进行身份验证,jwt,slim,slim-3,Jwt,Slim,Slim 3,我想为slim应用程序实现jwt身份验证,当我使用Postman时,我遵循了tuupora的PRS7 jwt身份验证中间件及其工作原理,因为当我请求“/auth/ibice”路由时,可以选择使用标题作为“Authorization:Bearer tokenString”,如下所示 我正在使用当我请求此路由时返回的令牌字符串“/authtoken”,如下面所示 { "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cuY

我想为slim应用程序实现jwt身份验证,当我使用Postman时,我遵循了tuupora的PRS7 jwt身份验证中间件及其工作原理,因为当我请求“/auth/ibice”路由时,可以选择使用标题作为“Authorization:Bearer tokenString”,如下所示

我正在使用当我请求此路由时返回的令牌字符串“/authtoken”,如下面所示

{
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cuYXNpZC5ydyIsImlhdCI6MTQ4Njk5MjcyNCwiZXhwIjoxNDg4Mjg4NzI0LCJjb250ZXh0Ijp7InVzZXIiOnsicGhvbmVubyI6IjA3ODQyMjY4OTUiLCJ1c2VyX2lkIjoiMSJ9fX0.1kFu4A16xxJriaRA9CccIJ3M9Bup06buK2LAh13Lzy4",
  "user_id": "1"
}
这是my middleware.php,用于保护“/auth/”的所有路由


请帮帮我,我不知道怎么做

我以前从未使用过Slim,但也许您可以使用少量Javascript访问localstorage bcz当localstorage在浏览器(客户端)中时,您无法使用php访问本地存储(php在服务器端工作)这里有一些步骤,您可以先通过点击此/authtoken端点$app->get('/authtoken')使用php获取Auth令牌然后,您需要将返回的json解码到php数组中,如果您的php数组包含令牌$arr,那么您可以使用少量javascript将该令牌保存在localstorage中,如下所示
localstorage.setItem('token','')然后,无论何时您想要读取它,您都可以使用javascript从localstorage读取它

<?php
$token = "<script>document.write(localStorage.getItem('token'));</script>"; ?>

thx人!!我将尝试一下,但剩下的唯一问题是发送包含类似头的授权的请求:Bearer
$app->group('/auth',function(){

 $this->get('/admin','App\Controllers\apiController:login')->setName('admin');   

//fetch ibice 
$this->get('/ibice','App\Controllers\apiController:ibice')->setName('Ibice');

//fetch ibice by id
$this->get('/igice/{id}', 'App\Controllers\apiController:igice')->setName('igiceId'); 

//search ibice
$this->get('/igice/search/[{query}]', 'App\Controllers\apiController:igice_search')->setName('Igice Search');

//imitwe igize igice
$this->get('/igice/{id}/imitwe','App\Controllers\apiController:imitwe')->setName('Imitwe');

//ingingo ziherereye mumutwe runaka
$this->get('/umutwe/{id}/ingingo', 'App\Controllers\apiController:ingingoBundle')->setName('Ingingo.bundle');

//ingingo ziri mucyiciro runaka
$this->get('/ingingo/icyiciro/{id}', 'App\Controllers\apiController:allstuff')->setName('Icyiciro');

//kuzana ikibazo kimwe kiri mungingo runaka
$this->get('/ingingo/{ingingoid}/question/{id}', 'App\Controllers\apiController:question')->setName('One_Exercise');

//kuzana ibibazo byose biri mungingo 
$this->get('/ingingo/{ingingoid}/questions', 'App\Controllers\apiController:questions')->setName('One_Exercise');

 //check if the answer is True or False
$this->get('/question/{id}/check/[{query}]','App\Controllers\apiController:checkQuestions')->setName('Check_Questions');

//get questions ids from ingingo
$this->get('/question/{ingingoid}','App\Controllers\apiController:questionsIDs')->setName('Check_Questions');
});
<?php
$token = "<script>document.write(localStorage.getItem('token'));</script>"; ?>