getState() からの localId ではなく、userId として idToken を取得しています。また、userId に localId を取得することがあります。アプリでこの問題が発生する原因がわかりません。
localId である redux 状態から正しい userId を取得すると、管理領域で製品を確認できます。
アクション/Product.js:
import Product from "../../models/Product";
export const DELETE_PRODUCT = "DELETE_PRODUCT";
export const CREATE_PRODUCT = "CREATE_PRODUCT";
export const UPDATE_PRODUCT = "UPDATE_PRODUCT";
export const SET_PRODUCT = "SET_PRODUCT";
export const fetchProducts = () => {
return async (dispatch, getState) => {
console.log(getState());
const userId = getState().Auth.userId;
try {
const response = await fetch(
"https://shopping-app-62e38-default-rtdb.firebaseio.com/products.json"
);
if (!response.ok) {
throw new Error("Something went wrong!");
}
const resData = await response.json();
const loadedProducts = [];
for (const key in resData) {
loadedProducts.push(
new Product(
key,
resData[key].ownerId,
resData[key].title,
resData[key].imageUrl,
resData[key].description,
resData[key].price
)
);
}
dispatch({
type: SET_PRODUCT,
products: loadedProducts,
userProducts: loadedProducts.filter((prod) => prod.ownerId === userId),
});
} catch (error) {
throw error;
}
};
};
export const deleteProduct = (productId) => {
return async (dispatch, getState) => {
const token = getState().Auth.token;
const response = await fetch(
`https://shopping-app-62e38-default-rtdb.firebaseio.com/products/${productId}.json?auth=${token}`,
{
method: "DELETE",
}
);
if (!response.ok) {
throw new Error("Something went wrong!");
}
dispatch({ type: DELETE_PRODUCT, pid: productId });
};
};
export const createProduct = (title, description, imageUrl, price) => {
return async (dispatch, getState) => {
const token = getState().Auth.token;
const userId = getState().Auth.userId;
console.log(getState());
const response = await fetch(
`https://shopping-app-62e38-default-rtdb.firebaseio.com/products.json?auth=${token}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title,
description,
imageUrl,
price,
ownerId: userId,
}),
}
);
const resData = await response.json();
console.log(userId);
dispatch({
type: CREATE_PRODUCT,
productData: {
id: resData.name,
title,
description,
imageUrl,
price,
ownerId: userId,
},
});
};
};
export const updateProduct = (id, title, description, imageUrl) => {
return async (dispatch, getState) => {
const token = getState().Auth.token;
const response = await fetch(
`https://shopping-app-62e38-default-rtdb.firebaseio.com/products/${id}.json?auth=${token}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title,
description,
imageUrl,
}),
}
);
if (!response.ok) {
throw new Error("Something went wrong!");
}
dispatch({
type: UPDATE_PRODUCT,
pid: id,
productData: { title, description, imageUrl },
});
};
};
アクション/Auth.js:
import { AsyncStorage } from "react-native";
let timer;
export const AUTHENTICATE = "AUTHENTICATE";
export const LOGOUT = "LOGOUT";
export const Authenticate = (userId, token, expiryTime) => {
return (dispatch) => {
dispatch(setLogoutTimer(expiryTime));
dispatch({ type: AUTHENTICATE, userId: userId, token: token });
};
};
export const signup = (email, password) => {
return async (dispatch) => {
const response = await fetch(
"https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyAnaJyjjGppk9-FiWocva_cP0vaMrKp4_4",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: email,
password: password,
returnSecureToken: true,
}),
}
);
if (!response.ok) {
const responseData = await response.json();
const errorMessage = responseData.error.message;
if (errorMessage === "EMAIL_EXISTS") {
throw new Error("Email already exists");
}
}
const resData = await response.json();
console.log(resData);
dispatch(
Authenticate(
resData.idToken,
resData.localId,
parseInt(resData.expiresIn) * 1000
)
);
const expirationDate = new Date(
new Date().getTime() + parseInt(resData.expiresIn) * 1000
);
saveDataToStorage(resData.idToken, resData.localId, expirationDate);
};
};
export const login = (email, password) => {
return async (dispatch) => {
const response = await fetch(
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyAnaJyjjGppk9-FiWocva_cP0vaMrKp4_4",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: email,
password: password,
returnSecureToken: true,
}),
}
);
if (!response.ok) {
const responseData = await response.json();
const errorMessage = responseData.error.message;
if (
errorMessage === "EMAIL_NOT_FOUND" ||
errorMessage === "INVALID_PASSWORD"
) {
throw new Error("Email or Password is wrong!!");
}
}
const resData = await response.json();
console.log(resData);
dispatch(
Authenticate(
resData.idToken,
resData.localId,
parseInt(resData.expiresIn) * 1000
)
);
const expirtationDate = new Date(
new Date().getTime() + parseInt(resData.expiresIn) * 1000
);
saveDataToStorage(resData.idToken, resData.localId, expirtationDate);
};
};
export const logout = () => {
clearLogoutTimer();
AsyncStorage.removeItem("userData");
return { type: LOGOUT };
};
const clearLogoutTimer = () => {
if (timer) {
clearTimeout(timer);
}
};
const setLogoutTimer = (expirationTime) => {
return (dispatch) => {
timer = setTimeout(() => {
dispatch(logout());
}, expirationTime);
};
};
const saveDataToStorage = (token, userId, expirtationDate) => {
AsyncStorage.setItem(
"userData",
JSON.stringify({
token: token,
userId: userId,
expiryDate: expirtationDate.toISOString(),
})
);
};
この問題を解決するには?localId を userId として取得する他の方法、またはこれと同じことを行う他の方法はありますか?