Skip to content

Commit

Permalink
uidをuseIdに統一
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakawaTakuya committed Dec 12, 2024
1 parent 63d1254 commit ce5e997
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 26 deletions.
14 changes: 8 additions & 6 deletions functions/src/routers/userRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,31 @@ router.get("/name/:userName", async (req: Request, res: Response) => {

// POST: 新しいユーザーを登録
router.post("/", async (req: Request, res: Response) => {
let uid: string;
let userId: string;
let name: User["name"];
let streak: User["streak"];
let fcmToken: User["fcmToken"];

try {
({ name, uid, streak = 0, fcmToken = "" } = req.body);
({ name, userId, streak = 0, fcmToken = "" } = req.body);
} catch (error) {
return res.status(400).json({ message: "Invalid request body" });
}

if (!name || !uid) {
return res.status(400).json({ message: "name and uid are required" });
if (!name || !userId) {
return res.status(400).json({ message: "name and userId are required" });
}

try {
await db.collection("user").doc(uid).set({
await db.collection("user").doc(userId).set({
name: name,
streak: streak,
fcmToken: fcmToken,
});

return res.status(201).json({ message: "User created successfully", uid });
return res
.status(201)
.json({ message: "User created successfully", userId });
} catch (error) {
return res.status(500).json({ message: "Error creating user" });
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/GoalModal/GoalModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function GoalModal() {
event.preventDefault();

const postData: Goal = {
userId: user?.uid as string,
userId: user?.userId as string,
text: text,
deadline: new Date(dueDate),
};
Expand Down
2 changes: 1 addition & 1 deletion src/Components/NameUpdate/NameUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function NameUpdate({
const handleNameUpdate = async (event: React.FormEvent) => {
event.preventDefault();

const response = await fetch(`${functionsEndpoint}/user/${user?.uid}`, {
const response = await fetch(`${functionsEndpoint}/user/${user?.userId}`, {
method: "PUT",
headers: {
"X-Firebase-AppCheck": appCheckToken,
Expand Down
2 changes: 1 addition & 1 deletion src/Components/PostModal/PostModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function PostModal({ goalId }: { goalId: string }) {
(errorMsg) => setError(errorMsg),
async (url) => {
const postData: Post = {
userId: user?.uid as string,
userId: user?.userId as string,
storedId: url,
text: text,
goalId: goalId,
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Progress/Progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const pendingStep = (result: GoalWithId, userName: string, user: UserData) => {
resultType="pending"
/>
{/* 自分の作成した目標の場合のみ投稿可能にする */}
{result.userId === user?.uid && <PostModal goalId={result.goalId} />}
{result.userId === user?.userId && <PostModal goalId={result.goalId} />}
</Step>
</StepperBlock>
);
Expand Down
4 changes: 2 additions & 2 deletions src/app/mycontent/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export default function MyContent() {
</div>

{value == "pending" ? (
<DashBoard userId={user?.uid} success={false} failed={false} />
<DashBoard userId={user?.userId} success={false} failed={false} />
) : (
<DashBoard userId={user?.uid} pending={false} />
<DashBoard userId={user?.userId} pending={false} />
)}
</>
);
Expand Down
2 changes: 1 addition & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface UserData {
uid: string;
userId: string;
name: string;
streak: number;
loginType: LoginType;
Expand Down
8 changes: 4 additions & 4 deletions src/utils/API/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { appCheckToken, functionsEndpoint } from "@/app/firebase";

/**
* Cloud FunctionsのAPIを呼び出して、ユーザー情報をFirestoreに登録する
* Authenticationのuidと/userのdocument IDは同じにする
* AuthenticationのuserIdと/userのdocument IDは同じにする
*
* @param {string} name
* @param {string} uid
* @param {string} userId
*/
export const createUser = async (name: string, uid: string) => {
export const createUser = async (name: string, userId: string) => {
const response = await fetch(`${functionsEndpoint}/user/`, {
method: "POST",
headers: {
"X-Firebase-AppCheck": appCheckToken,
"Content-Type": "application/json",
},
body: JSON.stringify({ uid, name }),
body: JSON.stringify({ userId, name }),
});

if (!response.ok) {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/API/fetchUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { UserData } from "@/types/types";
/**
* Cloud FunctionsのAPIを呼び出して、ユーザー情報をFirestoreから取得する
*
* @param {string} uid
* @param {string} userId
* @return {*} {Promise<UserData>}
*/
export const fetchUserById = async (uid: string): Promise<UserData> => {
const response = await fetch(`${functionsEndpoint}/user/id/${uid}`, {
export const fetchUserById = async (userId: string): Promise<UserData> => {
const response = await fetch(`${functionsEndpoint}/user/id/${userId}`, {
method: "GET",
headers: {
"X-Firebase-AppCheck": appCheckToken,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Auth/signUpWithGoogleAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const signUpWithGoogleAccount = async () => {
// uidとdocument IDを一致させる
await createUser(result.user.displayName ?? "no name", result.user.uid);
updateUser({
uid: result.user.uid,
userId: result.user.uid,
name: result.user.displayName ?? "no name",
streak: 0,
loginType: "Google",
Expand Down
6 changes: 3 additions & 3 deletions src/utils/Auth/signUpWithMail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "firebase/auth";

/**
* Firebase Authenticationでユーザーを作成し、生成されたuidをドキュメントIDとしてFirestoreにユーザー情報を登録する
* Firebase Authenticationでユーザーを作成し、生成されたuserIdをドキュメントIDとしてFirestoreにユーザー情報を登録する
*
* @param {string} email
* @param {string} password
Expand All @@ -33,10 +33,10 @@ export const signUpWithMail = (
console.error("プロファイル更新に失敗しました:", profileUpdateError);
}

// uidとdocument IDを一致させる
// userIdとdocument IDを一致させる
await createUser(name, user.uid);
updateUser({
uid: user.uid,
userId: user.uid,
name: name,
streak: 0,
loginType: "Mail",
Expand Down
4 changes: 2 additions & 2 deletions src/utils/UserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const UserProvider = ({ children }: Props) => {
// ゲスト以外のログインの場合はユーザーデータを取得しuseContextで保持
if (loginType === "Guest") {
const guestData: UserData = {
uid: firebaseUser.uid,
userId: firebaseUser.uid,
name: "Guest",
streak: 0,
loginType: "Guest",
Expand All @@ -89,7 +89,7 @@ export const UserProvider = ({ children }: Props) => {
const userData = await fetchUserById(firebaseUser.uid);
// ユーザーデータを作成する前にfetchしようとして"User not found"になるので、postした場所でsetさせている
// "User not found"ではない(= 初回ログイン直後ではない)場合のみsetする
if (userData.uid) {
if (userData.userId) {
setUser({
...userData,
loginType,
Expand Down

0 comments on commit ce5e997

Please sign in to comment.