You can use the boolean authenticated
from the usePrivy
hook to determine if your user is authenticated or not.
Before determining a user’s auth status from Privy, you should verify that
Privy has fully initialized and is ready
Usage
import { useRouter } from "next/router";
import { usePrivy } from "@privy-io/react-auth";
export default function MyComponent() {
const { ready, authenticated, user } = usePrivy();
const router = useRouter();
if (!ready) {
// Do nothing while the PrivyProvider initializes with updated user state
return <></>;
}
if (ready && !authenticated) {
// Replace this code with however you'd like to handle an unauthenticated user
// As an example, you might redirect them to a login page
router.push("/login");
}
if (ready && authenticated) {
// Replace this code with however you'd like to handle an authenticated user
return <p>User {user?.id} is logged in.</p>;
}
}
You can use the user
object from the usePrivy
hook to determine if your user is authenticated or not.
Usage
import { useRouter } from "expo-router";
import { usePrivy } from "@privy-io/expo";
import { useEffect } from "react";
import { Text, View } from "react-native";
export default function MyComponent() {
const { isReady, user } = usePrivy();
const router = useRouter();
useEffect(() => {
if (isReady && !user) {
// Replace this code with however you'd like to handle an unauthenticated user
// As an example, you might redirect them to a login page
router.replace("/login");
}
}, [isReady, user, router]);
if (!isReady) {
// Do nothing while the PrivyProvider initializes with updated user state
return null;
}
if (isReady && !user) {
// You could show a loading state or handle this differently
return <Text>Not authenticated</Text>;
}
if (isReady && user) {
// Replace this code with however you'd like to handle an authenticated user
return (
<View>
<Text>User {user.id} is logged in.</Text>
</View>
);
}
}
You can use the authState
property from the Privy
client to determine if your user is authenticated or not.
public enum AuthState {
/// AuthState has not been determined yet, show loading
case notReady
/// User is unauthenticated
case unauthenticated
// User is authenticated and has an associated PrivyUser object
case authenticated(PrivyUser)
}
Auth state is exposed as an AsyncStream on the Privy object:
public protocol Privy {
/// An AsyncStream that emits auth state changes.
var authStateStream: AsyncStream<AuthState> { get }
}
There are various ways to determine user’s auth state, outlined below:
1. Directly grab the User
As a convenience, you can grab the user object directly from the Privy instance. If the user is not null, there is an authenticated user.
let privyUser = privy.user
if (privyUser != null) {
// User is authenticated
let linkedAccounts = privyUser.linkedAccounts
}
2. Grab the user’s current auth state
public protocol Privy {
/// The user's current auth state.
var authState: AuthState { get }
}
// Grab current auth state
if case .authenticated(let user) = privy.authState {
// User is authenticated. Grab the user's linked accounts
let linkedAccounts = user.linkedAccounts
}
3. Subscribe to auth state updates
let task = Task {
for await authState in privy.authStateStream {
switch authState {
case .authenticated(let user):
// User is authenticated. Grab the user's linked accounts
let linkedAccounts = user.linkedAccounts
case .notReady:
// Privy not yet ready. Call privy.awaitReady()
case .unauthenticated:
// User in not authenticated. Perhaps show login screen.
}
}
}
You can use the authState
property from the Privy
client to determine if your user is authenticated or not.
public sealed interface AuthState {
// AuthState has not been determined yet, show loading
public data object NotReady : AuthState
// User is unauthenticated
public data object Unauthenticated : AuthState
// User is authenticated and has an associated PrivyUser object
public data class Authenticated(val user: PrivyUser) : AuthState
}
Auth state is exposed as a StateFlow on the Privy object:
public interface Privy {
// A state flow that can be subscribed to for auth state updates
public val authState: StateFlow<AuthState>
}
Usage
There are various ways to determine a user’s auth state:
1. Directly grab the User
As a convenience, you can grab the user object directly from the Privy instance. If the user is not null, there is an authenticated user.
val privyUser = privy.user
if (privyUser != null) {
// User is authenticated
val linkedAccounts = privyUser.linkedAccounts
}
2. Grab the user’s current auth state
// Grab current auth state
val currentAuthState = privy.authState.value
if (currentAuthState is AuthState.Authenticated) {
// User is authenticated. Grab the user's linked accounts
val privyUser = currentAuthState.user
val linkedAccount = privyUser.linkedAccounts
}
3. Subscribe to auth state updates
coroutineScope.launch {
privy.authState.collectLatest { authState ->
when(authState) {
is AuthState.Authenticated -> {
// User is authenticated. Grab the user's linked accounts
val privyUser = authState.user
val userId = privyUser.linkedAccounts
}
AuthState.NotReady -> {
// Privy not yet ready. Call privy.awaitReady()
}
AuthState.Unauthenticated -> {
// User in not authenticated. Perhaps show login screen.
}
}
}
}
A Privy user’s authentication state is defined by the AuthState
enum below.
public enum AuthState
{
NotReady, // Privy has not yet finished initializing
Unauthenticated, // User is unauthenticated
Authenticated // User is authenticated
}
You can retrieve the user’s AuthState
at any time via:
Debug.Log(PrivyManager.Instance.AuthState);
You can also subscribe to AuthState
updates via:
PrivyManager.Instance.SetAuthStateChangeCallback(state =>
{
// User's AuthState has updated
Debug.Log(state);
});
Once your user has successfully authenticated, you can get a PrivyUser
object containing their account data via:
// User will be null if no user is authenticated
PrivyUser user = PrivyManager.Instance.User;
A user’s authentication state is described by the AuthState sealed class.
/// Base class representing different authentication states.
sealed class AuthState {
const AuthState();
}
/// Represents the initial state before authentication status is determined.
class NotReady extends AuthState {
const NotReady();
}
/// Represents the state when the user is not authenticated.
class Unauthenticated extends AuthState {
const Unauthenticated();
}
/// Represents the state when the user is authenticated.
class Authenticated extends AuthState {
final PrivyUser user;
/// Constructor accepting the authenticated user's details.
const Authenticated(this.user);
}
The current auth state and an auth state stream are accessible directly on the Privy object.
abstract interface class Privy {
// Get the current authentication state.
AuthState get currentAuthState;
// A stream for auth state updates.
Stream<AuthState> get authStateStream;
}
Accessing authentication state
There are various ways to determine user’s auth state, outlined below. Mix and match to fit the needs of your application.
1. Directly retrieve the user
As a convenience, you can grab the user object directly from the Privy instance. If the user is not null, there is an authenticated user.
final privyUser = privy.user;
if (privyUser != null) {
// User is authenticated
final linkedAccounts = privyUser.linkedAccounts;
}
2. Retrieve the current auth state
// Grab current auth state
final currentAuthState = privy.currentAuthState;
if (currentAuthState is Authenticated) {
// User is authenticated. Retrieve the associated user from the auth state.
final privyUser = currentAuthState.user;
final linkedAccounts = privyUser.linkedAccounts;
}
3. Subscribe to auth state updates
privy.authStateStream.listen((authState) {
switch (authState) {
case Authenticated():
// User is authenticated. Retrieve the user.
final privyUser = authState.user;
final userId = privyUser.linkedAccounts;
break;
case NotReady():
// Privy is not yet ready. Ensure Privy is initialized first.
break;
case Unauthenticated():
// User is not authenticated. You may want to show the login screen.
break;
}
});