useOktaAuth
Usage
useOktaAuth Hook
The useOktaAuth hook provides access to authentication-related properties and functions from the Okta context. It simplifies the process of accessing Okta authentication state and actions within functional components.
Usage in Unprotected Routes
The useOktaAuth hook can be particularly useful in unprotected routes, such as login or logout components, which are inherently not protected by authentication. These components are responsible for initiating the authentication process (login) or terminating an existing session (logout), and they typically need access to authentication-related properties and functions.
By using the useOktaAuth hook within these components, you can easily access the necessary authentication context without the need for additional setup or prop drilling. This simplifies the development process and ensures consistency in handling authentication actions across your application.
Here's an example of how the useOktaAuth hook can be utilized in an unprotected route component:
Example
To use the useOktaAuth hook, import it and call it within a functional component:
import React from "react";
import { View, Button } from "react-native";
import useOktaAuth from "okta-react-native-web";
const Logout = () => {
const { isAuthenticated, oktaLogout } = useOktaAuth();
const handleLogout = () => {
oktaLogout();
};
return (
<View>
<Button title="Logout" onPress={handleLogout} />
</View>
);
};
export default Logout;
In this example, the Logout component uses the useOktaAuth hook to access the isAuthenticated state and the oktaLogout function. When the user clicks the "Logout" button, the oktaLogout function is called to initiate the logout process, regardless of whether the user is currently authenticated or not.
By leveraging the useOktaAuth hook in unprotected routes, you can streamline the authentication flow and ensure a consistent user experience throughout your application.
Returns
| Prop | Type | Description |
|---|---|---|
| accessToken | AccessToken | undefined | Access token for the authenticated user. |
| isAuthenticated | boolean | Indicates whether the user is authenticated. |
| oktaClient | OktaAuth | undefined | Okta client instance for authentication operations. |
| oktaLogin | () => void | Function to initiate the Okta login process. |
| oktaLogout | () => void | Function to initiate the Okta logout process. |