質問編集履歴
4
説明の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -58,7 +58,7 @@
|
|
58
58
|
|
59
59
|
|
60
60
|
|
61
|
-
|
61
|
+
④index.vueでユーザー一覧取得処理を呼び出す。
|
62
62
|
|
63
63
|
index.vue
|
64
64
|
|
@@ -272,7 +272,19 @@
|
|
272
272
|
|
273
273
|
```
|
274
274
|
|
275
|
+
⑤サーバー起動
|
276
|
+
|
277
|
+
yarn run dev
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
⑥アプリケーションにアクセスする
|
282
|
+
|
283
|
+
http://localhost:3000/
|
284
|
+
|
285
|
+
|
286
|
+
|
275
|
-
|
287
|
+
アプリケーションにアクセスすると「Cannot read property 'auth' of undefined」と表示されてしまいます。
|
276
288
|
|
277
289
|
解決方法について探していますが、なかなか解決できません。
|
278
290
|
|
3
タイトルと説明を修正。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Nuxt.jsでFirebaseの
|
1
|
+
Nuxt.jsでFirebase Admin SDKの初期化がうまくいかない
|
test
CHANGED
@@ -1,20 +1,120 @@
|
|
1
1
|
Nuxt.jsを勉強しているものです。
|
2
2
|
|
3
|
-
Nuxt.jsのWebアプリケーションでFirebaseの認証ユーザーの一覧を取得し
|
3
|
+
Nuxt.jsのWebアプリケーションでFirebaseの認証ユーザーの一覧を取得する処理を実装しようとしています。
|
4
|
+
|
5
|
+
|
6
|
+
|
4
|
-
|
7
|
+
作業手順
|
8
|
+
|
5
|
-
|
9
|
+
①プロジェクト作成
|
10
|
+
|
6
|
-
|
11
|
+
yarn create nuxt-app sample
|
12
|
+
|
13
|
+
|
14
|
+
|
7
|
-
|
15
|
+
②firebase-adminをインストール
|
16
|
+
|
8
|
-
|
17
|
+
npm install firebase-admin --save
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
③plugins配下にfirebaseAdmin.jsを作成
|
22
|
+
|
23
|
+
|
24
|
+
|
9
|
-
p
|
25
|
+
plugins/firebaseAdmin.js
|
26
|
+
|
10
|
-
|
27
|
+
```js
|
28
|
+
|
29
|
+
const admin = (function () {
|
30
|
+
|
31
|
+
if (process.server) {
|
32
|
+
|
33
|
+
const a = require('firebase-admin');
|
34
|
+
|
35
|
+
if (!a.apps.length) {
|
36
|
+
|
37
|
+
const serviceAccount = require('../key.json');
|
38
|
+
|
39
|
+
a.initializeApp({
|
40
|
+
|
41
|
+
credential: a.credential.cert(serviceAccount),
|
42
|
+
|
43
|
+
databaseURL: 'XXXXXXXXXX'
|
44
|
+
|
45
|
+
});
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
return a;
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
})();
|
54
|
+
|
55
|
+
export default admin
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
③index.vueでユーザー一覧取得処理を呼び出す。
|
62
|
+
|
63
|
+
index.vue
|
64
|
+
|
11
|
-
```
|
65
|
+
```vue
|
12
66
|
|
13
67
|
<template>
|
14
68
|
|
69
|
+
<div class="container">
|
70
|
+
|
15
|
-
<div>
|
71
|
+
<div>
|
16
|
-
|
72
|
+
|
17
|
-
<
|
73
|
+
<Logo />
|
74
|
+
|
75
|
+
<h1 class="title">
|
76
|
+
|
77
|
+
sample
|
78
|
+
|
79
|
+
</h1>
|
80
|
+
|
81
|
+
<div class="links">
|
82
|
+
|
83
|
+
<a
|
84
|
+
|
85
|
+
href="https://nuxtjs.org/"
|
86
|
+
|
87
|
+
target="_blank"
|
88
|
+
|
89
|
+
rel="noopener noreferrer"
|
90
|
+
|
91
|
+
class="button--green"
|
92
|
+
|
93
|
+
>
|
94
|
+
|
95
|
+
Documentation
|
96
|
+
|
97
|
+
</a>
|
98
|
+
|
99
|
+
<a
|
100
|
+
|
101
|
+
href="https://github.com/nuxt/nuxt.js"
|
102
|
+
|
103
|
+
target="_blank"
|
104
|
+
|
105
|
+
rel="noopener noreferrer"
|
106
|
+
|
107
|
+
class="button--grey"
|
108
|
+
|
109
|
+
>
|
110
|
+
|
111
|
+
GitHub
|
112
|
+
|
113
|
+
</a>
|
114
|
+
|
115
|
+
</div>
|
116
|
+
|
117
|
+
</div>
|
18
118
|
|
19
119
|
</div>
|
20
120
|
|
@@ -24,15 +124,63 @@
|
|
24
124
|
|
25
125
|
<script>
|
26
126
|
|
27
|
-
import
|
127
|
+
import admin from '~/plugins/firebaseAdmin'
|
28
128
|
|
29
129
|
export default {
|
30
130
|
|
131
|
+
data() {
|
132
|
+
|
31
|
-
|
133
|
+
return {
|
32
|
-
|
134
|
+
|
33
|
-
|
135
|
+
}
|
34
|
-
|
136
|
+
|
35
|
-
|
137
|
+
},
|
138
|
+
|
139
|
+
mounted() {
|
140
|
+
|
141
|
+
const listAllUsers = (nextPageToken) => {
|
142
|
+
|
143
|
+
// List batch of users, 1000 at a time.
|
144
|
+
|
145
|
+
admin
|
146
|
+
|
147
|
+
.auth()
|
148
|
+
|
149
|
+
.listUsers(1000, nextPageToken)
|
150
|
+
|
151
|
+
.then((listUsersResult) => {
|
152
|
+
|
153
|
+
listUsersResult.users.forEach((userRecord) => {
|
154
|
+
|
155
|
+
console.log('user', userRecord.toJSON());
|
156
|
+
|
157
|
+
});
|
158
|
+
|
159
|
+
if (listUsersResult.pageToken) {
|
160
|
+
|
161
|
+
// List next batch of users.
|
162
|
+
|
163
|
+
listAllUsers(listUsersResult.pageToken);
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
})
|
168
|
+
|
169
|
+
.catch((error) => {
|
170
|
+
|
171
|
+
console.log('Error listing users:', error);
|
172
|
+
|
173
|
+
});
|
174
|
+
|
175
|
+
};
|
176
|
+
|
177
|
+
// Start listing users from the beginning, 1000 at a time.
|
178
|
+
|
179
|
+
listAllUsers();
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
},
|
36
184
|
|
37
185
|
}
|
38
186
|
|
@@ -42,7 +190,81 @@
|
|
42
190
|
|
43
191
|
<style>
|
44
192
|
|
45
|
-
|
193
|
+
.container {
|
194
|
+
|
195
|
+
margin: 0 auto;
|
196
|
+
|
197
|
+
min-height: 100vh;
|
198
|
+
|
199
|
+
display: flex;
|
200
|
+
|
201
|
+
justify-content: center;
|
202
|
+
|
203
|
+
align-items: center;
|
204
|
+
|
205
|
+
text-align: center;
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
.title {
|
212
|
+
|
213
|
+
font-family:
|
214
|
+
|
215
|
+
'Quicksand',
|
216
|
+
|
217
|
+
'Source Sans Pro',
|
218
|
+
|
219
|
+
-apple-system,
|
220
|
+
|
221
|
+
BlinkMacSystemFont,
|
222
|
+
|
223
|
+
'Segoe UI',
|
224
|
+
|
225
|
+
Roboto,
|
226
|
+
|
227
|
+
'Helvetica Neue',
|
228
|
+
|
229
|
+
Arial,
|
230
|
+
|
231
|
+
sans-serif;
|
232
|
+
|
233
|
+
display: block;
|
234
|
+
|
235
|
+
font-weight: 300;
|
236
|
+
|
237
|
+
font-size: 100px;
|
238
|
+
|
239
|
+
color: #35495e;
|
240
|
+
|
241
|
+
letter-spacing: 1px;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
.subtitle {
|
248
|
+
|
249
|
+
font-weight: 300;
|
250
|
+
|
251
|
+
font-size: 42px;
|
252
|
+
|
253
|
+
color: #526488;
|
254
|
+
|
255
|
+
word-spacing: 5px;
|
256
|
+
|
257
|
+
padding-bottom: 15px;
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
.links {
|
264
|
+
|
265
|
+
padding-top: 15px;
|
266
|
+
|
267
|
+
}
|
46
268
|
|
47
269
|
</style>
|
48
270
|
|
@@ -50,510 +272,14 @@
|
|
50
272
|
|
51
273
|
```
|
52
274
|
|
53
|
-
|
54
|
-
|
55
|
-
/components/Users.vue
|
56
|
-
|
57
|
-
```Vue
|
58
|
-
|
59
|
-
<template>
|
60
|
-
|
61
|
-
<div class="container">
|
62
|
-
|
63
|
-
ユーザ一覧
|
64
|
-
|
65
|
-
<!-- TODO ここにテーブル形式でユーザー一覧を表示させる -->
|
66
|
-
|
67
|
-
</div>
|
68
|
-
|
69
|
-
</template>
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
<script>
|
74
|
-
|
75
|
-
|
275
|
+
この実装で動作確認を行うと「Cannot read property 'auth' of undefined」と表示されてしまいます。
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
276
|
+
|
80
|
-
|
81
|
-
data() {
|
82
|
-
|
83
|
-
return {};
|
84
|
-
|
85
|
-
},
|
86
|
-
|
87
|
-
mounted() {
|
88
|
-
|
89
|
-
|
277
|
+
解決方法について探していますが、なかなか解決できません。
|
90
|
-
|
91
|
-
|
278
|
+
|
92
|
-
|
93
|
-
console.log('user:', JSON.stringify(listUsersResult));
|
94
|
-
|
95
|
-
});
|
96
|
-
|
97
|
-
},
|
98
|
-
|
99
|
-
methods: {
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
</script>
|
106
|
-
|
107
|
-
```
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
/plugins/firebase.js
|
112
|
-
|
113
|
-
```Vue
|
114
|
-
|
115
|
-
i
|
279
|
+
Firebase Admin SDKの初期化手順が間違っているのでしょうか?
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
280
|
+
|
120
|
-
|
121
|
-
firebase.initializeApp(
|
122
|
-
|
123
|
-
{
|
124
|
-
|
125
|
-
apiKey: "XXXXXXX",
|
126
|
-
|
127
|
-
authDomain: "XXXXXX",
|
128
|
-
|
129
|
-
databaseURL: "XXXXXXX",
|
130
|
-
|
131
|
-
projectId: "XXXXXXXXX",
|
132
|
-
|
133
|
-
storageBucket: "XXXXX",
|
134
|
-
|
135
|
-
messagingSenderId: "XXXXXX",
|
136
|
-
|
137
|
-
appId: "XXXXXXXXXXXX",
|
138
|
-
|
139
|
-
measurementId: "XXXXXXXXX"
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
)
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
export default firebase
|
148
|
-
|
149
|
-
```
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
この状態で実行すると
|
154
|
-
|
155
|
-
listUsers関数が見つかりませんというエラーが発生します。
|
156
|
-
|
157
|
-
|
281
|
+
そもそもnuxtアプリケーションにFirebase Admin SDKを導入することはできないのでしょうか?
|
158
|
-
|
282
|
+
|
159
|
-
Firebase
|
283
|
+
Firebaseに詳しい方がいましたら、ご回答いただけないでしょうか
|
160
284
|
|
161
285
|
よろしくお願いいたします。
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
-- 追記 --
|
166
|
-
|
167
|
-
https://gurutaka-log.com/nuxt-firebase-auth-serviceworker#STEP4firebase-admin
|
168
|
-
|
169
|
-
https://gurutaka-log.com/nuxtserverinit-firestore
|
170
|
-
|
171
|
-
上記サイトを参考に以下のように実装してみました。
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
nuxt.config.json
|
176
|
-
|
177
|
-
```json
|
178
|
-
|
179
|
-
export default {
|
180
|
-
|
181
|
-
(中略)
|
182
|
-
|
183
|
-
workbox: {
|
184
|
-
|
185
|
-
importScripts: [
|
186
|
-
|
187
|
-
"https://www.gstatic.com/firebasejs/7.6.1/firebase-app.js",
|
188
|
-
|
189
|
-
"https://www.gstatic.com/firebasejs/7.6.1/firebase-auth.js",
|
190
|
-
|
191
|
-
"https://www.gstatic.com/firebasejs/6.2.4/firebase-database.js",
|
192
|
-
|
193
|
-
"sw-firebase-auth.js"
|
194
|
-
|
195
|
-
],
|
196
|
-
|
197
|
-
dev: process.env.MODE != "production",
|
198
|
-
|
199
|
-
},
|
200
|
-
|
201
|
-
(省略)
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
```
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
static/sw-firebase-auth.js
|
210
|
-
|
211
|
-
```json
|
212
|
-
|
213
|
-
// firebaseを初期化
|
214
|
-
|
215
|
-
firebase.initializeApp({
|
216
|
-
|
217
|
-
apiKey: XXXXXXX,
|
218
|
-
|
219
|
-
authDomain: XXXXXXXXX,
|
220
|
-
|
221
|
-
databaseURL: XXXXXXXXXX,
|
222
|
-
|
223
|
-
projectId: XXXXXXXXXXX,
|
224
|
-
|
225
|
-
storageBucket: XXXXXXXXXXX,
|
226
|
-
|
227
|
-
messagingSenderId: XXXXXXXXXXXXX,
|
228
|
-
|
229
|
-
appId: XXXXXXXXXXXXX,
|
230
|
-
|
231
|
-
measurementId: XXXXXXXXXXXXX
|
232
|
-
|
233
|
-
});
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
// onAuthStateChanged()で現在のuserからidTokenを取得
|
238
|
-
|
239
|
-
const getIdToken = () => {
|
240
|
-
|
241
|
-
return new Promise((resolve, reject) => {
|
242
|
-
|
243
|
-
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
|
244
|
-
|
245
|
-
unsubscribe();
|
246
|
-
|
247
|
-
if (user) {
|
248
|
-
|
249
|
-
user.getIdToken().then(
|
250
|
-
|
251
|
-
idToken => resolve(idToken),
|
252
|
-
|
253
|
-
error => resolve(null)
|
254
|
-
|
255
|
-
);
|
256
|
-
|
257
|
-
} else {
|
258
|
-
|
259
|
-
resolve(null);
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
});
|
264
|
-
|
265
|
-
});
|
266
|
-
|
267
|
-
};
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
// URLからルートのURLを取得する処理
|
272
|
-
|
273
|
-
const getOriginFromUrl = url => {
|
274
|
-
|
275
|
-
// https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript
|
276
|
-
|
277
|
-
const pathArray = url.split("/");
|
278
|
-
|
279
|
-
const protocol = pathArray[0];
|
280
|
-
|
281
|
-
const host = pathArray[2];
|
282
|
-
|
283
|
-
return protocol + "//" + host;
|
284
|
-
|
285
|
-
};
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
/**
|
290
|
-
|
291
|
-
* Service Workderのライフサイクルでfetchしたときの処理
|
292
|
-
|
293
|
-
*/
|
294
|
-
|
295
|
-
self.addEventListener("fetch", event => {
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
// リクエストをラップして、ヘッダにFirebase AuthのIdTokenを追加する処理
|
300
|
-
|
301
|
-
const requestProcessor = idToken => {
|
302
|
-
|
303
|
-
let req = event.request;
|
304
|
-
|
305
|
-
// URLを取得して、httpsもしくはlocalhostかなどをチェック
|
306
|
-
|
307
|
-
if (self.location.origin == getOriginFromUrl(event.request.url) &&
|
308
|
-
|
309
|
-
(self.location.protocol == "https:" || self.location.hostname == "localhost") &&
|
310
|
-
|
311
|
-
idToken
|
312
|
-
|
313
|
-
) {
|
314
|
-
|
315
|
-
// ヘッダ情報をクローンする
|
316
|
-
|
317
|
-
const headers = new Headers();
|
318
|
-
|
319
|
-
for (let entry of req.headers.entries()) {
|
320
|
-
|
321
|
-
headers.append(entry[0], entry[1]);
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
// クローンしたヘッダにFirebase AuthのIdTokenを追加
|
326
|
-
|
327
|
-
headers.append("Authorization", "Bearer " + idToken);
|
328
|
-
|
329
|
-
try {
|
330
|
-
|
331
|
-
req = new Request(req.url, {
|
332
|
-
|
333
|
-
method: req.method,
|
334
|
-
|
335
|
-
headers: headers,
|
336
|
-
|
337
|
-
mode: "same-origin",
|
338
|
-
|
339
|
-
credentials: req.credentials,
|
340
|
-
|
341
|
-
cache: req.cache,
|
342
|
-
|
343
|
-
redirect: req.redirect,
|
344
|
-
|
345
|
-
referrer: req.referrer,
|
346
|
-
|
347
|
-
body: req.body,
|
348
|
-
|
349
|
-
bodyUsed: req.bodyUsed,
|
350
|
-
|
351
|
-
context: req.context
|
352
|
-
|
353
|
-
});
|
354
|
-
|
355
|
-
} catch (e) {
|
356
|
-
|
357
|
-
console.error(e);
|
358
|
-
|
359
|
-
}
|
360
|
-
|
361
|
-
}
|
362
|
-
|
363
|
-
return fetch(req);
|
364
|
-
|
365
|
-
};
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
// 上の関数を使って、全リクエストでIdTokenの取得し、Firebase AuthのIdTokenを追加ようにする
|
370
|
-
|
371
|
-
event.respondWith(getIdToken().then(requestProcessor, requestProcessor));
|
372
|
-
|
373
|
-
});
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
/**
|
378
|
-
|
379
|
-
* Service Workderのライフサイクルでactivateしたときの処理
|
380
|
-
|
381
|
-
*/
|
382
|
-
|
383
|
-
self.addEventListener("activate", event => {
|
384
|
-
|
385
|
-
event.waitUntil(clients.claim());
|
386
|
-
|
387
|
-
});
|
388
|
-
|
389
|
-
```
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
plugins/firebaseAdmin.js
|
394
|
-
|
395
|
-
```javascript
|
396
|
-
|
397
|
-
let admin
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
if (process.server) {
|
402
|
-
|
403
|
-
admin = require('firebase-admin')
|
404
|
-
|
405
|
-
if (!admin.apps.length) {
|
406
|
-
|
407
|
-
const serviceAccount = require('key.json')
|
408
|
-
|
409
|
-
admin.initializeApp({
|
410
|
-
|
411
|
-
credential: admin.credential.cert(serviceAccount),
|
412
|
-
|
413
|
-
databaseURL: "XXXXXXXXXXXXXXX"
|
414
|
-
|
415
|
-
})
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
}
|
420
|
-
|
421
|
-
export default admin
|
422
|
-
|
423
|
-
```
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
/components/Users.vue
|
428
|
-
|
429
|
-
```Vue
|
430
|
-
|
431
|
-
<template>
|
432
|
-
|
433
|
-
<div class="container">
|
434
|
-
|
435
|
-
ユーザ一覧
|
436
|
-
|
437
|
-
</div>
|
438
|
-
|
439
|
-
</template>
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
<script>
|
444
|
-
|
445
|
-
export default {
|
446
|
-
|
447
|
-
data() {
|
448
|
-
|
449
|
-
return {};
|
450
|
-
|
451
|
-
},
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
created() {},
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
// 計算等
|
460
|
-
|
461
|
-
computed: {},
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
// 監視
|
466
|
-
|
467
|
-
watch: {
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
},
|
472
|
-
|
473
|
-
filters: {
|
474
|
-
|
475
|
-
},
|
476
|
-
|
477
|
-
mounted() {
|
478
|
-
|
479
|
-
this.nuxtServerInit();
|
480
|
-
|
481
|
-
},
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
methods: {
|
486
|
-
|
487
|
-
async nuxtServerInit() {
|
488
|
-
|
489
|
-
// firebase-adminの初期化
|
490
|
-
|
491
|
-
const admin = require('~/plugins/firebaseAdmin').default;
|
492
|
-
|
493
|
-
let users = admin.auth().listUsers(1000).then((listUsersResult) => {
|
494
|
-
|
495
|
-
console.log('user:', JSON.stringify(listUsersResult));
|
496
|
-
|
497
|
-
});
|
498
|
-
|
499
|
-
}
|
500
|
-
|
501
|
-
}
|
502
|
-
|
503
|
-
}
|
504
|
-
|
505
|
-
</script>
|
506
|
-
|
507
|
-
```
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
この状態で動作確認すると以下のエラーが発生します。
|
512
|
-
|
513
|
-
Uncaught (in promise) TypeError: Cannot read property 'auth' of undefined
|
514
|
-
|
515
|
-
admin.firestore().collectionの部分でエラーとなってしまいfirebaseの初期化がうまくいきません。
|
516
|
-
|
517
|
-
また、workboxを使ってfirebaseのライブラリを読み込んでいるはずが以下の警告が出てしまいます。
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
It looks like you're using the development build of the Firebase JS SDK.
|
522
|
-
|
523
|
-
When deploying Firebase apps to production, it is advisable to only import
|
524
|
-
|
525
|
-
the individual SDK components you intend to use.
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
For the module builds, these are available in the following manner
|
530
|
-
|
531
|
-
(replace <PACKAGE> with the name of a component - i.e. auth, database, etc):
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
CommonJS Modules:
|
536
|
-
|
537
|
-
const firebase = require('firebase/app');
|
538
|
-
|
539
|
-
require('firebase/<PACKAGE>');
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
ES Modules:
|
544
|
-
|
545
|
-
import firebase from 'firebase/app';
|
546
|
-
|
547
|
-
import 'firebase/<PACKAGE>';
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
Typescript:
|
552
|
-
|
553
|
-
import firebase from 'firebase/app';
|
554
|
-
|
555
|
-
import 'firebase/<PACKAGE>';
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
なかなか問題が解決できないですが、解決方法を確認しています。
|
2
拡張子を記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -52,7 +52,7 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
-
/components/Users
|
55
|
+
/components/Users.vue
|
56
56
|
|
57
57
|
```Vue
|
58
58
|
|
@@ -424,7 +424,7 @@
|
|
424
424
|
|
425
425
|
|
426
426
|
|
427
|
-
/components/Users
|
427
|
+
/components/Users.vue
|
428
428
|
|
429
429
|
```Vue
|
430
430
|
|
1
追記を追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -159,3 +159,401 @@
|
|
159
159
|
Firebase APIに詳しい方がいましたらご回答いただけないでしょうか?
|
160
160
|
|
161
161
|
よろしくお願いいたします。
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
-- 追記 --
|
166
|
+
|
167
|
+
https://gurutaka-log.com/nuxt-firebase-auth-serviceworker#STEP4firebase-admin
|
168
|
+
|
169
|
+
https://gurutaka-log.com/nuxtserverinit-firestore
|
170
|
+
|
171
|
+
上記サイトを参考に以下のように実装してみました。
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
nuxt.config.json
|
176
|
+
|
177
|
+
```json
|
178
|
+
|
179
|
+
export default {
|
180
|
+
|
181
|
+
(中略)
|
182
|
+
|
183
|
+
workbox: {
|
184
|
+
|
185
|
+
importScripts: [
|
186
|
+
|
187
|
+
"https://www.gstatic.com/firebasejs/7.6.1/firebase-app.js",
|
188
|
+
|
189
|
+
"https://www.gstatic.com/firebasejs/7.6.1/firebase-auth.js",
|
190
|
+
|
191
|
+
"https://www.gstatic.com/firebasejs/6.2.4/firebase-database.js",
|
192
|
+
|
193
|
+
"sw-firebase-auth.js"
|
194
|
+
|
195
|
+
],
|
196
|
+
|
197
|
+
dev: process.env.MODE != "production",
|
198
|
+
|
199
|
+
},
|
200
|
+
|
201
|
+
(省略)
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
static/sw-firebase-auth.js
|
210
|
+
|
211
|
+
```json
|
212
|
+
|
213
|
+
// firebaseを初期化
|
214
|
+
|
215
|
+
firebase.initializeApp({
|
216
|
+
|
217
|
+
apiKey: XXXXXXX,
|
218
|
+
|
219
|
+
authDomain: XXXXXXXXX,
|
220
|
+
|
221
|
+
databaseURL: XXXXXXXXXX,
|
222
|
+
|
223
|
+
projectId: XXXXXXXXXXX,
|
224
|
+
|
225
|
+
storageBucket: XXXXXXXXXXX,
|
226
|
+
|
227
|
+
messagingSenderId: XXXXXXXXXXXXX,
|
228
|
+
|
229
|
+
appId: XXXXXXXXXXXXX,
|
230
|
+
|
231
|
+
measurementId: XXXXXXXXXXXXX
|
232
|
+
|
233
|
+
});
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
// onAuthStateChanged()で現在のuserからidTokenを取得
|
238
|
+
|
239
|
+
const getIdToken = () => {
|
240
|
+
|
241
|
+
return new Promise((resolve, reject) => {
|
242
|
+
|
243
|
+
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
|
244
|
+
|
245
|
+
unsubscribe();
|
246
|
+
|
247
|
+
if (user) {
|
248
|
+
|
249
|
+
user.getIdToken().then(
|
250
|
+
|
251
|
+
idToken => resolve(idToken),
|
252
|
+
|
253
|
+
error => resolve(null)
|
254
|
+
|
255
|
+
);
|
256
|
+
|
257
|
+
} else {
|
258
|
+
|
259
|
+
resolve(null);
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
});
|
264
|
+
|
265
|
+
});
|
266
|
+
|
267
|
+
};
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
// URLからルートのURLを取得する処理
|
272
|
+
|
273
|
+
const getOriginFromUrl = url => {
|
274
|
+
|
275
|
+
// https://stackoverflow.com/questions/1420881/how-to-extract-base-url-from-a-string-in-javascript
|
276
|
+
|
277
|
+
const pathArray = url.split("/");
|
278
|
+
|
279
|
+
const protocol = pathArray[0];
|
280
|
+
|
281
|
+
const host = pathArray[2];
|
282
|
+
|
283
|
+
return protocol + "//" + host;
|
284
|
+
|
285
|
+
};
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
/**
|
290
|
+
|
291
|
+
* Service Workderのライフサイクルでfetchしたときの処理
|
292
|
+
|
293
|
+
*/
|
294
|
+
|
295
|
+
self.addEventListener("fetch", event => {
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
// リクエストをラップして、ヘッダにFirebase AuthのIdTokenを追加する処理
|
300
|
+
|
301
|
+
const requestProcessor = idToken => {
|
302
|
+
|
303
|
+
let req = event.request;
|
304
|
+
|
305
|
+
// URLを取得して、httpsもしくはlocalhostかなどをチェック
|
306
|
+
|
307
|
+
if (self.location.origin == getOriginFromUrl(event.request.url) &&
|
308
|
+
|
309
|
+
(self.location.protocol == "https:" || self.location.hostname == "localhost") &&
|
310
|
+
|
311
|
+
idToken
|
312
|
+
|
313
|
+
) {
|
314
|
+
|
315
|
+
// ヘッダ情報をクローンする
|
316
|
+
|
317
|
+
const headers = new Headers();
|
318
|
+
|
319
|
+
for (let entry of req.headers.entries()) {
|
320
|
+
|
321
|
+
headers.append(entry[0], entry[1]);
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
// クローンしたヘッダにFirebase AuthのIdTokenを追加
|
326
|
+
|
327
|
+
headers.append("Authorization", "Bearer " + idToken);
|
328
|
+
|
329
|
+
try {
|
330
|
+
|
331
|
+
req = new Request(req.url, {
|
332
|
+
|
333
|
+
method: req.method,
|
334
|
+
|
335
|
+
headers: headers,
|
336
|
+
|
337
|
+
mode: "same-origin",
|
338
|
+
|
339
|
+
credentials: req.credentials,
|
340
|
+
|
341
|
+
cache: req.cache,
|
342
|
+
|
343
|
+
redirect: req.redirect,
|
344
|
+
|
345
|
+
referrer: req.referrer,
|
346
|
+
|
347
|
+
body: req.body,
|
348
|
+
|
349
|
+
bodyUsed: req.bodyUsed,
|
350
|
+
|
351
|
+
context: req.context
|
352
|
+
|
353
|
+
});
|
354
|
+
|
355
|
+
} catch (e) {
|
356
|
+
|
357
|
+
console.error(e);
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
return fetch(req);
|
364
|
+
|
365
|
+
};
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
// 上の関数を使って、全リクエストでIdTokenの取得し、Firebase AuthのIdTokenを追加ようにする
|
370
|
+
|
371
|
+
event.respondWith(getIdToken().then(requestProcessor, requestProcessor));
|
372
|
+
|
373
|
+
});
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
/**
|
378
|
+
|
379
|
+
* Service Workderのライフサイクルでactivateしたときの処理
|
380
|
+
|
381
|
+
*/
|
382
|
+
|
383
|
+
self.addEventListener("activate", event => {
|
384
|
+
|
385
|
+
event.waitUntil(clients.claim());
|
386
|
+
|
387
|
+
});
|
388
|
+
|
389
|
+
```
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
plugins/firebaseAdmin.js
|
394
|
+
|
395
|
+
```javascript
|
396
|
+
|
397
|
+
let admin
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
if (process.server) {
|
402
|
+
|
403
|
+
admin = require('firebase-admin')
|
404
|
+
|
405
|
+
if (!admin.apps.length) {
|
406
|
+
|
407
|
+
const serviceAccount = require('key.json')
|
408
|
+
|
409
|
+
admin.initializeApp({
|
410
|
+
|
411
|
+
credential: admin.credential.cert(serviceAccount),
|
412
|
+
|
413
|
+
databaseURL: "XXXXXXXXXXXXXXX"
|
414
|
+
|
415
|
+
})
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
}
|
420
|
+
|
421
|
+
export default admin
|
422
|
+
|
423
|
+
```
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
/components/Users
|
428
|
+
|
429
|
+
```Vue
|
430
|
+
|
431
|
+
<template>
|
432
|
+
|
433
|
+
<div class="container">
|
434
|
+
|
435
|
+
ユーザ一覧
|
436
|
+
|
437
|
+
</div>
|
438
|
+
|
439
|
+
</template>
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
<script>
|
444
|
+
|
445
|
+
export default {
|
446
|
+
|
447
|
+
data() {
|
448
|
+
|
449
|
+
return {};
|
450
|
+
|
451
|
+
},
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
created() {},
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
// 計算等
|
460
|
+
|
461
|
+
computed: {},
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
// 監視
|
466
|
+
|
467
|
+
watch: {
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
},
|
472
|
+
|
473
|
+
filters: {
|
474
|
+
|
475
|
+
},
|
476
|
+
|
477
|
+
mounted() {
|
478
|
+
|
479
|
+
this.nuxtServerInit();
|
480
|
+
|
481
|
+
},
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
methods: {
|
486
|
+
|
487
|
+
async nuxtServerInit() {
|
488
|
+
|
489
|
+
// firebase-adminの初期化
|
490
|
+
|
491
|
+
const admin = require('~/plugins/firebaseAdmin').default;
|
492
|
+
|
493
|
+
let users = admin.auth().listUsers(1000).then((listUsersResult) => {
|
494
|
+
|
495
|
+
console.log('user:', JSON.stringify(listUsersResult));
|
496
|
+
|
497
|
+
});
|
498
|
+
|
499
|
+
}
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
</script>
|
506
|
+
|
507
|
+
```
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
この状態で動作確認すると以下のエラーが発生します。
|
512
|
+
|
513
|
+
Uncaught (in promise) TypeError: Cannot read property 'auth' of undefined
|
514
|
+
|
515
|
+
admin.firestore().collectionの部分でエラーとなってしまいfirebaseの初期化がうまくいきません。
|
516
|
+
|
517
|
+
また、workboxを使ってfirebaseのライブラリを読み込んでいるはずが以下の警告が出てしまいます。
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
It looks like you're using the development build of the Firebase JS SDK.
|
522
|
+
|
523
|
+
When deploying Firebase apps to production, it is advisable to only import
|
524
|
+
|
525
|
+
the individual SDK components you intend to use.
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
For the module builds, these are available in the following manner
|
530
|
+
|
531
|
+
(replace <PACKAGE> with the name of a component - i.e. auth, database, etc):
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
CommonJS Modules:
|
536
|
+
|
537
|
+
const firebase = require('firebase/app');
|
538
|
+
|
539
|
+
require('firebase/<PACKAGE>');
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
ES Modules:
|
544
|
+
|
545
|
+
import firebase from 'firebase/app';
|
546
|
+
|
547
|
+
import 'firebase/<PACKAGE>';
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
Typescript:
|
552
|
+
|
553
|
+
import firebase from 'firebase/app';
|
554
|
+
|
555
|
+
import 'firebase/<PACKAGE>';
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
なかなか問題が解決できないですが、解決方法を確認しています。
|