in fulltext-search-firestore/public/index.js [47:76]
function searchAlgoliaAuthenticated(query) {
var client;
var index;
// [START search_index_secure]
// Use Firebase Authentication to request the underlying token
return firebase.auth().currentUser.getIdToken()
.then(function(token) {
// The token is then passed to our getSearchKey Cloud Function
return fetch('https://us-central1-' + PROJECT_ID + '.cloudfunctions.net/getSearchKey/', {
headers: { Authorization: 'Bearer ' + token }
});
})
.then(function(response) {
// The Fetch API returns a stream, which we convert into a JSON object.
return response.json();
})
.then(function(data) {
// Data will contain the restricted key in the `key` field.
client = algoliasearch(ALGOLIA_APP_ID, data.key);
index = client.initIndex('notes');
// Perform the search as usual.
return index.search({query});
})
.then(function(responses) {
// Finally, use the search 'hits' returned from Algolia.
return responses.hits;
});
// [END search_index_secure]
}