Skip to content

Commit c25e7a1

Browse files
Adds tests in demo app for service worker compatibility with Firebase Auth (#571)
* Adds tests in demo app for service worker compatibility with Firebase Auth. This modifies the service worker to append the ID token in every fetch request via a custom HTTP header. Using Functions with Hosting, an HTTP endpoint was setup which will parse the ID token from the header and verify it using Admin SDK and then return a status response. A button is added to the demo app to test this. When a user is signed in, the user's uid is returned, otherwise an error is shown. * [AUTOMATED]: License Headers * Fixes comment. * Renames /echoIdToken endpoint to /checkIfAuthenticated.
1 parent 9d00c6f commit c25e7a1

File tree

10 files changed

+3572
-8
lines changed

10 files changed

+3572
-8
lines changed

packages/auth/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ dist/
1616
# Local config.
1717
**/.firebaserc
1818
demo/public/config.js
19+
demo/functions/node_modules/

packages/auth/buildtools/run_demo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ cp ../firebase/firebase-auth.js demo/public/dist/firebase-auth.js
3434
cp ../firebase/firebase-database.js demo/public/dist/firebase-database.js
3535
# Serve demo app.
3636
cd demo
37-
`yarn bin`/firebase serve
37+
`yarn bin`/firebase serve --only hosting,functions

packages/auth/demo/firebase.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"public": "public",
77
"rewrites": [
88
{
9-
"source": "**",
10-
"destination": "/index.html"
9+
"source": "/checkIfAuthenticated",
10+
"function": "checkIfAuthenticated"
1111
}
1212
]
1313
}

packages/auth/demo/functions/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright 2018 Google Inc.
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+
17+
/**
18+
* @fileoverview Defines the HTTP endpoints hosted via firebase functions which
19+
* are used to test service worker functionality for Firebase Auth via demo
20+
* app.
21+
*/
22+
23+
const functions = require('firebase-functions');
24+
const admin = require('firebase-admin');
25+
26+
admin.initializeApp(functions.config().firebase);
27+
28+
exports.checkIfAuthenticated = functions.https.onRequest((req, res) => {
29+
const idToken = req.get('x-id-token');
30+
res.setHeader('Content-Type', 'application/json');
31+
if (idToken) {
32+
admin.auth().verifyIdToken(idToken)
33+
.then((decodedIdToken) => {
34+
res.status(200).send(JSON.stringify({uid: decodedIdToken.sub}));
35+
})
36+
.catch((error) => {
37+
res.status(400).send(JSON.stringify({error: error.code}));
38+
});
39+
} else {
40+
res.status(403).send(JSON.stringify({error: 'Unauthorized access'}));
41+
}
42+
});

0 commit comments

Comments
 (0)