-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
152 lines (142 loc) · 4.74 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>GAPI and Firebase Auth</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
<script src="https://apis.google.com/js/api.js"></script>
<style>
.container {
max-width: 600px;
}
</style>
</head>
<body>
<main role="main" class="container">
<h1 class="mt-5">
GAPI and Firebase Auth
</h1>
<p className="lead">
An example of how to use GAPI and Firebase Auth together
to authenticate users and talk to Google APIs.
</p>
<p>
Ensure you have your developer console open.
</p>
<p>
<button id="sign_in" class="btn btn-primary">Sign in</button>
<button id="sign_out" class="btn btn-primary">Sign out</button>
</p>
</main>
<script type="text/babel">
const AUTH_SCOPES = [
'email',
'profile',
'https://www.googleapis.com/auth/analytics.readonly',
]
/***************************************************************************
* Initialize Firebase
*/
// OVERWRITE ME
/*
{
apiKey: "...",
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
projectId: "my-app-5e480",
storageBucket: "my-app-5e480.appspot.com",
messagingSenderId: "..."
}
*/
const FIREBASE_CONFIG = {}
const fb = firebase.initializeApp(FIREBASE_CONFIG)
// OVERWRITE ME
// ....apps.googleusercontent.com
const CLIENT_ID = ''
function handleIsSignedIn(isSignedIn) {
if (isSignedIn) {
const auth2 = gapi.auth2.getAuthInstance()
const currentUser = auth2.currentUser.get()
const profile = currentUser.getBasicProfile()
console.log('gapi: user signed in!', {
name: profile.getName(),
imageURL: profile.getImageUrl(),
email: profile.getEmail(),
})
const authResponse = currentUser.getAuthResponse(true)
const credential = firebase.auth.GoogleAuthProvider.credential(
authResponse.id_token,
authResponse.access_token
)
fb.auth().signInWithCredential(credential)
.then(({ user }) => {
console.log('firebase: user signed in!', {
displayName: user.displayName,
email: user.email,
photoURL: user.photoURL,
})
})
// Try to make a request to Google Analytics!
gapi.client.analytics.management.accounts.list()
.then((response) => {
console.log('Google Analytics request successful!')
if (response.result.items && response.result.items.length) {
const accountNames = response.result.items.map(account => account.name)
alert('Google Analytics account names: ' + accountNames.join(' '))
}
})
} else {
console.log('gapi: user is not signed in')
}
}
new Promise((resolve, reject) => {
gapi.load('client:auth2', () => {
resolve()
})
})
.then(() => { console.log('gapi: client:auth2 loaded', gapi.client) })
.then(() => {
return gapi.client.init({
apiKey: FIREBASE_CONFIG.apiKey,
clientId: CLIENT_ID,
scope: AUTH_SCOPES.join(' '),
})
})
.then(() => { console.log('gapi: client initialized') })
.then(() => { return gapi.client.load('analytics', 'v3') })
.then(() => { console.log('gapi: analytics v3 loaded', gapi.client.analytics)})
.then(() => {
const auth2 = gapi.auth2.getAuthInstance()
auth2.isSignedIn.listen(handleIsSignedIn)
handleIsSignedIn(auth2.isSignedIn.get())
document.querySelector('#sign_in')
.addEventListener('click', function handleSignIn() {
const auth2 = gapi.auth2.getAuthInstance()
if (auth2.isSignedIn.get()) {
alert('already signed in')
return
}
auth2.signIn()
.catch(error => { alert(`sign in error: ${error}`) })
})
document.querySelector('#sign_out')
.addEventListener('click', function handleSignOut() {
console.log('signing out...')
const auth2 = gapi.auth2.getAuthInstance()
if (!auth2.isSignedIn.get()) {
alert('Not signed in!')
return
}
auth2.signOut()
.then(() => { console.log('gapi: sign out complete') })
.then(() => { return fb.auth().signOut() })
.then(() => { console.log('firebase: sign out complete') })
})
})
</script>
</body>
</html>