|
| 1 | +/** |
| 2 | + * Copyright 2016 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 | +'use strict'; |
| 17 | + |
| 18 | +// Initializes the Demo. |
| 19 | +function Demo() { |
| 20 | + document.addEventListener('DOMContentLoaded', function() { |
| 21 | + // Shortcuts to DOM Elements. |
| 22 | + this.signInButton = document.getElementById('demo-sign-in-button'); |
| 23 | + this.signOutButton = document.getElementById('demo-sign-out-button'); |
| 24 | + this.responseContainer = document.getElementById('demo-response'); |
| 25 | + this.urlContainer = document.getElementById('demo-url'); |
| 26 | + this.helloFunctionUrl = 'https://us-central1-' + config.authDomain.split('.')[0] + '.cloudfunctions.net/authorizedHello/'; |
| 27 | + this.signedOutCard = document.getElementById('demo-signed-out-card'); |
| 28 | + this.signedInCard = document.getElementById('demo-signed-in-card'); |
| 29 | + |
| 30 | + // Bind events. |
| 31 | + this.signInButton.addEventListener('click', this.signIn.bind(this)); |
| 32 | + this.signOutButton.addEventListener('click', this.signOut.bind(this)); |
| 33 | + firebase.auth().onAuthStateChanged(this.onAuthStateChanged.bind(this)); |
| 34 | + }.bind(this)); |
| 35 | +} |
| 36 | + |
| 37 | +// Triggered on Firebase auth state change. |
| 38 | +Demo.prototype.onAuthStateChanged = function(user) { |
| 39 | + if (user) { |
| 40 | + this.urlContainer.textContent = this.helloFunctionUrl; |
| 41 | + this.signedOutCard.style.display = 'none'; |
| 42 | + this.signedInCard.style.display = 'block'; |
| 43 | + this.startFunctionsRequest(); |
| 44 | + } else { |
| 45 | + this.signedOutCard.style.display = 'block'; |
| 46 | + this.signedInCard.style.display = 'none'; |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +// Initiates the sign-in flow using LinkedIn sign in in a popup. |
| 51 | +Demo.prototype.signIn = function() { |
| 52 | + firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider()); |
| 53 | +}; |
| 54 | + |
| 55 | +// Signs-out of Firebase. |
| 56 | +Demo.prototype.signOut = function() { |
| 57 | + firebase.auth().signOut(); |
| 58 | +}; |
| 59 | + |
| 60 | +// Does an authenticated request to a Firebase Functions endpoint. |
| 61 | +Demo.prototype.startFunctionsRequest = function() { |
| 62 | + firebase.auth().currentUser.getToken().then(function(token) { |
| 63 | + console.log('Sending request to', this.helloFunctionUrl, 'with ID token', token); |
| 64 | + var req = new XMLHttpRequest(); |
| 65 | + req.onload = function() { |
| 66 | + this.responseContainer.innerText = req.responseText; |
| 67 | + }.bind(this); |
| 68 | + req.onerror = function() { |
| 69 | + this.responseContainer.innerText = 'There was an error'; |
| 70 | + }.bind(this); |
| 71 | + req.open('GET', this.helloFunctionUrl, true); |
| 72 | + req.setRequestHeader('Authorization', 'Bearer ' + token); |
| 73 | + req.send(); |
| 74 | + }.bind(this)); |
| 75 | +}; |
| 76 | + |
| 77 | +// Load the demo. |
| 78 | +window.demo = new Demo(); |
0 commit comments