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. 19function 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.responseContainerCookie = document.getElementById('demo-response-cookie'); 26 this.urlContainer = document.getElementById('demo-url'); 27 this.urlContainerCookie = document.getElementById('demo-url-cookie'); 28 this.helloUserUrl = window.location.href + 'hello'; 29 this.downloadUrl = window.location.href + 'download'; 30 this.signedOutCard = document.getElementById('demo-signed-out-card'); 31 this.signedInCard = document.getElementById('demo-signed-in-card'); 32 33 // Bind events. 34 this.signInButton.addEventListener('click', this.signIn.bind(this)); 35 this.signOutButton.addEventListener('click', this.signOut.bind(this)); 36 firebase.auth().onAuthStateChanged(this.onAuthStateChanged.bind(this)); 37 }.bind(this)); 38} 39 40// Triggered on Firebase auth state change. 41Demo.prototype.onAuthStateChanged = function(user) { 42 if (user) { 43 this.urlContainer.textContent = this.helloUserUrl; 44 this.urlContainerCookie.textContent = this.helloUserUrl; 45 this.signedOutCard.style.display = 'none'; 46 this.signedInCard.style.display = 'block'; 47 this.startFunctionsRequest(); 48 this.startFunctionsCookieRequest(); 49 this.startDownloadRequest(); 50 } else { 51 this.signedOutCard.style.display = 'block'; 52 this.signedInCard.style.display = 'none'; 53 } 54}; 55 56// Initiates the sign-in flow using GoogleAuthProvider sign in in a popup. 57Demo.prototype.signIn = function() { 58 firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider()); 59}; 60 61// Signs-out of Firebase. 62Demo.prototype.signOut = function() { 63 firebase.auth().signOut(); 64 // clear the __session cookie 65 document.cookie = '__session='; 66}; 67 68// Does an authenticated request to a Firebase Functions endpoint using an Authorization header. 69Demo.prototype.startFunctionsRequest = function() { 70 firebase.auth().currentUser.getIdToken().then(function(token) { 71 console.log('Sending request to', this.helloUserUrl, 'with ID token in Authorization header.'); 72 var req = new XMLHttpRequest(); 73 req.onload = function() { 74 this.responseContainer.innerText = req.responseText; 75 }.bind(this); 76 req.onerror = function() { 77 this.responseContainer.innerText = 'There was an error'; 78 }.bind(this); 79 req.open('GET', this.helloUserUrl, true); 80 req.setRequestHeader('Authorization', 'Bearer ' + token); 81 req.send(); 82 }.bind(this)); 83}; 84 85// Does an authenticated request to a Firebase Functions endpoint using a __session cookie. 86Demo.prototype.startFunctionsCookieRequest = function() { 87 // Set the __session cookie. 88 firebase.auth().currentUser.getIdToken(true).then(function(token) { 89 // set the __session cookie 90 document.cookie = '__session=' + token + ';max-age=3600'; 91 92 console.log('Sending request to', this.helloUserUrl, 'with ID token in __session cookie.'); 93 var req = new XMLHttpRequest(); 94 req.onload = function() { 95 this.responseContainerCookie.innerText = req.responseText; 96 }.bind(this); 97 req.onerror = function() { 98 this.responseContainerCookie.innerText = 'There was an error'; 99 }.bind(this); 100 req.open('GET', this.helloUserUrl, true); 101 req.send(); 102 }.bind(this)); 103}; 104 105 106// Does an authorized request to /download 107Demo.prototype.startDownloadRequest = function() { 108 firebase.auth().currentUser.getIdToken().then(function(token) { 109 console.log('Sending request to', this.downlaodUrl, 'with ID token in Authorization header.'); 110 var req = new XMLHttpRequest(); 111 req.onload = function() { 112 this.responseContainer.innerText = req.responseText; 113 }.bind(this); 114 req.onerror = function() { 115 this.responseContainer.innerText = 'There was an error'; 116 }.bind(this); 117 req.open('GET', this.downloadUrl, true); 118 req.setRequestHeader('Authorization', 'Bearer ' + token); 119 req.send(); 120 }.bind(this)); 121}; 122 123// Load the demo. 124window.demo = new Demo();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.