1
import logo from './logo.svg';
import './App.css';
import $ from 'jquery'; 
import { BrowserRouter as Router,Route,Switch,Redirect } from "react-router-dom";
import Home from './Home';
import Login from './Login';

// Getting the country code from the user's IP

// List of countries we want to block
// To see this in action add your country code to the array
var blacklist = ['US', 'CA', 'UK','IN']

// Getting the country code from the user's IP
$.get("https://api.ipdata.co?api-key=97173fac13f575130327fc00f93b12fe62ea33afb45930438111a4ea", function (response) {

  // Checking if the user's country code is in the blacklist
  // You could inverse the logic here to use a whitelist instead
  if (blacklist.includes(response.country_code)) {
    <Redirect push to="/home" />


    alert('This content is not available at your location.');
  

  

    
    
  } else {
    alert("You're allowed to see this!")
  }
}, "jsonp");






function App() {
  return (
    <>
   
      <Router>
           
           <Switch>
             <Route exact path="/" component={Login}/>
             <Route exact path="/home" component={Home}/>
             <Redirect to="/"/>
             
            </Switch>
         </Router>
    </>
  
    
  );
}

export default App;

I am blocking the users from specific country using api of ipdata,I want that it should redirect me after showing the alert that this content is not avalible, but I am not getting redirected after I see the alert.When I use windows.location command that its redirecting me again and again very quickly.

1
  • please answer my question Commented Oct 7, 2022 at 5:58

1 Answer 1

1
import logo from './logo.svg';
import './App.css';
import $ from 'jquery'; 
import { BrowserRouter as Router,Route,Switch,Redirect } from "react-router-dom";
import Home from './Home';
import Login from './Login';

// Getting the country code from the user's IP

// List of countries we want to block
// To see this in action add your country code to the array
var blacklist = ['US', 'CA', 'UK','IN']

// Getting the country code from the user's IP
$.get("https://api.ipdata.co?api-key=97173fac13f575130327fc00f93b12fe62ea33afb45930438111a4ea", function (response) {

  // Checking if the user's country code is in the blacklist
  // You could inverse the logic here to use a whitelist instead
  if (blacklist.includes(response.country_code)) {
     if ( window.location!= "http://localhost:3000/home"){  
          window.location = "http://localhost:3000/home"}
     else{
        console.log("sorry")
     }
    alert('This content is not available at your location.');
  

  

    
    
  } else {
    alert("You're allowed to see this!")
  }
}, "jsonp");

basicly I just added a if condition you can check that out and now its working perfectly!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.