質問編集履歴

14

修正

2020/09/01 08:46

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -94,6 +94,16 @@
94
94
 
95
95
  at dispatchDiscreteEvent (react-dom.development.js?61bb:5881)
96
96
 
97
+
98
+
99
+
100
+
101
+
102
+
103
+ Failed to launch editor: TypeError: editor.toLowerCase is not a function
104
+
105
+
106
+
97
107
  ```
98
108
 
99
109
 

13

修正

2020/09/01 08:46

投稿

tomsuma
tomsuma

スコア38

test CHANGED
@@ -1 +1 @@
1
- TypeError: SignUp is not a functionが消えません
1
+ TypeError: SignUp is not a functionエラーにより、ログイン機能実装できない
test CHANGED
File without changes

12

修正

2020/09/01 05:11

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -6,15 +6,11 @@
6
6
 
7
7
  Next.jsを用いたログイン機能のを実装していまして、
8
8
 
9
- Login画面には問題なく移行できるのですが、
9
+ SignIn画面には問題なく移行できるのですが、
10
-
11
- ~~ログインボタンを押すとこのようなエラーが出ます~~、、
10
+
12
-
13
- サインインしていないのでログインできないのは当然でした
11
+
14
-
15
-
16
-
12
+
17
- サインインボタンを押すとエラーが起きます、、
13
+ サインインボタンを押すとエラーが起きます
18
14
 
19
15
 
20
16
 

11

修正

2020/09/01 05:11

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -423,3 +423,7 @@
423
423
  Firebase library is only loaded once.
424
424
 
425
425
  ```
426
+
427
+
428
+
429
+ ![イメージ説明](280f2068b78b4af7ecb4b55779a9baaf.png)

10

エラー修正

2020/09/01 01:38

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -414,7 +414,7 @@
414
414
 
415
415
  ```
416
416
 
417
- issue
417
+ 検証ツールにて
418
418
 
419
419
  index.esm.js?abfd:106 [2020-09-01T01:16:47.798Z] @firebase/app:
420
420
 

9

修正

2020/09/01 01:20

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -409,3 +409,17 @@
409
409
  };
410
410
 
411
411
  ```
412
+
413
+
414
+
415
+ ```
416
+
417
+ issue
418
+
419
+ index.esm.js?abfd:106 [2020-09-01T01:16:47.798Z] @firebase/app:
420
+
421
+ Warning: Firebase is already defined in the global scope. Please make sure
422
+
423
+ Firebase library is only loaded once.
424
+
425
+ ```

8

修正

2020/09/01 01:17

投稿

tomsuma
tomsuma

スコア38

test CHANGED
@@ -1 +1 @@
1
- TypeError: createLogin is not a functionが消えません
1
+ TypeError: SignUp is not a functionが消えません
test CHANGED
@@ -1,4 +1,4 @@
1
- TypeError: createLogin is not a functionエラーが消えません
1
+ TypeError: SignUp is not a functionエラーが消えません
2
2
 
3
3
 
4
4
 
@@ -8,7 +8,15 @@
8
8
 
9
9
  Login画面には問題なく移行できるのですが、
10
10
 
11
- ログインボタンを押すとこのようなエラーが出ます、、
11
+ ~~ログインボタンを押すとこのようなエラーが出ます~~、、
12
+
13
+ サインインしていないのでログインできないのは当然でした
14
+
15
+
16
+
17
+ サインインボタンを押すとエラーが起きます、、
18
+
19
+
12
20
 
13
21
 
14
22
 
@@ -20,55 +28,9 @@
20
28
 
21
29
 
22
30
 
23
- 試したこと
31
+
24
-
25
- ターミナルで
32
+
26
-
27
- ```
33
+
28
-
29
-
30
-
31
- Could not open Login.js in the editor.
32
-
33
-
34
-
35
- To set up the editor integration, add something like REACT_EDITOR=atom to the .env.local file in your project folder and restart the development server.
36
-
37
-
38
-
39
- ```
40
-
41
- と合ったので
42
-
43
- ```
44
-
45
- .env.local
46
-
47
-
48
-
49
- # TODO. Fill in with Firebase Config
50
-
51
- NEXT_PUBLIC_FIREBASE_API_KEY="*********r8",
52
-
53
- NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN="ha********",
54
-
55
- NEXT_PUBLIC_FIREBASE_DATABASE_URL="******",
56
-
57
- NEXT_PUBLIC_FIREBASE_PROJECT_ID="h*******a",
58
-
59
- NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET="h*****m",
60
-
61
- NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=*****",
62
-
63
- NEXT_PUBLIC_FIREBASE_APP_ID=****44",
64
-
65
- NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID="****"
66
-
67
- REACT_EDITOR = atom  追加
68
-
69
- ```
70
-
71
- としたらこのエラーは消えました
72
34
 
