Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@auth0/auth0-react
Advanced tools
@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.
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.
import { Auth0Provider, useAuth0 } from '@auth0/auth0-react';
const App = () => (
<Auth0Provider
domain="YOUR_DOMAIN"
clientId="YOUR_CLIENT_ID"
redirectUri={window.location.origin}
>
<YourComponent />
</Auth0Provider>
);
const YourComponent = () => {
const { loginWithRedirect, logout, isAuthenticated } = useAuth0();
return (
<div>
{!isAuthenticated && (
<button onClick={() => loginWithRedirect()}>Log in</button>
)}
{isAuthenticated && (
<button onClick={() => logout({ returnTo: window.location.origin })}>Log out</button>
)}
</div>
);
};
User Profile
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.
import { useAuth0, withAuthenticationRequired } from '@auth0/auth0-react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
const ProtectedRoute = ({ component, ...args }) => (
<Route
component={withAuthenticationRequired(component, {
onRedirecting: () => <div>Loading...</div>,
})}
{...args}
/>
);
const App = () => (
<Router>
<Switch>
<Route path="/public" component={PublicComponent} />
<ProtectedRoute path="/protected" component={ProtectedComponent} />
</Switch>
</Router>
);
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.
📚 Documentation - 🚀 Getting Started - 💻 API Reference - 💬 Feedback
Using npm
npm install @auth0/auth0-react
Using yarn
yarn add @auth0/auth0-react
Create a Single Page Application in the Auth0 Dashboard.
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.
- Ensure that "Token Endpoint Authentication Method" under "Application Properties" is set to "None"
- 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:
http://localhost:3000
http://localhost:3000
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 by wrapping your application in Auth0Provider
:
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Auth0Provider } from '@auth0/auth0-react';
import App from './App';
ReactDOM.render(
<Auth0Provider
domain="YOUR_AUTH0_DOMAIN"
clientId="YOUR_AUTH0_CLIENT_ID"
authorizationParams={{
redirect_uri: window.location.origin,
}}
>
<App />
</Auth0Provider>,
document.getElementById('app')
);
Use the useAuth0
hook in your components to access authentication state (isLoading
, isAuthenticated
and user
) and authentication methods (loginWithRedirect
and logout
):
// src/App.js
import React from 'react';
import { useAuth0 } from '@auth0/auth0-react';
function App() {
const { isLoading, isAuthenticated, error, user, loginWithRedirect, logout } =
useAuth0();
if (isLoading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Oops... {error.message}</div>;
}
if (isAuthenticated) {
return (
<div>
Hello {user.name}{' '}
<button onClick={() => logout({ returnTo: window.location.origin })}>
Log out
</button>
</div>
);
} else {
return <button onClick={loginWithRedirect}>Log in</button>;
}
}
export default App;
For more code samples on how to integrate auth0-react SDK in your React application, have a look at our examples.
Explore public API's available in auth0-react.
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
To provide feedback or report a bug, please raise an issue on our issue tracker.
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.
FAQs
Auth0 SDK for React Single Page Applications (SPA)
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.
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.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.