0

I am making my first mern app and working on the login and registration part and I am stuck on a problem , the problem is that I made a verification route which pops up when someone registers but I want that no one should be able to go there via changing the URL, I know the concept of private routes but I don't know how to apply it here I mean what condition can I give to stop user to go there , I want that no one should be able to go there until they register.

Routes code:-

import Navbar from "./components/navbar/Navbar";
import Home from "./components/Home/Home";
import 'bootstrap/dist/css/bootstrap.min.css'
import Forgot_password from "./components/login_and_reg/Forgot_password/Forgot_password";
import Weather from "./components/Weather/Weather";
import Landing_page from './components/login_and_reg/Landing_page/Landing_page'
import {
    BrowserRouter as Router,
    Route,
    Routes,
    Navigate
  } from "react-router-dom";
import Verification from "./components/login_and_reg/Verification/Verification";
import Protected_routes from './components/Protected_routes'
import Protected_routes_2 from "./components/Protected_routes_2";
import { useSelector } from "react-redux";

function App() {
    const isloggedin = sessionStorage.getItem('isloggedin');
    const rememberMe = localStorage.getItem('rememberMe')

    
return (
    <>
            <Router>
                {/* <Navbar/> */}
                <Routes>
                    <Route path="/weather" element={
                        <Protected_routes_2>
                            <Weather/>
                        </Protected_routes_2>}></Route>
                    <Route exact path="/" element={<Protected_routes/>}></Route>
                    <Route path="/verify/:first_name/:email" element={<Verification/>}></Route>
                    <Route path="/forgot_password" element={<Forgot_password/>}></Route>
                    {/* <Route exact path="/home" element={<Protected_routes/>}></Route> */}


                </Routes>
            </Router>
                
            
            
        
    </>
);
}

export default App;
Samyak jain
  • 137
  • 1
  • 7

1 Answers1

0

If it's a popup/modal/dialog component

There's no need to give it a route. Just call it in the registration page. Doing so, only a registered user will see it.

If it's a well defined page

You can wrap your Verification page in a PrivateRoute with the condition only people coming from /register can access that page.

In this scenario, we assume your registration page is at /registrer and your verification at /verify.

To do that, you will have to:

  1. programmatically navigate to /verify. You'll have to do it while being inside your Register page. useNavigate hook will help.

  2. detect the previous path that has lead to your Verification page. and if it's not /register, redirect to 404 page or login. This logic goes inside the private route that will wrap your Verification page.

Couple of hints to achieve 2., see this related question and/or this react-router issue. They are all about getting previous path in React.

exaucae
  • 2,071
  • 1
  • 14
  • 24