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 token

  • false - 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 authentication

  • false - 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

  • null when 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:

  1. Redirects user to Deva login page

  2. User authenticates

  3. Redirects back to your app

  4. SDK exchanges code for tokens

  5. isAuthenticated becomes true


logout

Type: () => Promise<void>

Revokes tokens and clears authentication state.

Usage: End user session

What happens:

  1. Revokes tokens on server

  2. Clears local session data

  3. isAuthenticated becomes false

  4. user becomes null


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:


Last updated