73
35
 
74
36
 
@@ -80,67 +42,69 @@
80
42
 
81
43
 
82
44
 
83
- SignUpがコンテキストから取れてないと思うのですが、どうすれば原因が特定できるでしょうか
84
-
85
-
86
-
87
- ```
88
-
89
- Login.js?5292:11 Uncaught TypeError: createLogin is not a function
90
-
91
- at handleSubmit (Login.js?5292:11)
92
-
93
- at HTMLUnknownElement.callCallback (react-dom.development.js?61bb:188)
94
-
95
- at Object.invokeGuardedCallbackDev (react-dom.development.js?61bb:237)
96
-
97
- at invokeGuardedCallback (react-dom.development.js?61bb:292)
98
-
99
- at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js?61bb:306)
100
-
101
- at executeDispatch (react-dom.development.js?61bb:389)
102
-
103
- at executeDispatchesInOrder (react-dom.development.js?61bb:414)
104
-
105
- at executeDispatchesAndRelease (react-dom.development.js?61bb:3278)
106
-
107
- at executeDispatchesAndReleaseTopLevel (react-dom.development.js?61bb:3287)
108
-
109
- at forEachAccumulated (react-dom.development.js?61bb:3259)
110
-
111
- at runEventsInBatch (react-dom.development.js?61bb:3304)
112
-
113
- at runExtractedPluginEventsInBatch (react-dom.development.js?61bb:3514)
114
-
115
- at handleTopLevel (react-dom.development.js?61bb:3558)
116
-
117
- at batchedEventUpdates$1 (react-dom.development.js?61bb:21871)
118
-
119
- at batchedEventUpdates (react-dom.development.js?61bb:795)
120
-
121
- at dispatchEventForLegacyPluginEventSystem (react-dom.development.js?61bb:3568)
122
-
123
- at attemptToDispatchEvent (react-dom.development.js?61bb:4267)
124
-
125
- at dispatchEvent (react-dom.development.js?61bb:4189)
126
-
127
- at unstable_runWithPriority (scheduler.development.js?3069:653)
128
-
129
- at runWithPriority$1 (react-dom.development.js?61bb:11039)
130
-
131
- at discreteUpdates$1 (react-dom.development.js?61bb:21887)
132
-
133
- at discreteUpdates (react-dom.development.js?61bb:806)
134
-
135
-
136
-
137
- **省略
138
-
139
- ```
140
-
141
- ```
142
-
143
- LogIn.js
45
+ ```
46
+
47
+ ターミナルでのエラー
48
+
49
+
50
+
51
+ react-dom.development.js?61bb:475 Uncaught TypeError: SignUp is not a function
52
+
53
+
54
+
55
+ at handleSubmit (SignUp.js?b395:11)
56
+
57
+ at HTMLUnknownElement.callCallback (react-dom.development.js?61bb:336)
58
+
59
+ at Object.invokeGuardedCallbackDev (react-dom.development.js?61bb:385)
60
+
61
+ at invokeGuardedCallback (react-dom.development.js?61bb:440)
62
+
63
+ at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js?61bb:454)
64
+
65
+ at executeDispatch (react-dom.development.js?61bb:584)
66
+
67
+ at executeDispatchesInOrder (react-dom.development.js?61bb:609)
68
+
69
+ at executeDispatchesAndRelease (react-dom.development.js?61bb:713)
70
+
71
+ at executeDispatchesAndReleaseTopLevel (react-dom.development.js?61bb:722)
72
+
73
+ at forEachAccumulated (react-dom.development.js?61bb:694)
74
+
75
+ at runEventsInBatch (react-dom.development.js?61bb:739)
76
+
77
+ at runExtractedPluginEventsInBatch (react-dom.development.js?61bb:880)
78
+
79
+ at handleTopLevel (react-dom.development.js?61bb:5803)
80
+
81
+ at batchedEventUpdates$1 (react-dom.development.js?61bb:24401)
82
+
83
+ at batchedEventUpdates (react-dom.development.js?61bb:1415)
84
+
85
+ at dispatchEventForPluginEventSystem (react-dom.development.js?61bb:5894)
86
+
87
+ at attemptToDispatchEvent (react-dom.development.js?61bb:6010)
88
+
89
+ at dispatchEvent (react-dom.development.js?61bb:5914)
90
+
91
+ at unstable_runWithPriority (scheduler.development.js?3069:697)
92
+
93
+ at runWithPriority$2 (react-dom.development.js?61bb:12149)
94
+
95
+ at discreteUpdates$1 (react-dom.development.js?61bb:24417)
96
+
97
+ at discreteUpdates (react-dom.development.js?61bb:1438)
98
+
99
+ at dispatchDiscreteEvent (react-dom.development.js?61bb:5881)
100
+
101
+ ```
102
+
103
+
104
+
105
+ ```
106
+
107
+ SignUp.js
144
108
 
