Hi,Thank you this plugin,because i use this plugin on Wordpress one Network,so the request other api's url will be change.

my step is:

  1. login : http://localhost/wordpress/wp-json/jwt-auth/v1/token
  2. get user blogs: http://localhost/wordpress/wp-json/myplugin/v1/blogs
  3. get get first blog token : http://localhost/wordpress/wp-json/token/regain/2,param 2 is the user_blogid,get the return token,change the Angularjs saved user Token
  4. get test blog posts : http://localhost/wordpress/test/wp-json/wp/v2/posts

step 2 api like:

function list_blogs($request ){
$current_user = wp_get_current_user();
$user_blogs = get_blogs_of_user( $current_user->ID );
if(count($user_blogs)==0){
return null;
}
else{
return $user_blogs;
}
} add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/blogs', array(
'methods' => 'GET',
'callback' => 'list_blogs',
) );
} );

file public/class-jwt-auth-public.php,methodadd_api_routes(),i add

register_rest_route($this->namespace, 'token/regain/(?P<blog_id>[0-9]+)', [
'methods' => 'POST',
'callback' => array($this, 'regain_token'),
]);

i add method,code is:

/**
* regain the jwt auth for multiSite
* @param WP_REST_REQUEST $request
*
* @return string token
*/
public function regain_token($request){
$secret_key = defined('JWT_AUTH_SECRET_KEY') ? JWT_AUTH_SECRET_KEY : false; /** First thing, check the secret key if not exist return a error*/
if (!$secret_key) {
return new WP_Error(
'jwt_auth_bad_config',
__('JWT is not configurated properly, please contact the admin', 'wp-api-jwt-auth'),
array(
'status' => 403,
)
);
}
/** Second thing, check the user is logined if not exist return a error*/
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
return new WP_Error( 'rest_not_logged_in', __( 'You are not currently logged in.' ), array( 'status' => 401 ) );
} $url_params = $request->get_url_params();
$param_blog_id = $url_params['blog_id'];
/** Three thing, check the $param_blog_id belong to the logined user blogs list if not exist return a error*/
$user_blogs = get_blogs_of_user($current_user->ID);
$blog_details = null;
$blog_ids = array();
$blog_is_exist = false;
foreach ($user_blogs AS $user_blog) {
if($param_blog_id == $user_blog->userblog_id){
$blog_details = $user_blog;
$blog_is_exist = true;
}
}
if(!$blog_is_exist){
return new WP_Error( 'jwt_auth_user_not_have_current_blog', __( 'current user not have this blog.' ), array( 'status' => 400 ) );
} /** Valid credentials, the user exists create the according Token */
$issuedAt = time();
$notBefore = apply_filters('jwt_auth_not_before', $issuedAt, $issuedAt);
$expire = apply_filters('jwt_auth_expire', $issuedAt + (DAY_IN_SECONDS * 7), $issuedAt); $token = array(
'iss' => $blog_details->siteurl,
'iat' => $issuedAt,
'nbf' => $notBefore,
'exp' => $expire,
'data' => array(
'user' => array(
'id' =>$current_user->ID,
),
),
); /** Let the user modify the token data before the restore. */
$token = JWT::encode(apply_filters('jwt_auth_token_before_restore', $token), $secret_key); /** The token is signed,only return token */
$data = array(
'token' => $token
); /** Let the user modify the data before send it back */
return apply_filters('jwt_auth_token_before_dispatch', $data, $current_user);
}

the code many is use the generate_token() method code,I only want the logined usre not login again,so i try add this code.

