useDeva
React hook for accessing authentication state and user information.
Import
import { useDeva } from "@bitplanet/deva-sdk";Relationship with DevaProvider
useDeva and DevaProvider work together as a React Context pattern:
DevaProvider (Component):
Wraps your application at the root level
Manages OAuth authentication flow
Handles token refresh automatically
Stores authentication state in React Context
Provides configuration (clientId, redirectUri, env)
useDeva (Hook):
Consumes the Context created by DevaProvider
Provides a simplified API for child components
Returns only essential properties for application use
Cannot be used outside of DevaProvider
Relationship:
Think of it as:
DevaProvider = Authentication engine (does the work)
useDeva = Dashboard (shows you the results)
Learn more about DevaProvider →
What It Does
Provides access to:
User authentication status
User profile information
Login/logout functions
Access token for API calls
Must be used inside a DevaProvider component.
Basic Usage
Return Values
isAuthenticated
Type: boolean
Indicates whether the user is currently authenticated.
Values:
true- User has valid access tokenfalse- User not logged in or token expired
Usage: Check if user is logged in
isReady
Type: boolean Indicates whether the SDK has finished initializing.
Values:
true- SDK initialization complete, safe to check authenticationfalse- SDK still loading OpenID configuration
Usage: Check if SDK finished initializing
Important: Always check isReady before using isAuthenticated
user
Type: UserInfo | null
Complete user information including profile and persona data.
Contains:
Usage:
accessToken
Type: string | null
The current OAuth access token.
Values:
JWT string when authenticated
nullwhen not authenticated
Usage: Include in API requests
Note: SDK components automatically include the token. You only need this for custom API calls.
login
Type: () => Promise<void>
Initiates the OAuth 2.0 authentication flow.
Usage: Initiate OAuth login flow
What happens:
Redirects user to Deva login page
User authenticates
Redirects back to your app
SDK exchanges code for tokens
isAuthenticatedbecomestrue
logout
Type: () => Promise<void>
Revokes tokens and clears authentication state.
Usage: End user session
What happens:
Revokes tokens on server
Clears local session data
isAuthenticatedbecomesfalseuserbecomesnull
Common Patterns
Protected Route
User Profile Display
Login/Logout Toggle
Custom API Call
Important Notes
Must Use Inside DevaProvider
Always Check isReady First
Handle Null User
TypeScript
Full type definitions:
Related Documentation
Last updated