@auth0/auth0-react is a library that provides React hooks and components to integrate Auth0 authentication and authorization into React applications. It simplifies the process of adding authentication, handling user sessions, and securing routes.
What are @auth0/auth0-react's main functionalities?
Authentication
This feature allows you to integrate Auth0 authentication into your React application. The Auth0Provider component wraps your application and provides authentication context. The useAuth0 hook is used to access authentication methods like loginWithRedirect and logout.
This feature allows you to access and display the authenticated user's profile information. The useAuth0 hook provides the user object, which contains details like name, email, and profile picture.
import { useAuth0 } from '@auth0/auth0-react';
const UserProfile = () => {
const { user, isAuthenticated } = useAuth0();
if (!isAuthenticated) {
return <div>Please log in to see your profile.</div>;
}
return (
<div>
<img src={user.picture} alt={user.name} />
<h2>{user.name}</h2>
<p>{user.email}</p>
</div>
);
};
Securing Routes
This feature allows you to secure specific routes in your React application. The withAuthenticationRequired higher-order component ensures that only authenticated users can access the protected routes. If a user is not authenticated, they will be redirected to the login page.
react-oauth is a library that provides OAuth2 authentication for React applications. It supports multiple OAuth providers and offers hooks and components for integrating authentication. Compared to @auth0/auth0-react, react-oauth is more generic and can be used with various OAuth providers, whereas @auth0/auth0-react is specifically designed for Auth0.
react-aad-msal is a library for integrating Microsoft Azure Active Directory (AAD) authentication into React applications. It provides hooks and components for handling authentication and user sessions. Compared to @auth0/auth0-react, react-aad-msal is tailored for Microsoft AAD, while @auth0/auth0-react is focused on Auth0.
react-firebase-auth is a library that simplifies Firebase authentication in React applications. It provides hooks and components for managing user authentication and sessions with Firebase. Compared to @auth0/auth0-react, react-firebase-auth is specific to Firebase, whereas @auth0/auth0-react is specific to Auth0.
If you're using an existing application, verify that you have configured the following settings in your Single Page Application:
Click on the "Settings" tab of your application's page.
Scroll down and click on the "Show Advanced Settings" link.
Under "Advanced Settings", click on the "OAuth" tab.
Ensure that "JsonWebToken Signature Algorithm" is set to RS256 and that "OIDC Conformant" is enabled.
Next, configure the following URLs for your application under the "Application URIs" section of the "Settings" page:
Allowed Callback URLs: http://localhost:3000
Allowed Logout URLs: http://localhost:3000
Allowed Web Origins: http://localhost:3000
These URLs should reflect the origins that your application is running on. Allowed Callback URLs may also include a path, depending on where you're handling the callback.
Take note of the Client ID and Domain values under the "Basic Information" section. You'll need these values in the next step.
Configure the SDK
Configure the SDK by wrapping your application in Auth0Provider:
Use the useAuth0 hook in your components to access authentication state (isLoading, isAuthenticated and user) and authentication methods (loginWithRedirect and logout):
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?
This project is licensed under the MIT license. See the LICENSE file for more info.
Auth0 SDK for React Single Page Applications (SPA)
The npm package @auth0/auth0-react receives a total of 366,715 weekly downloads. As such, @auth0/auth0-react popularity was classified as popular.
We found that @auth0/auth0-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 45 open source maintainers collaborating on the project.
Package last updated on 11 Dec 2023
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.