Skip to content

Commit

Permalink
Merge pull request #121 from nkc-ug/fix/login_undefined
Browse files Browse the repository at this point in the history
存在しないフィールドを参照することによって発生するundefinedを潰した。
  • Loading branch information
Mount-Book authored Sep 1, 2023
2 parents 122fa4b + c199daf commit 2394dc1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/components/auth/AddUserData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { setDoc, doc } from 'firebase/firestore';
import { db } from '../../lib/firebase';
import { USERCOLLECTIONNAME } from '../../lib/firestore';
import { GetUserData } from './GetUserData';

export const AddUserData = async (email: string) => {
const userData = await GetUserData(email);
const collectionRef = doc(db, USERCOLLECTIONNAME, email);
const docData = {
userName: '',
totalEatCount: 0,
battleWinCount: 0,
goatType: '',
goatClothes: 'yagi',
};
if (docData.userName !== 'NoLogin') {
await setDoc(collectionRef, docData);
}
userData ? await setDoc(collectionRef, userData, { merge: true }) : null;
};
3 changes: 3 additions & 0 deletions src/components/auth/AuthGoogleSigninPopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { provider } from '../../lib/AuthGoogleProviderCreate';
import { SetupUserData } from './SetupUserData';
import { UserProps } from '../../types/UserDataType';
import { GetUserData } from './GetUserData';
import { AddUserData } from './AddUserData';

export const Auth = async () => {
const auth = await getAuth();
Expand All @@ -24,6 +25,8 @@ export const Auth = async () => {
if (!(await GetUserData(email))) {
// ユーザーがFireStoreに登録されていない場合、初期データを設定する。
SetupUserData(addUser);
} else {
AddUserData(email);
}
return email;
};
3 changes: 3 additions & 0 deletions src/components/auth/LoginButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Button } from '@mui/material';
import { Auth } from './AuthGoogleSigninPopup';
import { IsLoginContext } from '../../provider/ContextProviders';
import { EmailContext } from '../../provider/ContextProviders';
import { SetupUserData } from './SetupUserData';
import { AddUserData } from './AddUserData';

export const LoginButton = () => {
const [isLogin, setIsLogin] = useContext(IsLoginContext);
Expand All @@ -19,6 +21,7 @@ export const LoginButton = () => {
setEmail(fetchEmail);
setLoginButtonText('ログアウト');
setIsLogin(true);
AddUserData(fetchEmail);
}
}
}, []);
Expand Down
4 changes: 2 additions & 2 deletions src/components/auth/UserDataDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ export const UserDataDisplay: FC = () => {
</div>
<div>
<h3>ことばをたべさせたかいすう</h3>
<h4>{String(userData?.totalEatCount)}かい</h4>
<h4>{userData?.totalEatCount ? String(userData?.totalEatCount) : '0'}かい</h4>
</div>
<div>
<h3>バトルでかったかいすう</h3>
<h4>{String(userData?.battleWinCount)}かい</h4>
<h4>{userData?.battleWinCount ? String(userData?.battleWinCount) : '0'}かい</h4>
</div>
</Box>
</Box>
Expand Down

0 comments on commit 2394dc1

Please sign in to comment.