šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Book a DemoInstallSign in
Socket

@auth0/auth0-react

Package Overview
Dependencies
Maintainers
45
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@auth0/auth0-react

Auth0 SDK for React Single Page Applications (SPA)

2.0.0
Source
npm
Version published
Weekly downloads
617K
5.64%
Maintainers
45
Weekly downloads
Ā 
Created

What is @auth0/auth0-react?

@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.

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>
);

Other packages similar to @auth0/auth0-react

Keywords

auth0

FAQs

Package last updated on 19 Jan 2023

Did you know?

Socket

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.

Install

Related posts