Force Login with Unique ID
The force login method allows you to authenticate or create a player profile using a unique identifier from your system. This is useful for integrating with existing user databases or third-party authentication systems.
Overview
The playerForceLoginWithUniqueId method:
- Creates a new player if the unique ID doesn't exist
- Logs in existing player if the unique ID is found
- Supports additional user details for profile creation
- Returns a
Playerobject with player information
Method Signature
public function playerForceLoginWithUniqueId(
String $uniqueID,
String $channel = '',
array $userDetails = []
): ?Player
Parameters
Required Parameters
uniqueID(String) - Unique identifier from your system- Can be email, user ID, username, or any unique string
- Must be non-empty
Optional Parameters
-
channel(String) - Channel identifier for multi-channel support- Default: empty string
- Useful when you have multiple apps/games
-
userDetails(Array) - Additional player information- Supported keys:
name(String) - Player's display nameemailid(String) - Player's email addressphoneno(String) - Phone number (format: countrycode-number, no +)pwd(String) - Player's passwordprofilehiresimg(String) - Base64 encoded profile image
- Supported keys:
Return Value
Returns a Player object on success, null on failure.
Basic Usage
Simple Force Login
use Veniso\Moitribe\Sdk\modules\classes\MoitribeApi;
$moitribe = new MoitribeApi([
'gameid' => 'your-game-id',
'channelid' => 'your-channel-id',
'playerid' => 'temp-id'
]);
try {
$player = $moitribe->profRequestsHandler->playerForceLoginWithUniqueId('user-12345');
if ($player) {
echo "Login successful!\n";
echo "Player ID: " . $player->getId() . "\n";
echo "Player Name: " . $player->getName() . "\n";
} else {
echo "Login failed\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Force Login with User Details
$userDetails = [
'name' => 'John Doe',
'emailid' => 'john@example.com',
'phoneno' => '1-5551234567', // Format: countrycode-number
'pwd' => 'securepassword123'
];
try {
$player = $moitribe->profRequestsHandler->playerForceLoginWithUniqueId(
'user-12345',
'mobile-app',
$userDetails
);
if ($player) {
echo "Player created/logged in successfully!\n";
echo "Welcome, " . $player->getName() . "!\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Force Login with Profile Image
$userDetails = [
'name' => 'Jane Smith',
'emailid' => 'jane@example.com',
'profilehiresimg' => base64_encode(file_get_contents('profile.jpg'))
];
try {
$player = $moitribe->profRequestsHandler->playerForceLoginWithUniqueId(
'user-67890',
'',
$userDetails
);
if ($player) {
echo "Player with profile image created!\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Integration Examples
Integration with Existing User System
function authenticateUser($existingUserId, $userData = []) {
global $moitribe;
try {
// Use your existing user ID as unique identifier
$player = $moitribe->profRequestsHandler->playerForceLoginWithUniqueId(
$existingUserId,
'web-app',
$userData
);
if ($player) {
// Store Moitribe player ID for future use
$_SESSION['moitribe_player_id'] = $player->getId();
$_SESSION['player_name'] = $player->getName();
return true;
}
return false;
} catch (Exception $e) {
error_log("Moitribe authentication failed: " . $e->getMessage());
return false;
}
}
// Usage with your existing authentication
if (userIsLoggedIn()) {
$userId = getCurrentUserId();
$userData = [
'name' => getCurrentUserName(),
'emailid' => getCurrentUserEmail()
];
if (authenticateUser($userId, $userData)) {
echo "Successfully connected to Moitribe!";
}
}
Integration with Third-party Auth (OAuth)
function handleOAuthLogin($oauthUser) {
global $moitribe;
try {
$userDetails = [
'name' => $oauthUser['name'],
'emailid' => $oauthUser['email'],
// Use OAuth provider as channel
];
$player = $moitribe->profRequestsHandler->playerForceLoginWithUniqueId(
$oauthUser['id'], // Use OAuth user ID
$oauthUser['provider'], // e.g., 'google', 'facebook'
$userDetails
);
return $player;
} catch (Exception $e) {
error_log("OAuth integration failed: " . $e->getMessage());
return null;
}
}
// Example with Google OAuth
$googleUser = getGoogleUserData();
$player = handleOAuthLogin($googleUser);
if ($player) {
echo "Welcome from Google, " . $player->getName() . "!";
}
Error Handling
Common Exceptions
InvalidArgumentException- When unique ID is emptyException- For API communication errors
Error Handling Example
try {
$player = $moitribe->profRequestsHandler->playerForceLoginWithUniqueId($uniqueId, $channel, $userDetails);
if (!$player) {
// Handle null response
echo "Authentication failed - please try again";
return;
}
// Success
echo "Authenticated successfully!";
} catch (InvalidArgumentException $e) {
echo "Invalid input: " . $e->getMessage();
} catch (Exception $e) {
echo "Authentication error: " . $e->getMessage();
// Log error for debugging
error_log("Moitribe auth error: " . $e->getMessage());
}
Best Practices
-
Use Consistent Unique IDs
- Always use the same unique identifier for the same user
- Don't change unique IDs after initial login
-
Validate Input Data
- Ensure unique ID is not empty
- Validate email format if provided
- Check phone number format
-
Handle Profile Updates
- Use
updatePlayerInfofor updating existing profiles - Don't rely on force login for profile updates
- Use
-
Error Logging
- Log authentication failures for debugging
- Monitor for unusual authentication patterns
Related Methods
- Update Player Profile - Update existing player information
- Get Player Friends - Get player's friends list
- Get Multiple Players - Get multiple player profiles