[wordpress]wp-api-jwt-auth 尝试添加运行在多站点中 need change
2016-10-25 16:26
120 查看
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 :
get user blogs:
get get first blog token :
get
step 2 api like:
file
i add method,code is:
the code many is use the
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,param
2is the user_blogid,get the return token,change the Angularjs saved user Token
get
testblog 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,method
add_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教程add_submenu_page)
- 为WordPress编辑器添加WP-Syntax按钮
- [Wordpress]wp_dropdown_categories() 添加自定义的attribute(属性)
- 品尝阿里云容器服务:初步尝试ASP.NET Core Web API站点的Docker自动化部署
- Lumen上使用Dingo/Api做API开发时用JWT-Auth做认证的实现
- 为WordPress站点添加论坛
- How to implement JWT Auth About Restful API in Laravel 5.5
- wordpress如何屏蔽wp-json(禁用REST API)
- iOS 运行时runtime应用之二--在category中使用运行时API给类添加属性
- 品尝阿里云容器服务:初步尝试ASP.NET Core Web API站点的Docker自动化部署
- 黄聪:WordPress 多站点建站教程(六):使用WP_Query、switch_to_blog函数实现获取子站点分类中的文章
- Lumen上使用Dingo/Api做API开发时用JWT-Auth做认证的实现
- wordpress 添加站点统计
- [WP]常见wordpress安装,运行,使用出现500错误分析和解决方法
- WordPress JSON REST API (WP API) 使用文档-创建\获取\编辑\删除文章
- 建立WordPress电子商务站点的插件,WP商城支付插件
- [All in one WP Migration plugin] 搬迁wordpress 站点
- Wordpress入门建站教程二:搭建WP站点的准备工作
- lumen5.4整合dingo/api、jwt-auth
- 五种WordPress防止垃圾评论方法-过滤垃圾评论提高WP运行效率