質問編集履歴

1

改善したコードをのせました。

2021/02/23 11:24

投稿

3aki
3aki

スコア14

test CHANGED
File without changes
test CHANGED
@@ -250,6 +250,264 @@
250
250
 
251
251
 
252
252
 
253
+ 改善したコード
254
+
255
+ ```JavaScript
256
+
257
+ import Firebase, { Firestore } from './Firebase';
258
+
259
+
260
+
261
+ export default class Auth {
262
+
263
+ constructor() {
264
+
265
+ this.user = null;
266
+
267
+ this.checkUuidFlag = false;
268
+
269
+ this.onUuidChecked = result => { };
270
+
271
+
272
+
273
+ Firebase.auth().onAuthStateChanged(user => {
274
+
275
+ console.log("Call onAuthStateChanged.");
276
+
277
+ if (user) {
278
+
279
+ user = this.cuustomUserState(user);
280
+
281
+ console.log("user is ", user);
282
+
283
+ this.user = user;
284
+
285
+ if (this.checkUuidFlag) {
286
+
287
+ this.checkUuidRegisteredInDb();
288
+
289
+ this.checkUuidFlag = false;
290
+
291
+ }
292
+
293
+ } else {
294
+
295
+ console.log("user is null");
296
+
297
+ }
298
+
299
+ });
300
+
301
+ }
302
+
303
+
304
+
305
+ setOnUuidChecked = func => {
306
+
307
+ this.onUuidChecked = func;
308
+
309
+ }
310
+
311
+
312
+
313
+ // Googleでサインイン
314
+
315
+ googleSignIn = () => {
316
+
317
+ console.log("Google Sign In");
318
+
319
+ this.checkUuidFlag = true;
320
+
321
+ const provider = new Firebase.auth.GoogleAuthProvider();
322
+
323
+
324
+
325
+ Firebase.auth().signInWithPopup(provider); // signInWithPopup or signInWithRedirect
326
+
327
+
328
+
329
+ Firebase.auth().getRedirectResult().then(result => {
330
+
331
+ console.log("Google Sign In successful.");
332
+
333
+ if (result != null) console.log("Get result.");
334
+
335
+ }).catch(error => {
336
+
337
+ console.log(error);
338
+
339
+ });
340
+
341
+ }
342
+
343
+
344
+
345
+ // サインアウト
346
+
347
+ signOut = () => {
348
+
349
+ console.log("Sign Out");
350
+
351
+ Firebase.auth().signOut().then(() => {
352
+
353
+ console.log("Sign Out successful.");
354
+
355
+ }).catch(error => {
356
+
357
+ console.log(error);
358
+
359
+ });
360
+
361
+ }
362
+
363
+
364
+
365
+ // DBに登録があるか確認をする
366
+
367
+ checkUuidRegisteredInDb = () => {
368
+
369
+ if (this.user == null) {
370
+
371
+ this.onUuidChecked(false);
372
+
373
+ return;
374
+
375
+ }
376
+
377
+ const uuid = this.user.uid;
378
+
379
+
380
+
381
+ console.log("User Id is ", uuid);
382
+
383
+ Firestore.collection("members").where("userId", "==", uuid)
384
+
385
+ .get()
386
+
387
+ .then(querySnapshot => {
388
+
389
+ console.log("Check members database.");
390
+
391
+ let result = false;
392
+
393
+ querySnapshot.some(doc => {
394
+
395
+ if (doc.data().uuid == uuid) {
396
+
397
+ console.log("Match uuid.");
398
+
399
+ result = true;
400
+
401
+ return true; // breakの役割
402
+
403
+ }
404
+
405
+ });
406
+
407
+ this.onUuidChecked(result);
408
+
409
+ })
410
+
411
+ .catch(error => {
412
+
413
+ console.log("Error getting documents: ", error);
414
+
415
+ this.onUuidChecked(false);
416
+
417
+ });
418
+
419
+ }
420
+
421
+
422
+
423
+ cuustomUserState = user => {
424
+
425
+ const user_ = {
426
+
427
+ displayName: null,
428
+
429
+ email: null,
430
+
431
+ emailVerified: null,
432
+
433
+ f: null,
434
+
435
+ fb: null,
436
+
437
+ isAnonymous: null,
438
+
439
+ l: null,
440
+
441
+ m: null,
442
+
443
+ oa: null,
444
+
445
+ pa: null,
446
+
447
+ phoneNumber: null,
448
+
449
+ photoURL: null,
450
+
451
+ refreshToken: null,
452
+
453
+ tenantId: null,
454
+
455
+ uid: null,
456
+
457
+ xa: null,
458
+
459
+ ya: null,
460
+
461
+ };
462
+
463
+
464
+
465
+ user_.displayName = user.displayName;
466
+
467
+ user_.email = user.email;
468
+
469
+ user_.emailVerified = user.emailVerified;
470
+
471
+ user_.f = user.f;
472
+
473
+ user_.fb = user.fb;
474
+
475
+ user_.isAnonymous = user.isAnonymous;
476
+
477
+ user_.l = user.l;
478
+
479
+ user_.m = user.m;
480
+
481
+ user_.oa = user.oa;
482
+
483
+ user_.pa = user.pa;
484
+
485
+ user_.phoneNumber = user.phoneNumber;
486
+
487
+ user_.photoURL = user.photoURL;
488
+
489
+ user_.refreshToken = user.refreshToken;
490
+
491
+ user_.tenantId = user.tenantId;
492
+
493
+ user_.uid = user.uid;
494
+
495
+ user_.xa = user.xa;
496
+
497
+ user_.ya = user.ya;
498
+
499
+
500
+
501
+ return user_;
502
+
503
+ }
504
+
505
+ }
506
+
507
+ ```
508
+
509
+
510
+
253
511
  ### 補足情報(FW/ツールのバージョンなど)
254
512
 
255
513