145
109
 
146
110
 
@@ -152,13 +116,11 @@
152
116
 
153
117
 
154
118
 
155
- const Login = ( {history}) => {
119
+ const SignUp = ( ) => {
156
-
120
+
157
- const Login = useContext(AuthContext);
121
+ const SignUp = useContext(AuthContext);
158
-
159
-
160
-
161
- // AuthContextからlogin関数を受け取る
122
+
123
+
162
124
 
163
125
  const handleSubmit = event => {
164
126
 
@@ -166,7 +128,9 @@
166
128
 
167
129
  const { email, password } = event.target.elements;
168
130
 
169
- Login(email.value, password.value,history);
131
+ SignUp(email.value, password.value);
132
+
133
+ console.log(SignUp)
170
134
 
171
135
  };
172
136
 
@@ -176,7 +140,7 @@
176
140
 
177
141
  <div>
178
142
 
179
- <h1>Log in</h1>
143
+ <h1>Sign up</h1>
180
144
 
181
145
  <form onSubmit={handleSubmit}>
182
146
 
@@ -196,7 +160,7 @@
196
160
 
197
161
  </label>
198
162
 
199
- <button type="submit">Log in</button>
163
+ <button type="submit">Sign Up</button>
200
164
 
201
165
  </form>
202
166
 
@@ -208,314 +172,240 @@
208
172
 
209
173
 
210
174
 
211
- export default withRouter(Login);
175
+ export default withRouter(SignUp);
212
-
176
+
213
- ```
177
+ ```
214
-
178
+
215
- ```
179
+ ```
216
-
180
+
217
- SignUp.js
181
+ AuthProvider.jsx
218
-
219
-
220
-
182
+
183
+
184
+
221
- import React, { useContext } from "react";
185
+ import React, { useEffect, useState } from "react";
222
-
223
- import { AuthContext } from "./AuthProvider";
186
+
224
-
225
- import { withRouter } from 'next/router'
187
+
226
-
227
-
228
-
188
+
229
- const SignUp = ( ) => {
189
+ // contextの作成
230
-
190
+
231
- const SignUp = useContext(AuthContext);
191
+ export const AuthContext = React.createContext();
192
+
193
+
194
+
232
-
195
+ export const AuthProvider = ({ children }) => {
196
+
233
-
197
+ const [currentUser, setCurrentUser] = useState(null);
198
+
199
+
200
+
234
-
201
+ // ユーザーをログインさせる関数
202
+
235
- const handleSubmit = event => {
203
+ const Login = async (email, password) => {
204
+
236
-
205
+ try {
206
+
207
+ await app.auth().signInWithEmailAndPassword(email, password);
208
+
237
- event.preventDefault();
209
+ router.push("/");
238
-
239
- const { email, password } = event.target.elements;
210
+
240
-
241
- SignUp(email.value, password.value);
211
+ } catch (error) {
242
-
212
+
243
- console.log(SignUp)
213
+ alert(error);
214
+
215
+ }
244
216
 
245
217
  };
246
218
 
247
219
 
248
220
 
221
+ // 新しいユーザーを作成しログインさせる関数
222
+
223
+ const SignUp = async (email, password, history) => {
224
+
225
+ try {
226
+
227
+ await app.auth().createUserWithEmailAndPassword(email, password);
228
+
229
+ router.push("/");
230
+
231
+ } catch (error) {
232
+
233
+ alert(error);
234
+
235
+ }
236
+
237
+ };
238
+
239
+
240
+
241
+ useEffect(() => {
242
+
243
+ app.auth().onAuthStateChanged(setCurrentUser);
244
+
245
+ }, []);
246
+
247
+
248
+
249
249
  return (
250
250
 
251
- <div>
251
+ // Contextを使用して認証に必要な情報をコンポーネントツリーに流し込む。
252
-
252
+
253
- <h1>Sign up</h1>
253
+ <AuthContext.Provider
254
-
255
- <form onSubmit={handleSubmit}>
254
+
256
-
257
- <label>
258
-
259
- Email
260
-
261
- <input name="email" type="email" placeholder="Email" />
262
-
263
- </label>
264
-
265
- <label>
266
-
267
- Password
255
+ value={{
268
-
256
+
269
- <input name="password" type="password" placeholder="Password" />
257
+ Login: Login,
270
-
258
+
271
- </label>
259
+ SignUp: SignUp,
272
-
260
+
273
- <button type="submit">Sign Up</button>
261
+ currentUser
262
+
274
-
263
+ }}
264
+
265
+ >
266
+
267
+ {children}
268
+
275
- </form>
269
+ </AuthContext.Provider>
276
-
277
- </div>
278
270
 
279
271
  );
280
272
 
281
273
  };
282
274
 
283
-
284
-
285
- export default withRouter(SignUp);
286
-
287
- ```
275
+ ```
276
+
288
-
277
+ userContext.js
278
+
289
- ```
279
+ ```
290
-
291
- AuthProvider.jsx
280
+
292
-
293
-
294
-
281
+
282
+
283
+
284
+
295
- import React, { useEffect, useState } from "react";
285
+ import { useState, useEffect, createContext, useContext } from 'react'
296
-
297
-
298
-
286
+
299
- // contextの作成
287
+ import firebase from '../firebase/clientApp'
300
-
288
+
289
+
290
+
301
- export const AuthContext = React.createContext();
291
+ export const UserContext = createContext()
302
-
303
-
304
-
292
+
293
+
294
+
305
- export const AuthProvider = ({ children }) => {
295
+ export default function UserContextComp({ children }) {
306
-
296
+
307
- const [currentUser, setCurrentUser] = useState(null);
297
+ const [user, setUser] = useState(null)
308
-
309
-
310
-
311
- // ユーザーをログインさせる関数
298
+
312
-
313
- const Login = async (email, password) => {
314
-
315
- try {
316
-
317
- await app.auth().signInWithEmailAndPassword(email, password);
318
-
319
- router.push("/");
320
-
321
- } catch (error) {
322
-
323
- alert(error);
324
-
325
- }
326
-
327
- };
328
-
329
-
330
-
331
- // 新しいユーザーを作成しログインさせる関数
332
-
333
- const SignUp = async (email, password, history) => {
334
-
335
- try {
336
-
337
- await app.auth().createUserWithEmailAndPassword(email, password);
299
+ const [loadingUser, setLoadingUser] = useState(true) // Helpful, to update the UI accordingly.
338
-
339
- router.push("/");
340
-
341
- } catch (error) {
342
-
343
- alert(error);
344
-
345
- }
346
-
347
- };
348
300
 
349
301
 
350
302
 
351
303
  useEffect(() => {
352
304
 
305
+ // Listen authenticated user
306
+
353
- app.auth().onAuthStateChanged(setCurrentUser);
307
+ const unsubscriber = firebase.auth().onAuthStateChanged(async (user) => {
308
+
354
-
309
+ try {
310
+
311
+ if (user) {
312
+
313
+ // User is signed in.
314
+
315
+ const { uid, displayName, email } = user
316
+
317
+ // You could also look for the user doc in your Firestore (if you have one):
318
+
319
+ // const userDoc = await firebase.firestore().doc(`users/${uid}`).get()
320
+
321
+ setUser({ uid, displayName, email })
322
+
323
+ } else setUser(null)
324
+
325
+ } catch (error) {
326
+
327
+ // Most probably a connection error. Handle appropriately.
328
+
329
+ } finally {
330
+
331
+ setLoadingUser(false)
332
+
333
+ }
334
+
335
+ })
336
+
337
+
338
+
339
+ // Unsubscribe auth listener on unmount
340
+
341
+ return () => unsubscriber()
342
+
355
- }, []);
343
+ }, [])
356
344
 
357
345
 
358
346
 
359
347
  return (
360
348
 
361
- // Contextを使用して認証に必要な情報をコンポーネントツリーに流し込む。
349
+ <UserContext.Provider value={{ user, setUser, loadingUser ,setLoadingUser}}>
362
-
363
- <AuthContext.Provider
364
-
365
- value={{
366
-
367
- Login: Login,
368
-
369
- SignUp: SignUp,
370
-
371
- currentUser
372
-
373
- }}
374
-
375
- >
376
350
 
377
351
  {children}
378
352
 
379
- </AuthContext.Provider>
353
+ </UserContext.Provider>
354
+
380
-
355
+ )
356
+
357
+ }
358
+
359
+
360
+
361
+ // Custom hook that shorhands the context!
362
+
363
+ export const useUser = () => useContext(UserContext)
364
+
365
+
366
+
367
+ ```
368
+
369
+ また、[next.config.js](https://nextjs.org/docs/api-reference/next.config.js/introduction)この記事を読んでconfigにも自身がなくなりました、
370
+
371
+
372
+
373
+ ```
374
+
375
+
376
+
377
+ require("dotenv").config();
378
+
379
+ const path = require("path");
380
+
381
+ const Dotenv = require("dotenv-webpack");
382
+
383
+ module.exports = {
384
+
385
+ webpack: config => {
386
+
387
+ config.plugins = config.plugins || [];
388
+
389
+ config.plugins = [
390
+
391
+ ...config.plugins,
392
+
393
+ // Read the .env file
394
+
395
+ new Dotenv({
396
+
397
+ path: path.join(__dirname, ".env"),
398
+
399
+ systemvars: true
400
+
401
+ })
402
+
381
- );
403
+ ];
404
+
405
+ return config;
406
+
407
+ }
382
408
 
383
409
  };
384
410
 
385
411
  ```
386
-
387
- userContext.js
388
-
389
- ```
390
-
391
-
392
-
393
-
394
-
395
- import { useState, useEffect, createContext, useContext } from 'react'
396
-
397
- import firebase from '../firebase/clientApp'
398
-
399
-
400
-
401
- export const UserContext = createContext()
402
-
403
-
404
-
405
- export default function UserContextComp({ children }) {
406
-
407
- const [user, setUser] = useState(null)
408
-
409
- const [loadingUser, setLoadingUser] = useState(true) // Helpful, to update the UI accordingly.
410
-
411
-
412
-
413
- useEffect(() => {
414
-
415
- // Listen authenticated user
416
-
417
- const unsubscriber = firebase.auth().onAuthStateChanged(async (user) => {
418
-
419
- try {
420
-
421
- if (user) {
422
-
423
- // User is signed in.
424
-
425
- const { uid, displayName, email } = user
426
-
427
- // You could also look for the user doc in your Firestore (if you have one):
428
-
429
- // const userDoc = await firebase.firestore().doc(`users/${uid}`).get()
430
-
431
- setUser({ uid, displayName, email })
432
-
433
- } else setUser(null)
434
-
435
- } catch (error) {
436
-
437
- // Most probably a connection error. Handle appropriately.
438
-
439
- } finally {
440
-
441
- setLoadingUser(false)
442
-
443
- }
444
-
445
- })
446
-
447
-
448
-
449
- // Unsubscribe auth listener on unmount
450
-
451
- return () => unsubscriber()
452
-
453
- }, [])
454
-
455
-
456
-
457
- return (
458
-
459
- <UserContext.Provider value={{ user, setUser, loadingUser ,setLoadingUser}}>
460
-
461
- {children}
462
-
463
- </UserContext.Provider>
464
-
465
- )
466
-
467
- }
468
-
469
-
470
-
471
- // Custom hook that shorhands the context!
472
-
473
- export const useUser = () => useContext(UserContext)
474
-
475
-
476
-
477
- ```
478
-
479
- また、[next.config.js](https://nextjs.org/docs/api-reference/next.config.js/introduction)この記事を読んでconfigにも自身がなくなりました、
480
-
481
-
482
-
483
- ```
484
-
485
-
486
-
487
- require("dotenv").config();
488
-
489
- const path = require("path");
490
-
491
- const Dotenv = require("dotenv-webpack");
492
-
493
- module.exports = {
494
-
495
- webpack: config => {
496
-
497
- config.plugins = config.plugins || [];
498
-
499
- config.plugins = [
500
-
501
- ...config.plugins,
502
-
503
- // Read the .env file
504
-
505
- new Dotenv({
506
-
507
- path: path.join(__dirname, ".env"),
508
-
509
- systemvars: true
510
-
511
- })
512
-
513
- ];
514
-
515
- return config;
516
-
517
- }
518
-
519
- };
520
-
521
- ```

7

修正

2020/09/01 01:11

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -148,11 +148,13 @@
148
148
 
149
149
  import { AuthContext } from "./AuthProvider";
150
150
 
151
-
151
+ import { withRouter } from 'next/router'
152
-
152
+
153
+
154
+
153
- const createLogin = ( ) => {
155
+ const Login = ( {history}) => {
154
-
156
+
155
- const createLogin = useContext(AuthContext);
157
+ const Login = useContext(AuthContext);
156
158
 
157
159
 
158
160
 
@@ -164,7 +166,7 @@
164
166
 
165
167
  const { email, password } = event.target.elements;
166
168
 
167
- createLogin(email.value, password.value, history);
169
+ Login(email.value, password.value,history);
168
170
 
169
171
  };
170
172
 
@@ -206,7 +208,81 @@
206
208
 
207
209
 
208
210
 
209
- export default createLogin;
211
+ export default withRouter(Login);
212
+
213
+ ```
214
+
215
+ ```
216
+
217
+ SignUp.js
218
+
219
+
220
+
221
+ import React, { useContext } from "react";
222
+
223
+ import { AuthContext } from "./AuthProvider";
224
+
225
+ import { withRouter } from 'next/router'
226
+
227
+
228
+
229
+ const SignUp = ( ) => {
230
+
231
+ const SignUp = useContext(AuthContext);
232
+
233
+
234
+
235
+ const handleSubmit = event => {
236
+
237
+ event.preventDefault();
238
+
239
+ const { email, password } = event.target.elements;
240
+
241
+ SignUp(email.value, password.value);
242
+
243
+ console.log(SignUp)
244
+
245
+ };
246
+
247
+
248
+
249
+ return (
250
+
251
+ <div>
252
+
253
+ <h1>Sign up</h1>
254
+
255
+ <form onSubmit={handleSubmit}>
256
+
257
+ <label>
258
+
259
+ Email
260
+
261
+ <input name="email" type="email" placeholder="Email" />
262
+
263
+ </label>
264
+
265
+ <label>
266
+
267
+ Password
268
+
269
+ <input name="password" type="password" placeholder="Password" />
270
+
271
+ </label>
272
+
273
+ <button type="submit">Sign Up</button>
274
+
275
+ </form>
276
+
277
+ </div>
278
+
279
+ );
280
+
281
+ };
282
+
283
+
284
+
285
+ export default withRouter(SignUp);
210
286
 
211
287
  ```
212
288
 
@@ -234,13 +310,13 @@
234
310
 
235
311
  // ユーザーをログインさせる関数
236
312
 
237
- const createLogin = async (email, password, history) => {
313
+ const Login = async (email, password) => {
238
314
 
239
315
  try {
240
316
 
241
317
  await app.auth().signInWithEmailAndPassword(email, password);
242
318
 
243
- history.push("/");   Next.jsでhistoryは使わない?
319
+ router.push("/");
244
320
 
245
321
  } catch (error) {
246
322
 
@@ -254,13 +330,13 @@
254
330
 
255
331
  // 新しいユーザーを作成しログインさせる関数
256
332
 
257
- const signup = async (email, password, history) => {
333
+ const SignUp = async (email, password, history) => {
258
334
 
259
335
  try {
260
336
 
261
337
  await app.auth().createUserWithEmailAndPassword(email, password);
262
338
 
263
- history.push("/");
339
+ router.push("/");
264
340
 
265
341
  } catch (error) {
266
342
 
@@ -288,9 +364,9 @@
288
364
 
289
365
  value={{
290
366
 
291
- createLogin: createLogin,
367
+ Login: Login,
292
-
368
+
293
- signup: signup,
369
+ SignUp: SignUp,
294
370
 
295
371
  currentUser
296
372
 
@@ -308,7 +384,97 @@
308
384
 
309
385
  ```
310
386
 
311
-
387
+ userContext.js
388
+
389
+ ```
390
+
391
+
392
+
393
+
394
+
395
+ import { useState, useEffect, createContext, useContext } from 'react'
396
+
397
+ import firebase from '../firebase/clientApp'
398
+
399
+
400
+
401
+ export const UserContext = createContext()
402
+
403
+
404
+
405
+ export default function UserContextComp({ children }) {
406
+
407
+ const [user, setUser] = useState(null)
408
+
409
+ const [loadingUser, setLoadingUser] = useState(true) // Helpful, to update the UI accordingly.
410
+
411
+
412
+
413
+ useEffect(() => {
414
+
415
+ // Listen authenticated user
416
+
417
+ const unsubscriber = firebase.auth().onAuthStateChanged(async (user) => {
418
+
419
+ try {
420
+
421
+ if (user) {
422
+
423
+ // User is signed in.
424
+
425
+ const { uid, displayName, email } = user
426
+
427
+ // You could also look for the user doc in your Firestore (if you have one):
428
+
429
+ // const userDoc = await firebase.firestore().doc(`users/${uid}`).get()
430
+
431
+ setUser({ uid, displayName, email })
432
+
433
+ } else setUser(null)
434
+
435
+ } catch (error) {
436
+
437
+ // Most probably a connection error. Handle appropriately.
438
+
439
+ } finally {
440
+
441
+ setLoadingUser(false)
442
+
443
+ }
444
+
445
+ })
446
+
447
+
448
+
449
+ // Unsubscribe auth listener on unmount
450
+
451
+ return () => unsubscriber()
452
+
453
+ }, [])
454
+
455
+
456
+
457
+ return (
458
+
459
+ <UserContext.Provider value={{ user, setUser, loadingUser ,setLoadingUser}}>
460
+
461
+ {children}
462
+
463
+ </UserContext.Provider>
464
+
465
+ )
466
+
467
+ }
468
+
469
+
470
+
471
+ // Custom hook that shorhands the context!
472
+
473
+ export const useUser = () => useContext(UserContext)
474
+
475
+
476
+
477
+ ```
312
478
 
313
479
  また、[next.config.js](https://nextjs.org/docs/api-reference/next.config.js/introduction)この記事を読んでconfigにも自身がなくなりました、
314
480
 

6

修正

2020/09/01 00:54

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
-
5
+ プログラミング初心者です
6
6
 
7
7
  Next.jsを用いたログイン機能のを実装していまして、
8
8
 
@@ -80,7 +80,7 @@
80
80
 
81
81
 
82
82
 
83
-
83
+ SignUpがコンテキストから取れてないと思うのですが、どうすれば原因が特定できるでしょうか
84
84
 
85
85
 
86
86
 

5

修正

2020/09/01 00:42

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -138,6 +138,8 @@
138
138
 
139
139
  ```
140
140
 
141
+ ```
142
+
141
143
  LogIn.js
142
144
 
143
145
 

4

修正

2020/08/31 23:44

投稿

tomsuma
tomsuma

スコア38

test CHANGED
@@ -1 +1 @@
1
- TypeError: createLogin is not a function
1
+ TypeError: createLogin is not a functionが消えません
test CHANGED
File without changes

3

TypeError: createLogin is not a functionエラーが消えません

2020/08/31 23:43

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,9 @@
1
+ TypeError: createLogin is not a functionエラーが消えません
2
+
3
+
4
+
5
+
6
+
1
7
  Next.jsを用いたログイン機能のを実装していまして、
2
8
 
3
9
  Login画面には問題なく移行できるのですが、

2

修正

2020/08/31 23:43

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -299,3 +299,49 @@
299
299
  };
300
300
 
301
301
  ```
302
+
303
+
304
+
305
+ また、[next.config.js](https://nextjs.org/docs/api-reference/next.config.js/introduction)この記事を読んでconfigにも自身がなくなりました、
306
+
307
+
308
+
309
+ ```
310
+
311
+
312
+
313
+ require("dotenv").config();
314
+
315
+ const path = require("path");
316
+
317
+ const Dotenv = require("dotenv-webpack");
318
+
319
+ module.exports = {
320
+
321
+ webpack: config => {
322
+
323
+ config.plugins = config.plugins || [];
324
+
325
+ config.plugins = [
326
+
327
+ ...config.plugins,
328
+
329
+ // Read the .env file
330
+
331
+ new Dotenv({
332
+
333
+ path: path.join(__dirname, ".env"),
334
+
335
+ systemvars: true
336
+
337
+ })
338
+
339
+ ];
340
+
341
+ return config;
342
+
343
+ }
344
+
345
+ };
346
+
347
+ ```

1

修正

2020/08/31 22:02

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -126,103 +126,9 @@
126
126
 
127
127
  at discreteUpdates (react-dom.development.js?61bb:806)
128
128
 
129
- at dispatchDiscreteEvent (react-dom.development.js?61bb:4168)
129
+
130
-
131
- handleSubmit @ Login.js?5292:11
130
+
132
-
133
- callCallback @ react-dom.development.js?61bb:188
134
-
135
- invokeGuardedCallbackDev @ react-dom.development.js?61bb:237
136
-
137
- invokeGuardedCallback @ react-dom.development.js?61bb:292
138
-
139
- invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js?61bb:306
140
-
141
- executeDispatch @ react-dom.development.js?61bb:389
142
-
143
- executeDispatchesInOrder @ react-dom.development.js?61bb:414
144
-
145
- executeDispatchesAndRelease @ react-dom.development.js?61bb:3278
146
-
147
- executeDispatchesAndReleaseTopLevel @ react-dom.development.js?61bb:3287
148
-
149
- forEachAccumulated @ react-dom.development.js?61bb:3259
150
-
151
- runEventsInBatch @ react-dom.development.js?61bb:3304
152
-
153
- runExtractedPluginEventsInBatch @ react-dom.development.js?61bb:3514
154
-
155
- handleTopLevel @ react-dom.development.js?61bb:3558
156
-
157
- batchedEventUpdates$1 @ react-dom.development.js?61bb:21871
158
-
159
- batchedEventUpdates @ react-dom.development.js?61bb:795
160
-
161
- dispatchEventForLegacyPluginEventSystem @ react-dom.development.js?61bb:3568
162
-
163
- attemptToDispatchEvent @ react-dom.development.js?61bb:4267
164
-
165
- dispatchEvent @ react-dom.development.js?61bb:4189
166
-
167
- unstable_runWithPriority @ scheduler.development.js?3069:653
168
-
169
- runWithPriority$1 @ react-dom.development.js?61bb:11039
170
-
171
- discreteUpdates$1 @ react-dom.development.js?61bb:21887
172
-
173
- discreteUpdates @ react-dom.development.js?61bb:806
174
-
175
- dispatchDiscreteEvent @ react-dom.development.js?61bb:4168
176
-
177
- react-dom.development.js?61bb:327 Uncaught TypeError: createLogin is not a function
178
-
179
- at handleSubmit (Login.js?5292:11)
180
-
181
- at HTMLUnknownElement.callCallback (react-dom.development.js?61bb:188)
182
-
183
- at Object.invokeGuardedCallbackDev (react-dom.development.js?61bb:237)
184
-
185
- at invokeGuardedCallback (react-dom.development.js?61bb:292)
186
-
187
- at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js?61bb:306)
188
-
189
- at executeDispatch (react-dom.development.js?61bb:389)
190
-
191
- at executeDispatchesInOrder (react-dom.development.js?61bb:414)
192
-
193
- at executeDispatchesAndRelease (react-dom.development.js?61bb:3278)
194
-
195
- at executeDispatchesAndReleaseTopLevel (react-dom.development.js?61bb:3287)
196
-
197
- at forEachAccumulated (react-dom.development.js?61bb:3259)
198
-
199
- at runEventsInBatch (react-dom.development.js?61bb:3304)
200
-
201
- at runExtractedPluginEventsInBatch (react-dom.development.js?61bb:3514)
202
-
203
- at handleTopLevel (react-dom.development.js?61bb:3558)
204
-
205
- at batchedEventUpdates$1 (react-dom.development.js?61bb:21871)
206
-
207
- at batchedEventUpdates (react-dom.development.js?61bb:795)
208
-
209
- at dispatchEventForLegacyPluginEventSystem (react-dom.development.js?61bb:3568)
210
-
211
- at attemptToDispatchEvent (react-dom.development.js?61bb:4267)
212
-
213
- at dispatchEvent (react-dom.development.js?61bb:4189)
214
-
215
- at unstable_runWithPriority (scheduler.development.js?3069:653)
216
-
217
- at runWithPriority$1 (react-dom.development.js?61bb:11039)
218
-
219
- at discreteUpdates$1 (react-dom.development.js?61bb:21887)
220
-
221
- at discreteUpdates (react-dom.development.js?61bb:806)
222
-
223
- at dispatchDiscreteEvent (react-dom.development.js?61bb:4168)
224
-
225
- ```
131
+ **省略
226
132
 
227
133
  ```
228
134
 
@@ -326,6 +232,26 @@
326
232
 
327
233
  await app.auth().signInWithEmailAndPassword(email, password);
328
234
 
235
+ history.push("/");   Next.jsでhistoryは使わない?
236
+
237
+ } catch (error) {
238
+
239
+ alert(error);
240
+
241
+ }
242
+
243
+ };
244
+
245
+
246
+
247
+ // 新しいユーザーを作成しログインさせる関数
248
+
249
+ const signup = async (email, password, history) => {
250
+
251
+ try {
252
+
253
+ await app.auth().createUserWithEmailAndPassword(email, password);
254
+
329
255
  history.push("/");
330
256
 
331
257
  } catch (error) {
@@ -338,26 +264,6 @@
338
264
 
339
265
 
340
266
 
341
- // 新しいユーザーを作成しログインさせる関数
342
-
343
- const signup = async (email, password, history) => {
344
-
345
- try {
346
-
347
- await app.auth().createUserWithEmailAndPassword(email, password);
348
-
349
- history.push("/");
350
-
351
- } catch (error) {
352
-
353
- alert(error);
354
-
355
- }
356
-
357
- };
358
-
359
-
360
-
361
267
  useEffect(() => {
362
268
 
363
269
  app.auth().onAuthStateChanged(setCurrentUser);