This project demonstrates the implementation of Auth0 authentication in a React application. It provides a simple interface for users to log in and out using Auth0's authentication services.
- User authentication with Auth0
- Conditional rendering based on authentication status
- Display of user information when authenticated
Before you begin, ensure you have met the following requirements:
- Node.js installed on your local machine
- An Auth0 account and application set up
-
Clone the repository:
git clone https://github.com/adhikareeprayush/0Auth-Implementation-React.git
-
Navigate to the project directory:
cd 0Auth-Implementation-React
-
Install the dependencies:
npm install
-
Create a
.env
file in the root directory and add your Auth0 credentials:VITE_DOMAIN=your-auth0-domain VITE_CLIENT_ID=your-auth0-client-id
To run the application locally:
npm run dev
This will start the development server, and you can view the application in your web browser at http://localhost:5173
(or whichever port Vite assigns).
src/App.jsx
: Main application componentsrc/main.jsx
: Entry point of the application, sets up Auth0Provider
This is the main component of the application. It uses the useAuth0
hook to access authentication-related functions and user information.
import { useState } from 'react'
import { useAuth0 } from '@auth0/auth0-react'
function App() {
const { user, loginWithRedirect, isAuthenticated, logout } = useAuth0();
// ... (rest of the component code)
}
export default App
This file sets up the Auth0Provider and wraps the main App component.
import { createRoot } from 'react-dom/client'
import App from './App.jsx'
import { Auth0Provider } from '@auth0/auth0-react';
import './index.css'
createRoot(document.getElementById('root')).render(
<Auth0Provider
domain={import.meta.env.VITE_DOMAIN}
clientId={import.meta.env.VITE_CLIENT_ID}
authorizationParams={{
redirect_uri: window.location.origin
}}
>
<App />
</Auth0Provider>,
)
Contributions to this project are welcome. Please fork the repository and submit a pull request with your changes.
This project is open source and available under the MIT License.
If you have any questions or feedback, please open an issue in the GitHub repository.