-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
main.js
155 lines (130 loc) · 4.65 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2016, 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.
$(function(){
// This is the host for the backend.
// TODO: When running Firenotes locally, set to http://localhost:8081. Before
// deploying the application to a live production environment, change to
// https://backend-dot-<PROJECT_ID>.appspot.com as specified in the
// backend's app.yaml file.
var backendHostUrl = '<your-backend-url>';
// [START gae_python_firenotes_config]
// Obtain the following from the "Add Firebase to your web app" dialogue
// Initialize Firebase
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
projectId: "<PROJECT_ID>",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<MESSAGING_SENDER_ID>"
};
// [END gae_python_firenotes_config]
// This is passed into the backend to authenticate the user.
var userIdToken = null;
// Firebase log-in
function configureFirebaseLogin() {
firebase.initializeApp(config);
// [START gae_python_state_change]
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
$('#logged-out').hide();
var name = user.displayName;
/* If the provider gives a display name, use the name for the
personal welcome message. Otherwise, use the user's email. */
var welcomeName = name ? name : user.email;
user.getIdToken().then(function(idToken) {
userIdToken = idToken;
/* Now that the user is authenicated, fetch the notes. */
fetchNotes();
$('#user').text(welcomeName);
$('#logged-in').show();
});
} else {
$('#logged-in').hide();
$('#logged-out').show();
}
});
// [END gae_python_state_change]
}
// [START gae_python_firebase_login]
// Firebase log-in widget
function configureFirebaseLoginWidget() {
var uiConfig = {
'signInSuccessUrl': '/',
'signInOptions': [
// Leave the lines as is for the providers you want to offer your users.
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
firebase.auth.FacebookAuthProvider.PROVIDER_ID,
firebase.auth.TwitterAuthProvider.PROVIDER_ID,
firebase.auth.GithubAuthProvider.PROVIDER_ID,
firebase.auth.EmailAuthProvider.PROVIDER_ID
],
// Terms of service url
'tosUrl': '<your-tos-url>',
};
var ui = new firebaseui.auth.AuthUI(firebase.auth());
ui.start('#firebaseui-auth-container', uiConfig);
}
// [END gae_python_firebase_login]
// [START gae_python_fetch_notes]
// Fetch notes from the backend.
function fetchNotes() {
$.ajax(backendHostUrl + '/notes', {
/* Set header for the XMLHttpRequest to get data from the web server
associated with userIdToken */
headers: {
'Authorization': 'Bearer ' + userIdToken
}
}).then(function(data){
$('#notes-container').empty();
// Iterate over user data to display user's notes from database.
data.forEach(function(note){
$('#notes-container').append($('<p>').text(note.message));
});
});
}
// [END gae_python_fetch_notes]
// Sign out a user
var signOutBtn =$('#sign-out');
signOutBtn.click(function(event) {
event.preventDefault();
firebase.auth().signOut().then(function() {
console.log("Sign out successful");
}, function(error) {
console.log(error);
});
});
// Save a note to the backend
var saveNoteBtn = $('#add-note');
saveNoteBtn.click(function(event) {
event.preventDefault();
var noteField = $('#note-content');
var note = noteField.val();
noteField.val("");
/* Send note data to backend, storing in database with existing data
associated with userIdToken */
$.ajax(backendHostUrl + '/notes', {
headers: {
'Authorization': 'Bearer ' + userIdToken
},
method: 'POST',
data: JSON.stringify({'message': note}),
contentType : 'application/json'
}).then(function(){
// Refresh notebook display.
fetchNotes();
});
});
configureFirebaseLogin();
configureFirebaseLoginWidget();
});