Skip to content

Commit

Permalink
Default to us if location cannot be found
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 14, 2024
1 parent 92a2f70 commit 18e0826
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions resources/views/auth/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,43 @@

<script>
let phoneInputField = document.querySelector("#phone");
let update_location_success = false;
let phoneInput = window.intlTelInput(phoneInputField, {
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js",
initialCountry: "auto",
geoIpLookup: function (success, failure) {
data = {
ip: "{{ request()->ip() }}"
};
try {
fetch("https://ipinfo.io/json?token=cbcffb988673b9", {
headers: {
"Accept": "application/json",
},
//if blocked by net::ERR_BLOCKED_BY_CLIENT return us
mode: "cors",
}).then((resp) => {
if (resp.ok) {
return resp.json();
}
return Promise.reject("Failed to fetch location from IP");
}).then((data) => {
let countryCode = data.country;
success(countryCode);
update_location_success = true;
}).catch((err) => {
console.error(err);
failure("NG");
});
} catch (error) {
console.error(error);
failure("NG");
}
},
});
if (!update_location_success) {
// Default to US if we can't get the location
phoneInput.setCountry("us");
}
// When we select a country, update the location field
phoneInputField.addEventListener("countrychange", function () {
let countryData = phoneInput.getSelectedCountryData();
Expand Down

0 comments on commit 18e0826

Please sign in to comment.