質問編集履歴
1
改善したコードをのせました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -124,6 +124,135 @@
|
|
124
124
|
}
|
125
125
|
```
|
126
126
|
|
127
|
+
改善したコード
|
128
|
+
```JavaScript
|
129
|
+
import Firebase, { Firestore } from './Firebase';
|
130
|
+
|
131
|
+
export default class Auth {
|
132
|
+
constructor() {
|
133
|
+
this.user = null;
|
134
|
+
this.checkUuidFlag = false;
|
135
|
+
this.onUuidChecked = result => { };
|
136
|
+
|
137
|
+
Firebase.auth().onAuthStateChanged(user => {
|
138
|
+
console.log("Call onAuthStateChanged.");
|
139
|
+
if (user) {
|
140
|
+
user = this.cuustomUserState(user);
|
141
|
+
console.log("user is ", user);
|
142
|
+
this.user = user;
|
143
|
+
if (this.checkUuidFlag) {
|
144
|
+
this.checkUuidRegisteredInDb();
|
145
|
+
this.checkUuidFlag = false;
|
146
|
+
}
|
147
|
+
} else {
|
148
|
+
console.log("user is null");
|
149
|
+
}
|
150
|
+
});
|
151
|
+
}
|
152
|
+
|
153
|
+
setOnUuidChecked = func => {
|
154
|
+
this.onUuidChecked = func;
|
155
|
+
}
|
156
|
+
|
157
|
+
// Googleでサインイン
|
158
|
+
googleSignIn = () => {
|
159
|
+
console.log("Google Sign In");
|
160
|
+
this.checkUuidFlag = true;
|
161
|
+
const provider = new Firebase.auth.GoogleAuthProvider();
|
162
|
+
|
163
|
+
Firebase.auth().signInWithPopup(provider); // signInWithPopup or signInWithRedirect
|
164
|
+
|
165
|
+
Firebase.auth().getRedirectResult().then(result => {
|
166
|
+
console.log("Google Sign In successful.");
|
167
|
+
if (result != null) console.log("Get result.");
|
168
|
+
}).catch(error => {
|
169
|
+
console.log(error);
|
170
|
+
});
|
171
|
+
}
|
172
|
+
|
173
|
+
// サインアウト
|
174
|
+
signOut = () => {
|
175
|
+
console.log("Sign Out");
|
176
|
+
Firebase.auth().signOut().then(() => {
|
177
|
+
console.log("Sign Out successful.");
|
178
|
+
}).catch(error => {
|
179
|
+
console.log(error);
|
180
|
+
});
|
181
|
+
}
|
182
|
+
|
183
|
+
// DBに登録があるか確認をする
|
184
|
+
checkUuidRegisteredInDb = () => {
|
185
|
+
if (this.user == null) {
|
186
|
+
this.onUuidChecked(false);
|
187
|
+
return;
|
188
|
+
}
|
189
|
+
const uuid = this.user.uid;
|
190
|
+
|
191
|
+
console.log("User Id is ", uuid);
|
192
|
+
Firestore.collection("members").where("userId", "==", uuid)
|
193
|
+
.get()
|
194
|
+
.then(querySnapshot => {
|
195
|
+
console.log("Check members database.");
|
196
|
+
let result = false;
|
197
|
+
querySnapshot.some(doc => {
|
198
|
+
if (doc.data().uuid == uuid) {
|
199
|
+
console.log("Match uuid.");
|
200
|
+
result = true;
|
201
|
+
return true; // breakの役割
|
202
|
+
}
|
203
|
+
});
|
204
|
+
this.onUuidChecked(result);
|
205
|
+
})
|
206
|
+
.catch(error => {
|
207
|
+
console.log("Error getting documents: ", error);
|
208
|
+
this.onUuidChecked(false);
|
209
|
+
});
|
210
|
+
}
|
211
|
+
|
212
|
+
cuustomUserState = user => {
|
213
|
+
const user_ = {
|
214
|
+
displayName: null,
|
215
|
+
email: null,
|
216
|
+
emailVerified: null,
|
217
|
+
f: null,
|
218
|
+
fb: null,
|
219
|
+
isAnonymous: null,
|
220
|
+
l: null,
|
221
|
+
m: null,
|
222
|
+
oa: null,
|
223
|
+
pa: null,
|
224
|
+
phoneNumber: null,
|
225
|
+
photoURL: null,
|
226
|
+
refreshToken: null,
|
227
|
+
tenantId: null,
|
228
|
+
uid: null,
|
229
|
+
xa: null,
|
230
|
+
ya: null,
|
231
|
+
};
|
232
|
+
|
233
|
+
user_.displayName = user.displayName;
|
234
|
+
user_.email = user.email;
|
235
|
+
user_.emailVerified = user.emailVerified;
|
236
|
+
user_.f = user.f;
|
237
|
+
user_.fb = user.fb;
|
238
|
+
user_.isAnonymous = user.isAnonymous;
|
239
|
+
user_.l = user.l;
|
240
|
+
user_.m = user.m;
|
241
|
+
user_.oa = user.oa;
|
242
|
+
user_.pa = user.pa;
|
243
|
+
user_.phoneNumber = user.phoneNumber;
|
244
|
+
user_.photoURL = user.photoURL;
|
245
|
+
user_.refreshToken = user.refreshToken;
|
246
|
+
user_.tenantId = user.tenantId;
|
247
|
+
user_.uid = user.uid;
|
248
|
+
user_.xa = user.xa;
|
249
|
+
user_.ya = user.ya;
|
250
|
+
|
251
|
+
return user_;
|
252
|
+
}
|
253
|
+
}
|
254
|
+
```
|
255
|
+
|
127
256
|
### 補足情報(FW/ツールのバージョンなど)
|
128
257
|
|
129
258
|
"@testing-library/jest-dom": "^5.11.6",
|