The Profile class represents an immutable Facebook user profile with a global currentProfile instance for adding social context to your application.
Import
import { Profile } from 'react-native-fbsdk-next';
Static Methods
getCurrentProfile
Retrieves the current logged-in user’s profile.
Profile.getCurrentProfile(): Promise<Profile | null>
Returns a Promise that resolves to:
- A
Profile instance if a user is logged in
null if no user is logged in
Example:
import { Profile } from 'react-native-fbsdk-next';
const profile = await Profile.getCurrentProfile();
if (profile) {
console.log('User ID:', profile.userID);
console.log('Name:', profile.name);
console.log('Email:', profile.email);
} else {
console.log('No user logged in');
}
Instance Properties
All properties are read-only and may be null if not available.
userID
The user’s Facebook ID.
name
The user’s complete name.
firstName
The user’s first name.
firstName?: string | null
lastName
The user’s last name.
middleName
The user’s middle name.
middleName?: string | null
email
The user’s email address.
iOS only. This field is only populated if the user granted the email permission. Not available on Android.
imageURL
URL to the user’s profile image.
linkURL
URL to the user’s Facebook profile.
This field is only populated if the user granted the user_link permission.
refreshDate
The last time the profile data was fetched.
refreshDate?: Date | null
friendIDs
Array of the user’s friend IDs.
friendIDs?: Array<string> | null
iOS Limited Login only. This field is only populated if the user granted the user_friends permission and is using Limited Login on iOS.
birthday
The user’s birthday.
iOS Limited Login only. This field is only populated if the user granted the user_birthday permission and is using Limited Login on iOS.
ageRange
The user’s age range.
ageRange?: { min: number; max: number } | null
iOS Limited Login only. This field is only populated if the user granted the user_age_range permission and is using Limited Login on iOS.
Example:
if (profile?.ageRange) {
console.log(`Age range: ${profile.ageRange.min}-${profile.ageRange.max}`);
}
hometown
The user’s hometown.
hometown?: { id: string; name: string } | null
iOS Limited Login only. This field is only populated if the user granted the user_hometown permission and is using Limited Login on iOS.
Example:
if (profile?.hometown) {
console.log(`Hometown: ${profile.hometown.name} (${profile.hometown.id})`);
}
location
The user’s current location.
location?: { id: string; name: string } | null
iOS Limited Login only. This field is only populated if the user granted the user_location permission and is using Limited Login on iOS.
Example:
if (profile?.location) {
console.log(`Location: ${profile.location.name} (${profile.location.id})`);
}
gender
The user’s gender.
iOS Limited Login only. This field is only populated if the user granted the user_gender permission and is using Limited Login on iOS.
permissions
Array of permissions the user has granted.
permissions?: Array<string> | null
Usage Examples
Display User Profile
import React, { useEffect, useState } from 'react';
import { View, Text, Image } from 'react-native';
import { Profile } from 'react-native-fbsdk-next';
const UserProfile = () => {
const [profile, setProfile] = useState<Profile | null>(null);
useEffect(() => {
const loadProfile = async () => {
const currentProfile = await Profile.getCurrentProfile();
setProfile(currentProfile);
};
loadProfile();
}, []);
if (!profile) {
return <Text>Loading profile...</Text>;
}
return (
<View>
{profile.imageURL && (
<Image
source={{ uri: profile.imageURL }}
style={{ width: 100, height: 100, borderRadius: 50 }}
/>
)}
<Text>Name: {profile.name}</Text>
<Text>User ID: {profile.userID}</Text>
{profile.email && <Text>Email: {profile.email}</Text>}
</View>
);
};
Check if User is Logged In
import { Profile } from 'react-native-fbsdk-next';
const checkLoginStatus = async () => {
const profile = await Profile.getCurrentProfile();
if (profile) {
console.log('User is logged in:', profile.name);
return true;
} else {
console.log('No user logged in');
return false;
}
};
Access Extended Profile Data (iOS Limited Login)
import { Profile } from 'react-native-fbsdk-next';
import { Platform } from 'react-native';
const displayExtendedProfile = async () => {
const profile = await Profile.getCurrentProfile();
if (!profile) {
console.log('No profile available');
return;
}
console.log('Basic Info:');
console.log('- Name:', profile.name);
console.log('- User ID:', profile.userID);
if (Platform.OS === 'ios') {
console.log('\nExtended Info (iOS Limited Login):');
if (profile.birthday) {
console.log('- Birthday:', profile.birthday.toLocaleDateString());
}
if (profile.ageRange) {
console.log('- Age Range:', `${profile.ageRange.min}-${profile.ageRange.max}`);
}
if (profile.hometown) {
console.log('- Hometown:', profile.hometown.name);
}
if (profile.location) {
console.log('- Location:', profile.location.name);
}
if (profile.gender) {
console.log('- Gender:', profile.gender);
}
if (profile.friendIDs) {
console.log('- Friends Count:', profile.friendIDs.length);
}
}
if (profile.permissions) {
console.log('\nGranted Permissions:');
profile.permissions.forEach(perm => console.log('-', perm));
}
};
Profile Refresh Tracking
import { Profile } from 'react-native-fbsdk-next';
const checkProfileFreshness = async () => {
const profile = await Profile.getCurrentProfile();
if (profile?.refreshDate) {
const now = new Date();
const timeSinceRefresh = now.getTime() - profile.refreshDate.getTime();
const hoursSinceRefresh = timeSinceRefresh / (1000 * 60 * 60);
console.log(`Profile last refreshed ${hoursSinceRefresh.toFixed(1)} hours ago`);
if (hoursSinceRefresh > 24) {
console.log('Profile data may be stale');
// Consider re-authenticating or fetching fresh data
}
}
};
TypeScript Types
ProfileMap
Raw profile data object returned from native modules.
type ProfileMap = {
firstName?: string | null;
lastName?: string | null;
middleName?: string | null;
imageURL?: string | null;
linkURL?: string | null;
userID?: string | null;
email?: string | null;
name?: string | null;
refreshDate?: number | null;
friendIDs?: Array<string> | null;
birthday?: number | null;
ageRange?: { min: number; max: number } | null;
hometown?: { id: string; name: string } | null;
location?: { id: string; name: string } | null;
gender?: string | null;
permissions?: Array<string> | null;
};
Constructor
new Profile(profileMap: ProfileMap)
Creates a new immutable Profile instance from a ProfileMap. The profile object is frozen and cannot be modified after creation.
You typically don’t need to construct Profile instances manually. Use Profile.getCurrentProfile() instead.
Permissions Required
To access certain profile fields, users must grant specific permissions:
| Field | Permission Required | Platform |
|---|
email | email | iOS only |
linkURL | user_link | Both |
friendIDs | user_friends | iOS Limited Login |
birthday | user_birthday | iOS Limited Login |
ageRange | user_age_range | iOS Limited Login |
hometown | user_hometown | iOS Limited Login |
location | user_location | iOS Limited Login |
gender | user_gender | iOS Limited Login |
Example: Requesting permissions
import { LoginManager } from 'react-native-fbsdk-next';
const login = async () => {
const result = await LoginManager.logInWithPermissions([
'public_profile',
'email',
'user_friends',
'user_birthday',
]);
if (!result.isCancelled) {
const profile = await Profile.getCurrentProfile();
console.log('Profile with extended data:', profile);
}
};
iOS
- Full support for all profile fields
- Extended fields available with Limited Login
email field populated when permission granted
Android
- Basic fields supported
email field not available
- Extended fields (birthday, ageRange, etc.) not available
Best Practices
- Check for null values before accessing profile properties
- Request minimal permissions needed for your use case
- Cache profile data appropriately to minimize API calls
- Handle platform differences when accessing iOS-only fields
- Respect user privacy - only request and use data you truly need