Handling the user
object
You can get information about the current user from the user
object in the usePrivy
hook:
const {user} = usePrivy();
For unauthenticated users, the user
object will be null. For authenticated users, you can use:
user.id
to get their Privy DID, which you may use to identify your user on your backenduser.createdAt
to get a JavaScript Date object of when the user was created
The user
object also contains information about all of the accounts a user has linked with Privy. Use the fields:
user.email
to get the user's email accountuser.wallet
to get the user's Ethereum wallet (crypto account)user.google
to get the user's Google accountuser.discord
to get the user's Discord accountuser.twitter
to get the user's Twitter accountuser.github
to get the user's Github accountuser.apple
to get the user's Apple accountuser.phone
to get the user's phone (SMS) number
If a user has not linked an account of a given type, the corresponding field on the user
object will be undefined.
Different account types contain different information about the underlying account. For example, you might use user.wallet.address
to get a user's Ethereum address and user.discord.username
to get a user's Discord username. Check out the SDK Reference (linked above for each account type) to see what data each type contains.
Alternatively, you can also get a list of all the accounts a user has linked with Privy via user.linkedAccounts
. Each account in this list is additionally annotated with a type
field that denotes the type of account ('email', 'phone', 'wallet', etc.).
Below is an example of how you might use the user
object in a minimal user profile:
import {usePrivy} from '@privy-io/react-auth';
function User() {
const {ready, authenticated, user} = usePrivy();
// Show nothing if user is not authenticated or data is still loading
if (!(ready && authenticated) || !user) {
return null;
}
return (
<div>
<p>User {user.id} has linked the following accounts:</p>
<ul>
<li>Email: { user.email ? user.email.address : 'None' }</li>
<li>Wallet: { user.wallet ? user.wallet.address : 'None' }</li>
<li>Google: { user.google ? user.google.email : 'None' }</li>
<li>Discord: { user.discord ? user.discord.username : 'None' }</li>
<li>Twitter: { user.twitter ? user.twitter.username : 'None' }</li>
<li>Github: { user.github ? user.github.username : 'None' }</li>
<li>Phone: { user.phone ? user.phone.number : 'None' }</li>
</ul>
</div>
);
}