[wordpress]wp-api-jwt-auth 尝试添加运行在多站点中 need change的更多相关文章

  1. wp api jwt 403 (Forbidden) -- JWT is not configurated properly, please contact the admin

    需要在 wp-config.php 文件中配置 define('JWT_AUTH_SECRET_KEY', 'your-top-secrect-key'); 參考 403 (Forbidden) -- ...

  2. sharepoint 2013 文档库 资源管理器打开报错 在文件资源管理器中打开此位置时遇到问题,将此网站添加到受信任站点列表,然后重试。

    我们在使用sharepoint 2013的文档库或者资源库的时候,经常会需要用到使用“资源管理器”来管理文档,但是有时候,点击“使用资源管理器打开”,会提示如下错误: 在文件资源管理器中打开此位置时遇 ...

  3. 利用WordPress REST API 开发微信小程序从入门到放弃

    自从我发布并开源WordPress版微信小程序以来,很多WordPress网站的站长问有关程序开发的问题,其实在文章:<用微信小程序连接WordPress网站>讲述过一些基本的要点,不过仍 ...

  4. WordPress REST API 内容注入漏洞

    1 WordPress REST API 内容注入漏洞 1.1 摘要 1.1.1 漏洞介绍 WordPress是一个以PHP和MySQL为平台的自由开源的博客软件和内容管理系统.在4.7.0版本后,R ...

  5. WordPress Option API(数据库储存 API)

    WordPress Option API 是提供给开发者的数据库存储机制,通过调用函数,可以快速.安全的把数据存储到数据库里(都在 wp_options 表). 每个设置的模式是 key – valu ...

  6. WordPress版微信小程序开发系列(一):WordPress REST API

    自动我发布开源程序WordPress版微信小程序以来,很多WordPress站长在搭建微信小程序的过程中会碰到各种问题来咨询我,有些问题其实很简单,只要仔细看看我写的文章,就可以自己解决.不过这些文章 ...

  7. .net core 3 web api jwt 一直 401

    最近在给客户开发 Azure DevOps Exension, 该扩展中某个功能需要调用使用 .NET Core 3 写的 Web Api. 在拜读了 Authenticating requests ...

  8. 30.怎样在Swift中添加运行时属性?

    和OC一样,Swift中也可以添加运行时属性.下面将提供一个完整的例子,演示如何给按钮点击事件添加运行时属性. 1.示例 import UIKit var s_GofButtonTouchDownKe ...

  9. Visual Studio 2008中添加运行按钮 转载

    在Visual Studio 2008中添加运行按钮 默认情况下,VS2008中的工具栏上没有运行按钮,只有调试(Debug)按钮,可按照以下方法添加 1.点击菜单Tools(工具)->Cust ...

随机推荐

  1. VC++ 中滑动条(slider控件)使用 [转+补充]

    滑动控件slider是Windows中最常用的控件之一.一般而言它是由一个滑动条,一个滑块和可选的刻度组成,用户可以通过移动滑块在相应的控件中显示对应的值.通常,在滑动控件附近一定有标签控件或编辑框控 ...

  2. MySQL 日期时间

    NOW()函数以`'YYYY-MM-DD HH:MM:SS'返回当前的日期时间,可以直接存到DATETIME字段中.CURDATE()以’YYYY-MM-DD’的格式返回今天的日期,可以直接存到DAT ...

  3. php 将字符串中的连续多个空格转换为一个空格

    转载自:http://www.phpernote.com/php-function/633.html /** * 多个连续空格只保留一个 * * @param string $string 待转换的字 ...

  4. MHA高可用+VIP 集群故障转移(已测试成功)

       服务器部署说明192.168.158.201 mha管理,mysql主服192.168.158.202 mha节点,mysql从服192.168.158.203 mha节点,mysql从服Man ...

  5. 电子图书的编目和OPAC揭示

    摘  要  步入数字图书馆时代,电子资源日益增多,尤其在高校图书馆更加迅猛,怎样将图书馆的电子资源尽可能地整合在OPAC系统的检索功能里,以便更加简便.直观地利用,电子图书的编目已成为重要研究课题.本 ...

  6. web及移动应用测试知识总结

    发现自己对测试知识的掌握不够系统,在这里整理一下好了. 1. 通用测试点 功能测试 正向:输入一个有效的输入并且期望软件能够完成一些根据说明书规定的行为 逆向:输入一个无效的输入并且期望软件给出合理的 ...

  7. 【WebForm】Js调用后台C#方法

    因业务的需要,有这么个需求,需要前台的JS传参调用C#后台的方法.现在有这么个方法可以解决,整理如下. 首先,先说一下基本实现,前台用Jquery的ajax将其中的URL后加方法,然后在Data中传递 ...

  8. Ruby Scripting - Array

    A literal array is created by putting squarebrackets around a comma-separated list of elements eg: # ...

  9. 今天弱爆了,svn创建项目

    今天弱爆了 1.再svnRoot下新建你要建的项目名如:hqdj  文件夹,然后选中它点击右键选中create repository here... ,选择文件系统类型 2.进入conf文件夹进行配置 ...

  10. jQuery 效果 - animate() 方法

    http://www.w3school.com.cn/jquery/effect_animate.asp 实例 改变 "div" 元素的高度: $(".btn1" ...