Get Player Profile
After a player successfully authenticates, their profile data is automatically retrieved and made available through the SDK. This guide explains how to access player profile information in your game.
Overview
Player profile data is returned in the loginCallback after successful authentication. The profile includes the player's name, ID, images, level, and other information stored on Moitribe servers.
Profile data is automatically retrieved during authentication. You don't need to make a separate API call to fetch profile information.
Accessing Profile Data
During Authentication
Profile data is available immediately after successful login:
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result) => {
if (result.success) {
// Profile data is in result.playerdata
const profile = result.playerdata;
console.log('Player ID:', profile.id);
console.log('Player Name:', profile.name);
console.log('Player Level:', profile.level);
} else {
console.log('Player not authenticated');
}
}
});
TypeScript Example
Use proper typing for better type safety:
import MoitribeSDK from '@veniso/moitribe-js';
import type { SignedInProfile } from '@veniso/moitribe-js';
interface LoginCallbackResult {
success: boolean;
playerdata?: SignedInProfile;
}
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result: LoginCallbackResult) => {
if (result.success && result.playerdata) {
const profile: SignedInProfile = result.playerdata;
console.log('Player ID:', profile.id);
console.log('Player Name:', profile.name);
console.log('Player Level:', profile.level);
console.log('Profile Icon:', profile.profileicon);
}
}
});
Profile Data Contents
The playerdata object contains the following fields:
| Field | Type | Description | Example |
|---|---|---|---|
id | string | Unique player identifier | 'player_12345' |
name | string | Player's display name | 'Alex' |
title | string | Player's title or rank | 'Champion' |
level | string | Player's current level | '25' |
profileicon | string | URL to profile icon | 'https://...' |
profilehiresimg | string | URL to high-res image | 'https://...' |
bannerimgland | string | Landscape banner URL | 'https://...' |
bannerimgport | string | Portrait banner URL | 'https://...' |
friends | string[] | Array of friend IDs | ['player_123', ...] |
See Profile Data Structure for detailed descriptions of each field.
Storing Profile Data
Store the profile data in your application state for easy access throughout the game session:
JavaScript Example
// Store profile globally
let currentPlayer = null;
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result) => {
if (result.success) {
// Store profile for later use
currentPlayer = result.playerdata;
// Update game UI
updatePlayerDisplay();
startGame();
} else {
showLoginScreen();
}
}
});
// Access profile anywhere in your game
function updatePlayerDisplay() {
if (currentPlayer) {
document.getElementById('player-name').textContent = currentPlayer.name;
document.getElementById('player-level').textContent = currentPlayer.level;
document.getElementById('player-avatar').src = currentPlayer.profileicon;
}
}
function getPlayerId() {
return currentPlayer?.id || null;
}
TypeScript Example
import type { SignedInProfile } from '@veniso/moitribe-js';
class PlayerManager {
private static currentPlayer: SignedInProfile | null = null;
static setPlayer(profile: SignedInProfile): void {
this.currentPlayer = profile;
}
static getPlayer(): SignedInProfile | null {
return this.currentPlayer;
}
static getPlayerId(): string | null {
return this.currentPlayer?.id || null;
}
static getPlayerName(): string {
return this.currentPlayer?.name || 'Guest';
}
}
// Initialize SDK and store profile
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result: { success: boolean; playerdata?: SignedInProfile }) => {
if (result.success && result.playerdata) {
PlayerManager.setPlayer(result.playerdata);
}
}
});
// Access player data anywhere
const playerName = PlayerManager.getPlayerName();
const playerId = PlayerManager.getPlayerId();
Common Use Cases
Display Player Information
Show player details in your game UI:
function displayPlayerCard(profile) {
const card = document.createElement('div');
card.className = 'player-card';
card.innerHTML = `
<img src="${profile.profileicon}" alt="${profile.name}" class="avatar">
<div class="info">
<h3>${profile.name}</h3>
<p>Level ${profile.level} - ${profile.title}</p>
</div>
`;
document.getElementById('player-info').appendChild(card);
}
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result) => {
if (result.success) {
displayPlayerCard(result.playerdata);
}
}
});
Use Profile in Game Logic
Access profile data for game features:
function canAccessPremiumFeature(profile) {
// Check player level
const level = parseInt(profile.level);
return level >= 10;
}
function getPlayerAvatar(profile) {
// Return high-res image if available, otherwise use icon
return profile.profilehiresimg || profile.profileicon;
}
function hasCompletedTutorial(profile) {
// Check player level to determine tutorial status
return parseInt(profile.level) > 1;
}
Display Profile Images
Load and display player profile images:
function loadPlayerImages(profile) {
// Load profile icon for small displays
const avatarImg = document.getElementById('avatar');
avatarImg.src = profile.profileicon;
avatarImg.alt = `${profile.name}'s avatar`;
// Load high-resolution image for profile page
const profileImg = document.getElementById('profile-image');
profileImg.src = profile.profilehiresimg || profile.profileicon;
// Load banner for profile background
const banner = document.getElementById('profile-banner');
banner.style.backgroundImage = `url(${profile.bannerimgland})`;
// Handle image load errors
avatarImg.onerror = () => {
avatarImg.src = 'default-avatar.png';
};
}
Check Social Connections
Access friend list from profile:
function displayFriendCount(profile) {
const friendCount = profile.friends?.length || 0;
console.log(`${profile.name} has ${friendCount} friends`);
document.getElementById('friend-count').textContent = friendCount;
}
function hasFriend(profile, friendId) {
return profile.friends?.includes(friendId) || false;
}
function getFriendIds(profile) {
return profile.friends || [];
}
Profile After Updates
After calling updateProfile(), the updated profile is returned in the callback:
MoitribeSDK('my-game-id', 'updateProfile', {
name: 'NewPlayerName',
callback: (result) => {
if (result.success) {
// Updated profile is returned
if (result.playerdata) {
currentPlayer = result.playerdata;
console.log('Updated name:', result.playerdata.name);
updatePlayerDisplay();
}
}
}
});
Not all update operations return the full updated profile. Always check if result.playerdata exists before using it.
Profile Data Availability
Profile data is available:
- ✅ After successful OTP login
- ✅ After successful social login
- ✅ After successful account creation
- ✅ After successful profile update (in some cases)
- ❌ Before authentication
- ❌ During guest sessions
Checking if Profile Exists
Always check if profile data is available:
function getPlayerInfo() {
if (currentPlayer && currentPlayer.id) {
return {
authenticated: true,
name: currentPlayer.name,
id: currentPlayer.id,
level: currentPlayer.level
};
}
return {
authenticated: false,
name: 'Guest',
id: null,
level: 0
};
}
TypeScript Null Checking
Use proper null checking in TypeScript:
function displayPlayerName(profile: SignedInProfile | null): void {
const nameElement = document.getElementById('player-name');
if (nameElement) {
nameElement.textContent = profile?.name || 'Guest';
}
}
function getPlayerLevel(profile: SignedInProfile | null): number {
if (!profile) return 0;
return parseInt(profile.level) || 0;
}
Error Handling
Handle cases where profile data might not be available:
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result) => {
if (result.success && result.playerdata) {
// Profile successfully retrieved
currentPlayer = result.playerdata;
startGame();
} else if (result.success && !result.playerdata) {
// Authenticated but no profile data (unusual case)
console.warn('Authenticated but no profile data received');
showError('Unable to load profile. Please try again.');
} else {
// Not authenticated
showLoginScreen();
}
}
});
Best Practices
1. Store Profile in Application State
Keep profile data accessible throughout your application:
// Good: Store profile in global state
let appState = {
player: null,
isAuthenticated: false
};
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result) => {
if (result.success) {
appState.player = result.playerdata;
appState.isAuthenticated = true;
}
}
});
2. Validate Profile Data
Check that required fields exist:
function isValidProfile(profile) {
return profile &&
profile.id &&
profile.name &&
typeof profile.id === 'string' &&
typeof profile.name === 'string';
}
MoitribeSDK('my-game-id', 'init', {
loginCallback: (result) => {
if (result.success && isValidProfile(result.playerdata)) {
currentPlayer = result.playerdata;
startGame();
} else {
showError('Invalid profile data received');
}
}
});
3. Handle Image Loading
Provide fallbacks for profile images:
function getProfileImage(profile, defaultImage = 'default-avatar.png') {
if (!profile) return defaultImage;
// Try high-res first, then icon, then default
return profile.profilehiresimg ||
profile.profileicon ||
defaultImage;
}
4. Cache Profile Data
Avoid repeatedly accessing nested properties:
// Bad: Repeatedly accessing nested properties
function displayProfile() {
console.log(currentPlayer?.name);
console.log(currentPlayer?.level);
console.log(currentPlayer?.title);
// ...
}
// Good: Destructure once
function displayProfile() {
if (!currentPlayer) return;
const { name, level, title, profileicon } = currentPlayer;
console.log(name, level, title);
// ...
}
Next Steps
- Update Profile - Modify player profile information
- Profile Data Structure - Complete field reference
- Authentication Overview - Understanding the login flow
Related topics:
- Check Authentication - Verify login status
- Login with OTP - Authenticate players
- Advanced: Storage - Local data persistence