Problem with your api registration logic
-
Perfect plugin, not a perfect way to register or login programmatically.
It is mentioned in the docs that register and login through api is not supported, well it makes sense we need to connect it to our authentication systems (whatever we use), why do we need a ready api for that?
But here at number (3) https://nextendweb.com/nextend-social-login-docs/nextend-social-login-rest-api/
You are suggesting this gist : https://gist.github.com/nextend/012ca54957e27dbea952fc42195fb0d1
to make our own register and login with it.It is good but not good enough, you already take care of a lot such a grabbing the brith-day from google getting the avatar and so on.
Why would we need to re-implement all those once again in our register logic?
Long story short, you need to provide a function that we can call, pass in the access_token and provider and it should handle register if the user is not already register and return its ID, if the user has registered just return its ID.
This makes a lot of sense why do we need to do everything you’ve done again? But not having a rest api for it as you decided makes sense we just need a
better function for doing it ourselves.——
Okay, let’s get to it, I have read your code and in a way did it. (it’s a workaround or a hack). the main issue with your code is in just blindly redirects and exits here and there, if you put an option for that (a couple of if statements here and there) and we are done.
$providerID = $params["provider_id"]; $accessToken = $params["access_token_data"]; $data = [ "access_token_data" => $accessToken, ]; // it's an api we don't need auth cookies, disable them. add_filter("send_auth_cookies", "__return_false"); // nextend outputs some html for redirection don't let it output it. ob_start(); if (NextendSocialLogin::isProviderEnabled($providerID)) { $provider = NextendSocialLogin::$enabledProviders[$providerID]; try { /** * suggested by nextend from https://gist.github.com/nextend/012ca54957e27dbea952fc42195fb0d1 * * This basically gets userdata from access_token and saves it on the provider. */ $socialID = $provider->findSocialIDByAccessToken( $accessToken ); // found by me which is internally used for register and login. $socialUser = new NextendSocialUser($provider, $data); // logout if it is already logged in, otherwise NextendSocialUser won't respond. if (get_current_user_id() > 0) { wp_logout(); } // after liveConnectGetUserProfile on $socialUser(NextendSocialUser) has runs it will redirect and exit so let's catch it before it exits register_shutdown_function(function () use ( $socialUser, $socialID ) { //region get rid of the html nextend outputs for redirection. ob_get_clean(); ob_flush(); //endregion $user_id = $socialUser->getProvider()->getUserIDByProviderIdentifier($socialID); // or get_current_user_id(); var_dump($user_id); // we are done here! well after a lot of twists and turns, it works but why does it need to be like this? it doesn't. }); /** * does the login or register on access token then you can get user id after this, via * 1. get_current_user_id() * or * 2. $user_id = $socialUser->getProvider()->getUserIDByProviderIdentifier($socialID); * */ $socialUser->liveConnectGetUserProfile(); } catch (Exception $e) { // TODO do something about exceptions. } }
- The topic ‘Problem with your api registration logic’ is closed to new replies.