4

WordPress 版本 4.7.5

我正在从 Instagram 注册新用户,我可以成功注册新用户。注册后我试图登录用户。

我可以使用钩子,但想要比肮脏的更好的解决方案,我已经发布了,因为那将完全没用。

if( $user_created ) {
    $new_user = get_user_by( 'id', $user_created ); 
    //$loggedin = programmatic_login( $new_user->user_login );
    //ob_start();
    if ( !is_user_logged_in() ) {
        wp_clear_auth_cookie();
        wp_set_current_user( $user_created, $new_user->user_login );
        wp_set_auth_cookie( $user_created, true );
        do_action( 'wp_login', $new_user->user_login );
        // wp_safe_redirect(site_url() . '/profile/');
        // exit;
        // $redirect_to=user_admin_url();
        // wp_safe_redirect($redirect_to);
        // exit();
    }
    //ob_end_clean();
} ?>
<script>jQuery(document).ready(function(){ window.location.href = "<?php echo site_url() . '/profile/'; ?>";});</script> <?php

还尝试使用另一篇文章中的自定义programmatic_login函数。

如果我试图低于此var_dump wp_get_current_user()$_COOKIE我将获得用户对象 forwp_get_current_user()array(1) { ["wordpress_test_cookie"]=> string(15) "WP Cookie check" }for $_COOKIE

重要编辑

代码在一个函数内部,该函数与一个钩子挂钩,该钩子在页眉显示后的页面中调用,因此我们不能在这里使用 init。这样做的任何其他方式,也是因为这个wp_safe_redirect()header('Location: ')也不起作用。

我刚刚尝试过的重要更新

肮脏的工作

当我从 Instagram 登录创建用户时,成功创建后,我将用户重定向到一个带有类似 get 参数的页面,?user_id=$inserted_id并且在 wp 钩子上,我试图获取GET['user_id']并记录它并且它工作,试图找到一个合适的解决方案。

if ( $wpdb->insert_id ){  //that is registration with instagram
   ?>
    <script>jQuery(document).ready(function(){ window.location.href = "<?php echo get_bloginfo('url') . '/profile/?id=' . $wpdb->insert_id; ?>";});</script>
   <?php
}

接着

add_action( 'wp', 'login_current_user' );
function login_current_user(){
    if ( is_page() && get_the_id() == 863 ){
        if ( isset( $_GET['id'] ) ){
            if ( !is_user_logged_in() ) {
                $user_id = $_GET['id'];
                $user = get_user_by( 'id', $user_id ); 
                wp_clear_auth_cookie();
                wp_set_current_user( $user_id, $user->user_login );
                wp_set_auth_cookie( $user_id, true );
                do_action( 'wp_login', $user->user_login );
                if ( is_user_logged_in() ){
                    $redirect_to=site_url() . '/profile';
                    wp_safe_redirect($redirect_to);
                    exit();
                }
            }
        }
    }
}

有肮脏的把戏的问题。如果id我们传递的 get 参数存在于 wp_users 表中,则无需验证即可登录。

更新

我在重定向到个人资料页面之前创建了一个 user_meta,在验证登录用户并删除了 usermeta 之后,就像 OTP 一样。如果可能的话,努力让它变得更好。

在代码下方: -

if ( $wpdb->insert_id ){  //that is registration with instagram
    $temporary_token = sha1(rand());
    update_user_meta( $wpdb->insert_id, 'temporary_token', $temporary_token);
   ?>
    <script>jQuery(document).ready(function(){ window.location.href = "<?php echo get_bloginfo('url') . '/profile/?id=' . $wpdb->insert_id . '&token=' . $temporary_token; ?>";});</script>
   <?php
}

接着

add_action( 'wp', 'login_current_user' );
    function login_current_user(){
        if ( is_page() && get_the_id() == 863 ){
            if ( isset( $_GET['id'] ) ){
                if ( !is_user_logged_in() ) {
                    $user_id = $_GET['id'];
                    $user = get_user_by( 'id', $user_id );
                    if ( $_GET['token'] == get_user_meta( $user_id, 'temporary_token', true ) ){
                        delete_user_meta( $user_id, 'temporary_token', $_GET['token'] ); 
                        wp_clear_auth_cookie();
                        wp_set_current_user( $user_id, $user->user_login );
                        wp_set_auth_cookie( $user_id, true );
                        do_action( 'wp_login', $user->user_login );
                        if ( is_user_logged_in() ){
                            $redirect_to=site_url() . '/profile';
                            wp_safe_redirect($redirect_to);
                            exit();
                        }
                    }
                }
            }
        }
    }
4

1 回答 1

3

如果标头之前已发送wp_clear_auth_cookie()或被wp_set_auth_cookie()调用,那么两者都不会起作用,因为两者都依赖于 PHP 的setcookie函数。该after_setup_theme操作将在发送标头之前发生,因此挂钩并在那里设置 cookie 应该可以解决问题。

