1
+ /**
2
+ * Redirects to the FirebaseUI widget.
3
+ */
4
+ var signInWithRedirect = function ( ) {
5
+ window . location . assign ( '/widget' ) ;
6
+ } ;
7
+
8
+
9
+ /**
10
+ * Open a popup with the FirebaseUI widget.
11
+ */
12
+ var signInWithPopup = function ( ) {
13
+ window . open ( '/widget' , 'Sign In' , 'width=985,height=735' ) ;
14
+ } ;
15
+
16
+
17
+ /**
18
+ * Displays the UI for a signed in user.
19
+ */
20
+ var handleSignedInUser = function ( user ) {
21
+ document . getElementById ( 'user-signed-in' ) . style . display = 'block' ;
22
+ document . getElementById ( 'user-signed-out' ) . style . display = 'none' ;
23
+ document . getElementById ( 'name' ) . textContent = user . displayName ;
24
+ document . getElementById ( 'email' ) . textContent = user . email ;
25
+ if ( user . photoURL ) {
26
+ document . getElementById ( 'photo' ) . src = user . photoURL ;
27
+ document . getElementById ( 'photo' ) . style . display = 'block' ;
28
+ } else {
29
+ document . getElementById ( 'photo' ) . style . display = 'none' ;
30
+ }
31
+ } ;
32
+
33
+
34
+ /**
35
+ * Displays the UI for a signed out user.
36
+ */
37
+ var handleSignedOutUser = function ( ) {
38
+ document . getElementById ( 'user-signed-in' ) . style . display = 'none' ;
39
+ document . getElementById ( 'user-signed-out' ) . style . display = 'block' ;
40
+ } ;
41
+
42
+ // Listen to change in auth state so it displays the correct UI for when
43
+ // the user is signed in or not.
44
+ firebase . auth ( ) . onAuthStateChanged ( function ( user ) {
45
+ user ? handleSignedInUser ( user ) : handleSignedOutUser ( ) ;
46
+ } ) ;
47
+
48
+
49
+ /**
50
+ * Initializes the app.
51
+ */
52
+ var initApp = function ( ) {
53
+ document . getElementById ( 'sign-in-with-redirect' ) . addEventListener (
54
+ 'click' , signInWithRedirect ) ;
55
+ document . getElementById ( 'sign-in-with-popup' ) . addEventListener (
56
+ 'click' , signInWithPopup ) ;
57
+ document . getElementById ( 'sign-out' ) . addEventListener ( 'click' , function ( ) {
58
+ firebase . auth ( ) . signOut ( ) ;
59
+ } ) ;
60
+ document . getElementById ( 'delete-account' ) . addEventListener (
61
+ 'click' , function ( ) {
62
+ firebase . auth ( ) . currentUser . delete ( ) ;
63
+ } ) ;
64
+ } ;
65
+
66
+ window . addEventListener ( 'load' , initApp ) ;
0 commit comments