main.js
4.93 KB
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
/**
* Copyright 2018 Google Inc. All Rights Reserved.
*
* 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.
*/
'use strict';
// Signs-in Friendly Chat.
function signIn() {
// TODO 1: Sign in Firebase with credential from the Google user.
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider);
}
// Signs-out of Friendly Chat.
function signOut() {
// TODO 2: Sign out of Firebase.
firebase.auth().signOut();
}
// Initiate firebase auth.
function initFirebaseAuth() {
// TODO 3: Initialize Firebase.
firebase.auth().onAuthStateChanged(authStateObserver);
}
// Returns the signed-in user's profile Pic URL.
function getProfilePicUrl() {
// TODO 4: Return the user's profile pic URL.
return firebase.auth().currentUser.photoURL || '/images/profile_placeholder.png';
}
// Returns the signed-in user's display name.
function getUserName() {
// TODO 5: Return the user's display name.
return firebase.auth().currentUser.displayName;
}
// Returns true if a user is signed-in.
function isUserSignedIn() {
// TODO 6: Return true if a user is signed-in.
return !!firebase.auth().currentUser;
}
// Saves the messaging device token to the datastore.
function saveMessagingDeviceToken() {
// TODO 10: Save the device token in the realtime datastore
firebase.messaging().getToken().then(function(currentToken) {
if (currentToken) {
console.log('Got FCM device token:', currentToken);
// Saving the Device Token to the datastore.
var username = firebase.auth().currentUser.displayName;
firebase.database().ref('/fcmTokens').child(username)
.set(currentToken);
} else {
// Need to request permissions to show notifications.
requestNotificationsPermissions();
}
}).catch(function(error){
console.error('Unable to get messaging token.', error);
});
}
// Requests permissions to show notifications.
function requestNotificationsPermissions() {
// TODO 11: Request permissions to send notifications.
console.log('Requesting notifications permission...');
firebase.messaging().requestPermission().then(function() {
// Notification permission granted.
saveMessagingDeviceToken();
}).catch(function(error) {
console.error('Unable to get permission to notify.', error);
});
}
// Triggers when the auth state change for instance when the user signs-in or signs-out.
function authStateObserver(user) {
if (user) { // User is signed in!
// Get the signed-in user's profile pic and name.
var profilePicUrl = getProfilePicUrl();
var userName = getUserName();
// Set the user's profile pic and name.
userPicElement.style.backgroundImage = 'url(' + profilePicUrl + ')';
userNameElement.textContent = userName;
// Show user's profile and sign-out button.
userNameElement.removeAttribute('hidden');
userPicElement.removeAttribute('hidden');
signOutButtonElement.removeAttribute('hidden');
// Hide sign-in button.
signInButtonElement.setAttribute('hidden', 'true');
// We save the Firebase Messaging Device token and enable notifications.
saveMessagingDeviceToken();
} else { // User is signed out!
// Hide user's profile and sign-out button.
userNameElement.setAttribute('hidden', 'true');
userPicElement.setAttribute('hidden', 'true');
signOutButtonElement.setAttribute('hidden', 'true');
// Show sign-in button.
signInButtonElement.removeAttribute('hidden');
}
}
// A loading image URL.
var LOADING_IMAGE_URL = 'https://www.google.com/images/spin-32.gif?a';
// Checks that the Firebase SDK has been correctly setup and configured.
function checkSetup() {
if (!window.firebase || !(firebase.app instanceof Function) || !firebase.app().options) {
window.alert('You have not configured and imported the Firebase SDK. ' +
'Make sure you go through the codelab setup instructions and make ' +
'sure you are running the codelab using `firebase serve`');
}
}
// Checks that Firebase has been imported.
checkSetup();
// Shortcuts to DOM Elements.
var userPicElement = document.getElementById('user-pic');
var userNameElement = document.getElementById('user-name');
var signInButtonElement = document.getElementById('sign-in');
var signOutButtonElement = document.getElementById('sign-out');
// Saves message on form submit.
signOutButtonElement.addEventListener('click', signOut);
signInButtonElement.addEventListener('click', signIn);
// initialize Firebase
initFirebaseAuth();