1
- /**
2
- * Copyright 2018, Google LLC
3
- * Licensed under the Apache License, Version 2.0 (the `License`);
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an `AS IS` BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- // [START gae_python38_log]
17
- 'use strict' ;
18
-
19
- window . addEventListener ( 'load' , function ( ) {
20
- document . getElementById ( 'sign-out' ) . onclick = function ( ) {
21
- firebase . auth ( ) . signOut ( ) ;
22
- } ;
23
-
24
- // FirebaseUI config.
25
- var uiConfig = {
26
- signInSuccessUrl : '/' ,
27
- signInOptions : [
28
- // Comment out any lines corresponding to providers you did not check in
29
- // the Firebase console.
30
- firebase . auth . GoogleAuthProvider . PROVIDER_ID ,
31
- firebase . auth . EmailAuthProvider . PROVIDER_ID ,
32
- //firebase.auth.FacebookAuthProvider.PROVIDER_ID,
33
- //firebase.auth.TwitterAuthProvider.PROVIDER_ID,
34
- //firebase.auth.GithubAuthProvider.PROVIDER_ID,
35
- //firebase.auth.PhoneAuthProvider.PROVIDER_ID
36
-
37
- ] ,
38
- // Terms of service url.
39
- tosUrl : '<your-tos-url>'
40
- } ;
41
-
42
- firebase . auth ( ) . onAuthStateChanged ( function ( user ) {
43
- if ( user ) {
44
- // User is signed in, so display the "sign out" button and login info.
45
- document . getElementById ( 'sign-out' ) . hidden = false ;
46
- document . getElementById ( 'login-info' ) . hidden = false ;
47
- console . log ( `Signed in as ${ user . displayName } (${ user . email } )` ) ;
48
- user . getIdToken ( ) . then ( function ( token ) {
49
- // Add the token to the browser's cookies. The server will then be
50
- // able to verify the token against the API.
51
- // SECURITY NOTE: As cookies can easily be modified, only put the
52
- // token (which is verified server-side) in a cookie; do not add other
53
- // user information.
54
- document . cookie = "token=" + token ;
55
- } ) ;
56
- } else {
57
- // User is signed out.
58
- // Initialize the FirebaseUI Widget using Firebase.
59
- var ui = new firebaseui . auth . AuthUI ( firebase . auth ( ) ) ;
60
- // Show the Firebase login button.
61
- ui . start ( '#firebaseui-auth-container' , uiConfig ) ;
62
- // Update the login state indicators.
63
- document . getElementById ( 'sign-out' ) . hidden = true ;
64
- document . getElementById ( 'login-info' ) . hidden = true ;
65
- // Clear the token cookie.
66
- document . cookie = "token=" ;
67
- }
68
- } , function ( error ) {
69
- console . log ( error ) ;
70
- alert ( 'Unable to log in: ' + error )
71
- } ) ;
72
- } ) ;
0 commit comments