-
-
Save nextendweb-laszlo/8753826f1fdc1fae84342d3b957b18d3 to your computer and use it in GitHub Desktop.
Skeleton code example for implementing the registration and linking using the functions of Nextend Social Login.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Step1: | |
* -Get an access token and put it into an associative array with the key: access_token_data | |
* -Read out the providerID from the request. | |
*/ | |
$providerID = 'google'; | |
$access_token = '{"access_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","expires_in":3599,"refresh_token":"1\/\/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy","scope":"https:\/\/www.googleapis.com\/auth\/user.birthday.read https:\/\/www.googleapis.com\/auth\/user.phonenumbers.read https:\/\/www.googleapis.com\/auth\/user.addresses.read https:\/\/www.googleapis.com\/auth\/userinfo.email https:\/\/www.googleapis.com\/auth\/userinfo.profile openid","token_type":"Bearer","id_token":"zzzzzzzzzzzzzzzzzzzzzzzzzzzz","created":1664258821}'; | |
$authOptions['access_token_data'] = $access_token; | |
//Note: The Steam provider works differently, as it is based on OpenID authentication. In case of Steam you need the Claimed ID instead of the access token: | |
/* | |
$providerID = 'steam'; | |
$openid_claimed_id = 'https://steamcommunity.com/openid/id/01234567891234567'; | |
$authOptions['openid_claimed_id'] = $openid_claimed_id; | |
*/ | |
function nslLinkOrRegister($providerID, $authOptions) { | |
$provider = NextendSocialLogin::getProviderByProviderID($providerID); | |
if ($provider) { | |
$social_user_id = $provider->getAuthUserDataByAuthOptions('id', $authOptions); | |
if ($social_user_id) { | |
/** | |
* Step2: Check if the social media account is linked to any WordPress account. | |
*/ | |
$wordpress_user_id = $provider->getUserIDByProviderIdentifier($social_user_id); | |
if (!is_user_logged_in()) { | |
/** | |
* Step3: Handle the logged out users | |
*/ | |
if ($wordpress_user_id !== null) { | |
$provider->triggerSync($wordpress_user_id, $authOptions, "login", true); | |
/** | |
* Step 4: This social media account is already linked to a WordPress account.-> Log the user in using the returned User ID. | |
*/ | |
return $wordpress_user_id; | |
} else { | |
/** | |
* Step 5: This social media account is not linked to any WordPress account, yet. -> Find out if we need to Link or Register | |
*/ | |
$wordpress_user_id = false; | |
/** | |
* Step 6: Attempt to match a WordPress account with the email address returned by the provider: | |
*/ | |
$email = $provider->getAuthUserDataByAuthOptions('email', $authOptions); | |
if (empty($email)) { | |
$email = ''; | |
} else { | |
$wordpress_user_id = email_exists($email); | |
} | |
if ($wordpress_user_id !== false) { | |
/** | |
* Step 7: There is an email address match -> Link the existing user to the provider | |
*/ | |
if ($provider->linkUserToProviderIdentifier($wordpress_user_id, $social_user_id)) { | |
$provider->triggerSync($wordpress_user_id, $authOptions, "login", true); | |
//log the user in if the linking was successful | |
return $wordpress_user_id; | |
} else { | |
// Throw error: User already have another social account from this provider linked to the WordPress account that has the email match. They should use that account. | |
} | |
} else { | |
/** | |
* Step 8: There is no email address match -> Register a new WordPress account, e.g. with wp_insert_user() | |
* fill $user_data with the data that the provider returned | |
*/ | |
$user_data = array( | |
'user_login' => '', | |
//generate a unique username, e.g. from the name returned by the provider: $provider->getAuthUserDataByAuthOptions('name', $authOptions); | |
'user_email' => $email, | |
//use the email address returned by the provider, note: it can be empty in certain cases | |
'user_pass' => '', | |
//generate a password, e.g.: with wp_generate_password() | |
'display_name' => '', | |
//generate a display name, e.g. from the name returned by the provider: $provider->getAuthUserDataByAuthOptions('name', $authOptions); | |
'first_name' => '', | |
//generate a first name, e.g.: from the first name returned by the provider: $provider->getAuthUserDataByAuthOptions('first_name', $authOptions); | |
'last_name' => '', | |
//generate a last name, e.g.: from the last name returned by the provider: $provider->getAuthUserDataByAuthOptions('last_name', $authOptions); | |
); | |
$wordpress_user_id = wp_insert_user($user_data); | |
if (!is_wp_error($wordpress_user_id) && $wordpress_user_id) { | |
/** | |
* Step 9: Link the new user to the provider | |
*/ | |
if ($provider->linkUserToProviderIdentifier($wordpress_user_id, $social_user_id, true)) { | |
$provider->triggerSync($wordpress_user_id, $authOptions, 'register', false); | |
$provider->triggerSync($wordpress_user_id, $authOptions, "login", true); | |
//The registration and the linking was successful -> log the user in. | |
return $wordpress_user_id; | |
} | |
} else { | |
//Throw error: There was an error with the registration | |
} | |
} | |
} | |
} else { | |
/** | |
* Step 10: Handle the linking for logged in users | |
*/ | |
$current_user = wp_get_current_user(); | |
if ($wordpress_user_id === null) { | |
// Let's connect the account to the current user! | |
if ($provider->linkUserToProviderIdentifier($current_user->ID, $social_user_id)) { | |
//account is linked, we don't need to trigger additional actions we just need to sync the avatar | |
$provider->triggerSync($current_user->ID, $authOptions, false, true); | |
return $current_user->ID; | |
} else { | |
//Throw error: Another social media account is already linked to the current WordPress account. The user need to unlink the currently linked one and he/she can link the this social media account. | |
} | |
} else if ($current_user->ID != $wordpress_user_id) { | |
//Throw error: This social account is already linked to another WordPress user. | |
} | |
} | |
} | |
} | |
return false; | |
} | |
try { | |
$user_id = nslLinkOrRegister($providerID, $authOptions); | |
/** | |
* $user_id contains the User ID if the registration/linking was successful or if the account was already linked to an existing account | |
* otherwise it returns false. | |
* If the user is not logged in already, you can log the user with the $user_id in. | |
*/ | |
} catch (Exception $e) { | |
//handle the exceptions | |
return new WP_Error('error', $e->getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment