1
- const functions = require ( "firebase-functions" ) ;
2
- const firestore = require ( "firebase-functions/lib/providers/firestore" ) ;
3
- const Firestore = require ( "@google-cloud/firestore" ) ;
4
-
5
- const algoliasearch = require ( "algoliasearch" ) ;
1
+ /**
2
+ * Copyright 2017 Google Inc. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the 'License');
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an 'AS IS' BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ const functions = require ( 'firebase-functions' ) ;
17
+ const algoliasearch = require ( 'algoliasearch' ) ;
6
18
7
19
// [START init_algolia]
8
20
// Initialize Algolia, requires installing Algolia dependencies:
@@ -11,25 +23,20 @@ const algoliasearch = require("algoliasearch");
11
23
// App ID and API Key are stored in functions config variables
12
24
const ALGOLIA_ID = functions . config ( ) . algolia . app_id ;
13
25
const ALGOLIA_ADMIN_KEY = functions . config ( ) . algolia . api_key ;
26
+ const ALGOLIA_SEARCH_KEY = functions . config ( ) . algolia . search_key ;
14
27
15
- const ALGOLIA_INDEX_NAME = " notes" ;
28
+ const ALGOLIA_INDEX_NAME = ' notes' ;
16
29
const client = algoliasearch ( ALGOLIA_ID , ALGOLIA_ADMIN_KEY ) ;
17
30
// [END init_algolia]
18
31
19
- // Initialize Firestore
20
- const firestoreOptions = {
21
- projectId : process . env . GCLOUD_PROJECT
22
- } ;
23
- const db = new Firestore ( firestoreOptions ) ;
24
-
25
32
// [START update_index_function]
26
33
// Update the search index every time a blog post is written.
27
- exports . onNoteCreated = firestore . document ( " notes/{noteId}" ) . onCreate ( event => {
34
+ exports . onNoteCreated = functions . firestore . document ( ' notes/{noteId}' ) . onCreate ( event => {
28
35
// Get the note document
29
36
const note = event . data . data ( ) ;
30
37
31
- // Add an " objectID" field which Algolia requires
32
- note . objectID = event . params . postId ;
38
+ // Add an ' objectID' field which Algolia requires
39
+ note . objectID = event . params . noteId ;
33
40
34
41
// Write to the algolia index
35
42
const index = client . initIndex ( ALGOLIA_INDEX_NAME ) ;
@@ -38,65 +45,64 @@ exports.onNoteCreated = firestore.document("notes/{noteId}").onCreate(event => {
38
45
// [END update_index_function]
39
46
40
47
// [START get_firebase_user]
41
- const admin = require ( "admin" ) ;
48
+ const admin = require ( 'firebase-admin' ) ;
49
+ admin . initializeApp ( functions . config ( ) . firebase ) ;
42
50
43
51
function getFirebaseUser ( req , res , next ) {
44
- console . log ( " Check if request is authorized with Firebase ID token" ) ;
52
+ console . log ( ' Check if request is authorized with Firebase ID token' ) ;
45
53
46
- if (
47
- ! req . headers . authorization ||
48
- ! req . headers . authorization . startsWith ( "Bearer " )
49
- ) {
54
+ if ( ! req . headers . authorization
55
+ || ! req . headers . authorization . startsWith ( 'Bearer ' ) ) {
50
56
console . error (
51
- " No Firebase ID token was passed as a Bearer token in the Authorization header." ,
52
- " Make sure you authorize your request by providing the following HTTP header:" ,
53
- " Authorization: Bearer <Firebase ID Token>"
57
+ ' No Firebase ID token was passed as a Bearer token in the Authorization header.' ,
58
+ ' Make sure you authorize your request by providing the following HTTP header:' ,
59
+ ' Authorization: Bearer <Firebase ID Token>'
54
60
) ;
55
- res . status ( 403 ) . send ( " Unauthorized" ) ;
61
+ res . status ( 403 ) . send ( ' Unauthorized' ) ;
56
62
return ;
57
63
}
58
64
59
65
let idToken ;
60
66
if (
61
67
req . headers . authorization &&
62
- req . headers . authorization . startsWith ( " Bearer " )
68
+ req . headers . authorization . startsWith ( ' Bearer ' )
63
69
) {
64
- console . log ( " Found 'Authorization' header" ) ;
65
- idToken = req . headers . authorization . split ( " Bearer " ) [ 1 ] ;
70
+ console . log ( ' Found 'Authorization ' header' ) ;
71
+ idToken = req . headers . authorization . split ( ' Bearer ' ) [ 1 ] ;
66
72
}
67
73
68
74
admin
69
75
. auth ( )
70
76
. verifyIdToken ( idToken )
71
77
. then ( decodedIdToken => {
72
- console . log ( " ID Token correctly decoded" , decodedIdToken ) ;
78
+ console . log ( ' ID Token correctly decoded' , decodedIdToken ) ;
73
79
req . user = decodedIdToken ;
74
80
next ( ) ;
75
81
} )
76
82
. catch ( error => {
77
- console . error ( " Error while verifying Firebase ID token:" , error ) ;
78
- res . status ( 403 ) . send ( " Unauthorized" ) ;
83
+ console . error ( ' Error while verifying Firebase ID token:' , error ) ;
84
+ res . status ( 403 ) . send ( ' Unauthorized' ) ;
79
85
} ) ;
80
86
}
81
87
// [END get_firebase_user]
82
88
83
89
// [START get_algolia_user_token]
84
90
// This complex HTTP function will be created as an ExpressJS app:
85
91
// https://expressjs.com/en/4x/api.html
86
- const app = require ( " express" ) ( ) ;
92
+ const app = require ( ' express' ) ( ) ;
87
93
88
94
// We'll enable CORS support to allow the function to be invoked
89
95
// from our app client-side.
90
- app . use ( require ( " cors" ) ( { origin : true } ) ) ;
96
+ app . use ( require ( ' cors' ) ( { origin : true } ) ) ;
91
97
92
- // Then we'll also use a special " getFirebaseUser" middleware which
98
+ // Then we'll also use a special ' getFirebaseUser' middleware which
93
99
// verifies the Authorization header and adds a `user` field to the
94
100
// incoming request:
95
101
// https://gist.github.com/abehaskins/832d6f8665454d0cd99ef08c229afb42
96
102
app . use ( getFirebaseUser ) ;
97
103
98
104
// Add a route handler to the app to generate the secured key
99
- app . get ( "/" , ( req , res ) => {
105
+ app . get ( '/' , ( req , res ) => {
100
106
// Create the params object as described in the Algolia documentation:
101
107
// https://www.algolia.com/doc/guides/security/api-keys/#generating-api-keys
102
108
const params = {
@@ -109,11 +115,11 @@ app.get("/", (req, res) => {
109
115
// Call the Algolia API to generate a unique key based on our search key
110
116
const key = client . generateSecuredApiKey ( ALGOLIA_SEARCH_KEY , params ) ;
111
117
112
- // Then return this key as {key: " ...key" }
118
+ // Then return this key as {key: ' ...key' }
113
119
res . json ( { key } ) ;
114
120
} ) ;
115
121
116
122
// Finally, pass our ExpressJS app to Cloud Functions as a function
117
- // called " getSearchKey" ;
123
+ // called ' getSearchKey' ;
118
124
exports . getSearchKey = functions . https . onRequest ( app ) ;
119
125
// [END get_algolia_user_token]
0 commit comments