How do you integrate authentication with React-Router

0 votes
With the help of code tell me How do you integrate authentication with React-Router?
Feb 23 in Node-js by anonymous
• 19,190 points
63 views

1 answer to this question.

0 votes

To integrate authentication with React Router, follow these precise steps:

1. Install Dependencies

Ensure you have React Router installed:

npm install react-router-dom

2. Create Authentication Context (auth.js)

Manage authentication state using React Context:

import { createContext, useContext, useState } from "react";

const AuthContext = createContext();

export const AuthProvider = ({ children }) => {

  const [user, setUser] = useState(null);

  const login = (userData) => setUser(userData);

  const logout = () => setUser(null);

  return (

    <AuthContext.Provider value={{ user, login, logout }}>

      {children}

    </AuthContext.Provider>

  );

};

export const useAuth = () => useContext(AuthContext);

3. Create Protected Route Component

Restrict access to authenticated users:

import { Navigate } from "react-router-dom";

import { useAuth } from "./auth";

const ProtectedRoute = ({ children }) => {

  const { user } = useAuth();

  return user ? children : <Navigate to="/login" />;

};

export default ProtectedRoute;

4. Define Routes in App.js

Use React Router with authentication logic:

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

import { AuthProvider } from "./auth";

import ProtectedRoute from "./ProtectedRoute";

import Home from "./Home";

import Login from "./Login";

import Dashboard from "./Dashboard";

const App = () => (

  <AuthProvider>

    <Router>

      <Routes>

        <Route path="/" element={<Home />} />

        <Route path="/login" element={<Login />} />

        <Route 

          path="/dashboard" 

          element={<ProtectedRoute><Dashboard /></ProtectedRoute>} 

        />

      </Routes>

    </Router>

  </AuthProvider>

);

export default App;

5. Handle Login and Logout (Login.js)

Simulate login authentication:

import { useAuth } from "./auth";

import { useNavigate } from "react-router-dom";

const Login = () => {

  const { login } = useAuth();

  const navigate = useNavigate();

  const handleLogin = () => {

    login({ name: "User" }); 

    navigate("/dashboard"); 

  };

  return <button onClick={handleLogin}>Login</button>;

};

export default Login;

answered Feb 24 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you handle nested dynamic routes with React-Router?

Handling Nested Dynamic Routes in React Router ...READ MORE

answered Feb 24 in Node-js by Kavya
73 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I apply HTML5 Validation with React?

You can utilize the built-in HTML5 form ...READ MORE

answered Feb 12 in Node-js by Kavya
78 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do you implement breadcrumbs in a React-Router app?

Breadcrumbs help users navigate by showing the ...READ MORE

answered Feb 24 in Node-js by Kavya
62 views
0 votes
1 answer

How do you handle scroll restoration in a React-Router app?

By default, React Router does not restore ...READ MORE

answered Feb 24 in Node-js by Kavya
75 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP