@@ -20,6 +20,7 @@ var messageForm = document.getElementById('message-form');
20
20
var messageInput = document . getElementById ( 'new-post-message' ) ;
21
21
var titleInput = document . getElementById ( 'new-post-title' ) ;
22
22
var signInButton = document . getElementById ( 'sign-in-button' ) ;
23
+ var signOutButton = document . getElementById ( 'sign-out-button' ) ;
23
24
var splashPage = document . getElementById ( 'page-splash' ) ;
24
25
var addPost = document . getElementById ( 'add-post' ) ;
25
26
var addButton = document . getElementById ( 'add' ) ;
@@ -34,14 +35,15 @@ var myTopPostsMenuButton = document.getElementById('menu-my-top-posts');
34
35
* Saves a new post to the Firebase DB.
35
36
*/
36
37
// [START write_fan_out]
37
- function writeNewPost ( uid , username , title , body ) {
38
+ function writeNewPost ( uid , username , picture , title , body ) {
38
39
// A post entry.
39
40
var postData = {
40
41
author : username ,
41
42
uid : uid ,
42
43
body : body ,
43
44
title : title ,
44
- starCount : 0
45
+ starCount : 0 ,
46
+ authorPic : picture
45
47
} ;
46
48
47
49
// Get a key for a new Post.
@@ -82,7 +84,7 @@ function toggleStar(postRef, uid) {
82
84
/**
83
85
* Creates a post element.
84
86
*/
85
- function createPostElement ( postId , title , text , author , authorId ) {
87
+ function createPostElement ( postId , title , text , author , authorId , authorPic ) {
86
88
var uid = firebase . auth ( ) . currentUser . uid ;
87
89
88
90
var html =
@@ -128,7 +130,8 @@ function createPostElement(postId, title, text, author, authorId) {
128
130
// Set values.
129
131
postElement . getElementsByClassName ( 'text' ) [ 0 ] . innerText = text ;
130
132
postElement . getElementsByClassName ( 'mdl-card__title-text' ) [ 0 ] . innerText = title ;
131
- postElement . getElementsByClassName ( 'username' ) [ 0 ] . innerText = author ;
133
+ postElement . getElementsByClassName ( 'username' ) [ 0 ] . innerText = author || 'Anonymous' ;
134
+ postElement . getElementsByClassName ( 'avatar' ) [ 0 ] . style . backgroundImage = `url("${ authorPic || './silhouette.jpg' } ")` ;
132
135
133
136
// Listen for comments.
134
137
// [START child_event_listener_recycler]
@@ -218,7 +221,7 @@ function addCommentElement(postElement, id, text, author) {
218
221
comment . classList . add ( 'comment-' + id ) ;
219
222
comment . innerHTML = '<span class="username"></span><span class="comment"></span>' ;
220
223
comment . getElementsByClassName ( 'comment' ) [ 0 ] . innerText = text ;
221
- comment . getElementsByClassName ( 'username' ) [ 0 ] . innerText = author ;
224
+ comment . getElementsByClassName ( 'username' ) [ 0 ] . innerText = author || 'Anonymous' ;
222
225
223
226
var commentsContainer = postElement . getElementsByClassName ( 'comments-container' ) [ 0 ] ;
224
227
commentsContainer . appendChild ( comment ) ;
@@ -256,9 +259,10 @@ function startDatabaseQueries() {
256
259
257
260
var fetchPosts = function ( postsRef , sectionElement ) {
258
261
postsRef . on ( 'child_added' , function ( data ) {
262
+ var author = data . val ( ) . author || 'Anonymous' ;
259
263
var containerElement = sectionElement . getElementsByClassName ( 'posts-container' ) [ 0 ] ;
260
264
containerElement . insertBefore (
261
- createPostElement ( data . key , data . val ( ) . title , data . val ( ) . body , data . val ( ) . author , data . val ( ) . uid ) ,
265
+ createPostElement ( data . key , data . val ( ) . title , data . val ( ) . body , author , data . val ( ) . uid , data . val ( ) . authorPic ) ,
262
266
containerElement . firstChild ) ;
263
267
} ) ;
264
268
} ;
@@ -272,10 +276,11 @@ function startDatabaseQueries() {
272
276
* Writes the user's data to the database.
273
277
*/
274
278
// [START basic_write]
275
- function writeUserData ( userId , name , email ) {
279
+ function writeUserData ( userId , name , email , imageUrl ) {
276
280
firebase . database ( ) . ref ( 'users/' + userId ) . set ( {
277
281
username : name ,
278
- email : email
282
+ email : email ,
<
9E88
/td>283
+ profile_picture : imageUrl
279
284
} ) ;
280
285
}
281
286
// [END basic_write]
@@ -288,11 +293,16 @@ window.addEventListener('load', function() {
288
293
firebase . auth ( ) . signInWithPopup ( provider ) ;
289
294
} ) ;
290
295
296
+ // Bind Sign out button.
297
+ signOutButton . addEventListener ( 'click' , function ( ) {
298
+ firebase . auth ( ) . signOut ( ) ;
299
+ } ) ;
300
+
291
301
// Listen for auth state changes
292
302
firebase . auth ( ) . onAuthStateChanged ( function ( user ) {
293
303
if ( user ) {
294
304
splashPage . style . display = 'none' ;
295
- writeUserData ( user . uid , user . displayName , user . email ) ;
305
+ writeUserData ( user . uid , user . displayName , user . email , user . photoURL ) ;
296
306
startDatabaseQueries ( ) ;
297
307
} else {
298
308
splashPage . style . display = '' ;
@@ -311,6 +321,7 @@ window.addEventListener('load', function() {
311
321
var username = snapshot . val ( ) . username ;
312
322
// [START_EXCLUDE]
313
323
writeNewPost ( firebase . auth ( ) . currentUser . uid , firebase . auth ( ) . currentUser . displayName ,
324
+ firebase . auth ( ) . currentUser . photoURL ,
314
325
titleInput . value , postText ) . then ( function ( ) {
315
326
myPostsMenuButton . click ( ) ;
316
327
} ) ;
0 commit comments