[wordpress]wp-api-jwt-auth 尝试添加运行在多站点中 need change
2024-09-10 07:12:59
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:
- login :
http://localhost/wordpress/wp-json/jwt-auth/v1/token
- get user blogs:
http://localhost/wordpress/wp-json/myplugin/v1/blogs
- get get first blog token :
http://localhost/wordpress/wp-json/token/regain/2
,param2
is the user_blogid,get the return token,change the Angularjs saved user Token - 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.
最新文章
- ubuntu16.04部署RED5流媒体服务器
- Cach&#233;数据库学习笔记(5)
- 基于bootstrap的后台二级垂直菜单[转]
- 6种编写HTML和CSS的最有效的方法
- ListView与DataTable传递数据
- hive UDF添加方式
- 安卓的UI界面开发(初学者推荐)
- 移动端JS事件、移动端框架
- struts校验
- DataFrame对行列的基本操作实战
- 解决Windows下mysql无法启动1067错误
- java多线程中并发集合和同步集合有哪些?区别是什么?
- 机器学习-数据可视化神器matplotlib学习之路(三)
- 在Office上怎么用MathType编辑公式
- POJ3278_Catch that cow
- gdb -Mysql源代码级调试方法
- HUE中oozie执行shell
- Harbor 镜像管理专家
- Oracle数据库基本语句练习
- Cannot load JDBC driver class &#39;com.mysql.jdbc.Driver &#39;
热门文章
- OC中控制台日志打印
- Form实现主从块金额汇总
- JS 执行环境与作用域链
- Parallel.ForEach , ThreadPool.QueueUserWorkItem
- Java常见排序算法之Shell排序
- CentOS 6.4安装本地yum源,并安装X Window System
- C++ 迭代器类别
- CodeForces 164C Machine Programming 费用流
- ecmall二次开发 直接实例化mysql对象
- 手把手让你爱上Android sdk自带“9妹”(9patch 工具)