[Solved] Block an ip and redirect to another page

Hi, I need to block specific ip addresses and redirect to another page on my site. I have code to get the ip address, I use this on other parts of my site and it works. What I’m struggling with is to query the ip and redirect. I’ve created a collection ‘BlockedIP’ and have a field in it ‘ipAddress’. I’ve put the code in the site code so whatever page they land on it should redirect them. I’m getting no errors and the ip is being shown in the console so I guess it’s something to do with my lack of code experience that the query isn’t working! Here’s the code I’m using, any pointers or corrections are much appreciated!

$w.onReady( function () {
let ipadd =
fetch(‘https://extreme-ip-lookup.com/json’, {
method: ‘get’
})

    .then((httpResponse) => { 

if (httpResponse.ok) {
return httpResponse.json();
}
})

    .then((json) => { 

const ipaddress = json.query;
console.log(ipaddress);
return ipaddress;

    })   

    .then(() => { 
        wixData.query("BlockedIP") 
        .eq("ipAddress",ipadd) 
         .find() 
        .then( (results) => { 

let firstItem = results.items [0];
wixLocation.to(“/terms”)
})
. catch ( (err) => {
let errorMsg = err;
} );

})
})

https://support.wix.com/en/article/request-blocking-specific-ip-addresses

Thanks! Now I know why I couldn’t find anything about this, I thought this would easily be available. Voted anyway, hope this comes soon!

You have a small bug in your code, fix it and our functionality should work.
You need to pass the discovered IP to the last section that checks it against the collection, so your last section should look like so:

.then((ipaddress) => {
            wixData.query("BlockedIP")
                .eq("ipAddress", ipaddress)
                .find()
                .then((results) => {
                    let firstItem = results.items[0];
                    wixLocation.to("/terms")
                })
                .catch((err) => {
                     let errorMsg = err;
                });
        })
1 Like

Thanks Ohad for the quick correction of my reply post.

Also, is it possible to remove that request option if it isn’t appropriate now, or at least put this option as a workaround if it is still needed to be shown to users.

1 Like

Actually you have an uneeded extra “then” there. You can just use it like this:

.then((json) => {
      const ipaddress = json.query; 
      wixData.query("BlockedIP")
      .eq("ipAddress", ipaddress)
      .find()
      .then((results) => {
        if(results.items.length > 0) {
            wixLocation.to("/terms")
           }
        })
    })

Thanks @givemeawhisky @ohadlaufer @J. D. Code works great! Here’s the complete code I used. Thanks again all.

import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;
import {fetch} from ‘wix-fetch’;
$w.onReady( function () {

fetch('https://extreme-ip-lookup.com/json', { 
        method: 'get' 
    }) 
    .then((httpResponse) => { 

if (httpResponse.ok) {
return httpResponse.json();
}
})
.then((json) => {
let ipaddress = json.query;
wixData.query(“BlockedIP”)//use your collection name (case-sensitive)
.eq(“ipAddress”, ipaddress)//use your collection field key (type: text)
.find()
.then( (res) => {
if (res.items.length > 0){
wixLocation.to(“/blocked”);//redirect to blocked page be sure you have such a path
}
})
})
})

1 Like

Hi I used the same code but i am getting this error : @sailorcihan @jonatandor35 @givemeawhisky

“TypeError: Cannot read property ‘find’ of undefined at https://be65f7c1-d72c-4483-8ed7-3264ad73f3e8.dev.wix-code.com/pages/masterPage.js?viewMode=preview&instance=wixcode-dev.4b2c2c9d658072cce25597c5d0f66c0682ce242c.eyJpbnN0YW5jZUlkIjoiYmU2NWY3YzEtZDcyYy00NDgzLThlZDctMzI2NGFkNzNmM2U4IiwiY29ycmVsYXRpb25JZCI6Ijk1ZjFjNGI2LThmMjItNDhhOC05NWU3LTRjZWM3NWUyMGJmYSIsInNlc3Npb25VSWQiOiI1YjgyZDNkZS1lNWExLTQwMzUtYmMyYi00NzNhYWM1NDAzMGEiLCJpc1RlbXBsYXRlIjpmYWxzZSwic2lnbkRhdGUiOjE1ODA4MzkyNjg2MDUsInNpdGVNZW1iZXJJZCI6IjViODJkM2RlLWU1YTEtNDAzNS1iYzJiLTQ3M2FhYzU0MDMwYSIsInVpZCI6IjViODJkM2RlLWU1YTEtNDAzNS1iYzJiLTQ3M2FhYzU0MDMwYSIsImFwcERlZklkIjoiQ2xvdWRTaXRlRXh0ZW5zaW9uIiwiaXNBZG1pbiI6dHJ1ZSwibWV0YVNpdGVJZCI6IjNiODQ1YzlkLTQ4NDktNDVhZC04M2E4LWVjYjVkYTI1N2M4NyIsImNhY2hlIjpudWxsLCJleHBpcmF0aW9uRGF0ZSI6IjIwMjAtMDItMDRUMjI6MDE6MDguNjA1WiIsInNpdGVPd25lcklkIjoiNWI4MmQzZGUtZTVhMS00MDM1LWJjMmItNDczYWFjNTQwMzBhIiwiaW5zdGFuY2VUeXBlIjoiZGV2In0=&gridAppId=f4e212da-a3d7-4f7f-8b13-f849acab4192&empty-if-missing=true&exclude=wix-&module-name=masterPage&cacheKiller=1580840995029:28:31

}

This is the code :

import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;
import {fetch} from ‘wix-fetch’;
$w.onReady( function () {
fetch(‘https://extreme-ip-lookup.com/json’, {
method: ‘get’
})
.then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
}
})
.then((json) => {
let ipaddress = json.query;
wixData.query(“BlockedIP”)//use your collection name (case-sensitive)
.eq(“223.237.225.21”, ipaddress)//use your collection field key (type: text)
console.log(ipaddress)
.find()
.then( (res) => {
if (res.items.length > 0){
wixLocation.to(‘/blank-page’);//redirect to blocked page be sure you have such a path
}
})
})
})

Please check the code and understand it before using it as the original post was about querying a returned ip address against a dataset of ip addresses and not for just using a specific ip address.

You don’t need to change anything here as long as your dataset is called BlockedIP and you have the field with a key of ipAddress.

wixData.query("BlockedIP")//use your collection name (case-sensitive)
.eq("ipAddress", ipaddress)//use your collection field key (type: text)

See the Wix Data Query API and the eq. function for more info on using it.

Also, make sure that you have this page in your website so that it can go to it.

wixLocation.to('/blank-page');
//redirect to blocked page be sure you have such a path

Finally, this is an old post from last year and therefore will be CLOSED.
if you require more help then please post a new forum post.