function myPlugin_createUser(){
  // Probably need to put code that creates user here too otherwise $user_created will never return true
  if( $user_created ) {
      $new_user = get_user_by( 'id', $user_created ); 
      if ( !is_user_logged_in() ) {
          wp_clear_auth_cookie();
          wp_set_current_user( $user_created, $new_user->user_login );
          wp_set_auth_cookie( $user_created, true );
          do_action( 'wp_login', $new_user->user_login );
      }
  } 
}

add_action( 'after_setup_theme', 'myPlugin_createUser' );

更新:

如果您在触发createUser函数之前输出视图元素,它将不起作用,因此我们需要确保它们完全独立地运行。下面我整理了一个简单的例子来说明如何实现这一点 - 我们向视图输出一个传递 GET 参数的链接,该参数由插件获取并在呈现任何内容之前运行操作,从而允许我们设置 cookie .

我的插件.php

// Note: you'll need to include a completed version of myPlugin_createUser() function as above for these examples to work
add_shortcode( 'my-plugin', function() {  
   // I've used a GET request for simplicity- a form + POST request may be more practical for your purposes and will prevent caching issues.
   echo "<a href='?my_plugin_login_user=1'>Login</a>";
});

// Check for our GET parameter 
if( isset( $_GET['my_plugin_login_user'] ) ) {
  // Nothing should be rendered yet as plugin template hasn't even started loading yet so cookies should set fine
  add_action( 'plugins_loaded', 'myPlugin_createUser' ); 
}

页面我的模板.php

<div> ...
    <?php 
        do_shortcode( 'my-plugin' ); 
    ?>
</div>

更新 2: 如果使用 Instagram 身份验证 API,则概述https://www.instagram.com/developer/authentication/的显式流程(下面使用的大多数示例都取自那里)可能是最好的方法。下面我将简要介绍如何实现它。其中大部分是从那里获取的,并添加了注释。

将用户发送到https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

您可以将 WordPress 用户 ID 作为参数包含在回调 URL 中,或者如果尚未创建用户并且您需要维护状态,那么您需要生成一个临时令牌,稍后您可以使用该令牌与所述相关联数据或存储该数据客户端,如果它不敏感,这可能更容易和更好,因为它不需要用户登录才能运行。

然后用户将登录

关于这一步没什么好说的——如果发生错误,他们将被重定向到http://your-redirect-uri?error=access_denied&error_reason=user_denied&error_description=The+user+denied+your+request

如果登录成功,用户将被重定向到http://your-redirect-uri?code=CODE

传回的CODE重定向 URL 我们可以兑换访问令牌。

因此,从这里我们可以有多种方法来处理它,但本质上我们需要的是一个重定向端点,我们有足够的控制权来在发送任何 HTTP 响应正文之前发送 HTTP 标头。

接近这个的方法(不是一个详尽的列表):

  • 允许有条件地挂钩页面 Instagram 身份验证(如您的示例中所示)。这具有不需要额外重定向的优点。
  • 自定义页面模板
  • 手动引导 WordPress 的外部脚本执行我们需要的操作,然后重定向回

因此,现在一旦我们完成了端点设置,我们就需要兑换CODE访问令牌。改编自https://gist.github.com/arturmamedov/7f5a90b85a20e06e344ebb88dc898d25

$uri = 'https://api.instagram.com/oauth/access_token'; 
$data = [
    'client_id' => '213esdaedasdasd12...YOUR_CLIENT_ID', 
    'client_secret' => 'a8b4aaf06c0da310...YOUR_CLIENT_SECRET', 
    'grant_type' => 'authorization_code', 
    'redirect_uri' => 'http://www.YOUR_REDIRECT_URL.it',      // The exact redirect URL as used previously
    'code' => $_GET['code']  
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri); // uri
curl_setopt($ch, CURLOPT_POST, true); // POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // POST DATA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN RESULT true
curl_setopt($ch, CURLOPT_HEADER, 0); // RETURN HEADER false
curl_setopt($ch, CURLOPT_NOBODY, 0); // NO RETURN BODY false / we need the body to return
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // VERIFY SSL HOST false
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // VERIFY SSL PEER false
$result = json_decode(curl_exec($ch));

$result 将是 JSON 响应的对象表示:

{
"access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
"user": {
    "id": "1574083",
    "username": "snoopdogg",
    "full_name": "Snoop Dogg",
    "profile_picture": "..."
  }
}

从这里我们只需创建我们的用户/使用 WordPress 进行身份验证并重定向到所需的页面/呈现模板(如果通过条件挂钩处理则无需重定向)并在需要时恢复页面状态。

于 2017-06-05T09:50:32.563 回答