Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HansS/011489f49613ba8a18283c57ce37a626 to your computer and use it in GitHub Desktop.
Save HansS/011489f49613ba8a18283c57ce37a626 to your computer and use it in GitHub Desktop.
/**
* @license
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var config = {
// REPLACE WITH THE CONFIG OBJECT YOU CREATED
// https://medium.com/@SandeepDinesh/using-google-apis-with-firebase-auth-and-firebase-ui-on-the-web-46e6189cf571
};
firebase.initializeApp(config);
var uiConfig = {
signInSuccessUrl: "localhost:5000", // Assuming you are running on your local machine
signInOptions: [
{
provider: firebase.auth.GoogleAuthProvider.PROVIDER_ID,
scopes: config.scopes
}
],
// Terms of service url.
tosUrl: "<your-tos-url>"
};
// Initialize the FirebaseUI Widget using Firebase.
var ui = new firebaseui.auth.AuthUI(firebase.auth());
// The start method will wait until the DOM is loaded.
ui.start("#firebaseui-auth-container", uiConfig);
// This function will trigger when there is a login event
firebase.auth().onAuthStateChanged(function(user) {
console.log(user)
// Make sure there is a valid user object
if (user) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://apis.google.com/js/api.js";
// Once the Google API Client is loaded, you can run your code
script.onload = function(e) {
// Initialize the Google API Client with the config object
gapi.client
.init({
apiKey: config.apiKey,
clientId: config.clientID,
discoveryDocs: config.discoveryDocs,
scope: config.scopes.join(" ")
})
// Loading is finished, so start the app
.then(function() {
// Make sure the Google API Client is properly signed in
if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
startApp(user);
} else {
firebase.auth().signOut(); // Something went wrong, sign out
}
});
};
// Add to the document
document.getElementsByTagName("head")[0].appendChild(script);
}
});
function startApp(user) {
console.log(user);
// Make sure to refresh the Auth Token in case it expires!
firebase.auth().currentUser.getToken()
.then(function(){
return gapi.client.calendar.events
.list({
calendarId: "primary",
timeMin: new Date().toISOString(),
showDeleted: false,
singleEvents: true,
maxResults: 10,
orderBy: "startTime"
})
})
.then(function(response) {
console.log(response);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment