I wanted to know whenever anyone logs into a site I’ve built as a security feature (much like Wordfence does for Wordpress sites) and it’s incredibly easy with Wappler so I thought I’d give a bit back to this great community and show how I did it.
In my login Server Action I have one Global which is in $_SERVER
and is called REMOTE_ADDR
. This will store the user’s IP address.
I signed up to the free service at https://ipstack.com/ which is an API for resolving IP addresses and giving location information. It’s very easy to use with the API Server Action in Wappler. You need to register for the free account to get your access_key.
The API Server Action should be set up like this:
ID: apiResolveIP
Output: ticked
Pass Errors: ticked
Url: http://api.ipstack.com/{{$_SERVER.REMOTE_ADDR}}
Method: GET
Data Type: Auto
Authorization: None
Then add one entry to the Query list.
Name: access_key
Value: <YOUR KEY PROVIDED BY IPSTACK.COM>
After entering all those details, click on the ‘Define API Schema’ button and then ‘Fetch Schema’ to get all the variables so it’s easy to use them in your email template.
Finally, set up your email settings using all the variables now at your disposal. My email template looks like this:
Someone has just logged into your website.
ID: {{security1.identity}}
Name: {{queryLoggedInUser.uFirstName}} {{queryLoggedInUser.uLastName}}
Email: {{queryLoggedInUser.uEmail}}
IP Address: {{$_SERVER.REMOTE_ADDR}}
Location: {{apiResolveIP.data.city}}, {{apiResolveIP.data.zip}}
Country: {{apiResolveIP.data.country_name}} ({{apiResolveIP.data.country_code}})
You’ll notice I added a condition so that I don’t receive lots of emails for any attempted logins. It’s only the successful ones which trigger the email.