Read session and user data with Expo
This guide demonstrates how to access active session and user data in your Expo application.
Session data example
The useAuth()
hook provides information about the current auth state, as well as helper methods to manage the current active session. The hook returns userId
, which can be used to protect your routes, as shown in the following example:
import { useAuth } from '@clerk/clerk-expo'
import { Text } from 'react-native'
export default function UseAuthExample() {
const { isLoaded, isSignedIn, userId, sessionId, getToken } = useAuth()
if (!isLoaded) {
return <Text>Loading...</Text>
}
if (!isSignedIn) {
// You could also add a redirect to the sign-in page here
return <Text>Sign in to view this page</Text>
}
return (
<Text>
Hello, {userId}! Your current active session is {sessionId}.
</Text>
)
}
User data example
The useUser()
hook enables you to access the User
object, which contains the current user's data such as their full name.
The following example demonstrates how to use useUser()
to check if the user is signed in and display their first name:
import { useUser } from '@clerk/clerk-expo'
import { Text } from 'react-native'
export default function Example() {
const { isSignedIn, user, isLoaded } = useUser()
if (!isLoaded) {
return <Text>Loading...</Text>
}
if (!isSignedIn) {
return <Text>Sign in to view this page</Text>
}
return <Text>Hello {user.firstName}!</Text>
}
Feedback
Last updated on