Skip to content

Commit 335a321

Browse files
authored
Merge pull request #169 from BuildFire/reove-static-key
fix(static-key): remove static google api key
2 parents 6594e18 + ddd4536 commit 335a321

6 files changed

Lines changed: 63 additions & 5 deletions

File tree

plugin.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@
2929

3030
"features": [{ "name": "Geo" }],
3131
"languages": ["en"],
32+
"usesApiKeys": [
33+
{ "name": "googleMapKey" }
34+
],
3235
"webpack": 8080
3336
}

src/control/content/components/LocationsActionBar.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ class LocationsActionBar extends React.Component {
7373
// use google maps api to fetch them async
7474
// otherwise just return the location
7575
if (!address_lat || !address_lng) {
76+
const { apiKeys } = buildfire.getContext();
77+
const { googleMapKey } = apiKeys;
7678
promises.push(
7779
new Promise((resolve, reject) => {
7880
const formattedAddress = name
7981
.replace(/,/g, "")
8082
.replace(/ /g, "+");
81-
const url = `https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyBOp1GltsWARlkHhF1H_cb6xtdR1pvNDAk&address=${formattedAddress}`;
83+
const url = `https://maps.googleapis.com/maps/api/geocode/json?key=${googleMapKey}&address=${formattedAddress}`;
8284
// get geodata from google api
8385
fetch(url)
8486
.then((response) => response.json())

src/control/content/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<script src="../../../../scripts/buildfire/components/pluginInstance/sortableList.js"></script>
1717
<script src="../../../../scripts/buildfire/components/actionItems/sortableList.js"></script>
1818
<script src="../../../../scripts/tinymce/tinymce.min.js"></script>
19-
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyBOp1GltsWARlkHhF1H_cb6xtdR1pvNDAk"></script>
2019
<script src="../../../../scripts/buildfire/services/searchEngine/searchEngine.js"></script>
2120
</head>
2221
<body>

src/control/content/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,35 @@ import Content from './content';
44

55
const container = document.getElementById('mount');
66

7+
const initGoogleMapsSDK = () => {
8+
const { apiKeys } = buildfire.getContext();
9+
const { googleMapKey } = apiKeys;
10+
const script = document.createElement('script');
11+
script.type = 'text/javascript';
12+
script.src = `https://maps.googleapis.com/maps/api/js?libraries=places&key=${
13+
googleMapKey
14+
}`;
15+
script.onload = () => {
16+
console.info("Successfully loaded Google's Maps SDK.");
17+
};
18+
script.onerror = () => {
19+
buildfire.dialog.alert({
20+
title: 'Error',
21+
message: 'Failed to load Google Maps API.',
22+
});
23+
};
24+
window.gm_authFailure = () => {
25+
buildfire.dialog.alert({
26+
title: 'Error',
27+
message: 'Failed to load Google Maps API.',
28+
});
29+
};
30+
document.head.appendChild(script);
31+
};
32+
733
function renderApp() {
34+
35+
initGoogleMapsSDK();
836
render(<Content />, container);
937
}
1038

src/widget/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<head>
44
<meta name="viewport" content="width=device-width,initial-scale=1">
55
<meta charset="utf-8">
6-
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBOp1GltsWARlkHhF1H_cb6xtdR1pvNDAk&sensor=false"></script>
76
<script src="../../../scripts/_bundles/buildfire_lightcarousel.min.js"></script>
87
<link rel="stylesheet" href="https://cdn.quilljs.com/1.0.0/quill.snow.css"/>
98
<script src="../../../scripts/buildfire/components/toast/toast.js"></script>

src/widget/js/app.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ import "../js/shared/strings";
1818

1919
window.strings = new buildfire.services.Strings("en-us", stringsConfig);
2020

21+
const initGoogleMapsSDK = () => {
22+
const { apiKeys } = buildfire.getContext();
23+
const { googleMapKey } = apiKeys;
24+
const script = document.createElement('script');
25+
script.type = 'text/javascript';
26+
script.src = `https://maps.googleapis.com/maps/api/js?key=${
27+
googleMapKey
28+
}&sensor=false`;
29+
script.onload = () => {
30+
console.info("Successfully loaded Google's Maps SDK.");
31+
};
32+
script.onerror = () => {
33+
buildfire.dialog.alert({
34+
title: 'Error',
35+
message: 'Failed to load Google Maps API.',
36+
});
37+
};
38+
window.gm_authFailure = () => {
39+
buildfire.dialog.alert({
40+
title: 'Error',
41+
message: 'Failed to load Google Maps API.',
42+
});
43+
};
44+
document.head.appendChild(script);
45+
};
46+
2147
window.app = {
2248
goBack: null,
2349
settings: {
@@ -115,7 +141,7 @@ window.app = {
115141
window.app.loadPage(0,50,(err,places)=>{
116142
window.listView.setItems(places);
117143
window.app.state.places=places;
118-
});
144+
});
119145
},
120146
loadPage: (page, pageSize, callback) => {
121147
let places = [];
@@ -194,7 +220,7 @@ window.app = {
194220
latitude: window.app.state.location.lat,
195221
longitude: window.app.state.location.lng,
196222
};
197-
223+
198224
let destination = {
199225
latitude: address.lat,
200226
longitude: address.lng
@@ -223,6 +249,7 @@ window.app = {
223249
},
224250
init: (placesCallback, positionCallback) => {
225251

252+
initGoogleMapsSDK();
226253
let userLocation = JSON.parse(localStorage.getItem("user_location"));
227254
buildfire.datastore.get(
228255
window.app.settings.placesTag,

0 commit comments

Comments
 (0)