Redux saves user data on login

This commit is contained in:
Clayton Wilson 2019-10-03 10:58:45 -04:00
parent 8de496826a
commit f14e8bd970
4 changed files with 32 additions and 12 deletions

View File

@ -214,3 +214,20 @@ exports.getUserDetails = (req, res) => {
return res.status(500).json({ error: err.code }); return res.status(500).json({ error: err.code });
}); });
}; };
exports.getAuthenticatedUser = (req, res) => {
let userData = {};
db.doc(`/users/${req.user.handle}`)
.get()
.then((doc) => {
if (doc.exists) {
userData.credentials = doc.data();
return res.status(200).json({userData});
} else {
return res.status(400).json({error: "User not found."})
}})
.catch((err) => {
console.error(err);
return res.status(500).json({ error: err.code });
});
};

View File

@ -10,6 +10,7 @@ app.use(cors());
* handlers/users.js * * handlers/users.js *
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
const { const {
getAuthenticatedUser,
getUserDetails, getUserDetails,
getProfileInfo, getProfileInfo,
login, login,
@ -34,6 +35,8 @@ app.get("/getProfileInfo", fbAuth, getProfileInfo);
// Updates the currently logged in user's profile information // Updates the currently logged in user's profile information
app.post("/updateProfileInfo", fbAuth, updateProfileInfo); app.post("/updateProfileInfo", fbAuth, updateProfileInfo);
app.get("/user", fbAuth, getAuthenticatedUser);
/*------------------------------------------------------------------* /*------------------------------------------------------------------*
* handlers/post.js * * handlers/post.js *
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/

View File

@ -37,5 +37,5 @@
"last 1 safari version" "last 1 safari version"
] ]
}, },
"proxy": " http://localhost:5001/twistter-e4649/us-central1/api" "proxy": "https://us-central1-twistter-e4649.cloudfunctions.net/api"
} }

View File

@ -10,7 +10,7 @@ export const loginUser = (loginData, history) => (dispatch) => {
const FBIdToken = `Bearer ${res.data.token}`; const FBIdToken = `Bearer ${res.data.token}`;
localStorage.setItem('FBIdToken', FBIdToken); localStorage.setItem('FBIdToken', FBIdToken);
axios.defaults.headers.common['Authorization'] = FBIdToken; axios.defaults.headers.common['Authorization'] = FBIdToken;
dispatch(getProfileInfo()); dispatch(getUserData());
dispatch({ type: CLEAR_ERRORS }) dispatch({ type: CLEAR_ERRORS })
// Redirects to home page // Redirects to home page
history.push('/home'); history.push('/home');
@ -23,13 +23,13 @@ export const loginUser = (loginData, history) => (dispatch) => {
}); });
} }
// export const getProfileInfo = () => (dispatch) => { export const getUserData = () => (dispatch) => {
// axios.get('/getProfileInfo') axios.get('/user')
// .then((res) => { .then((res) => {
// dispatch({ dispatch({
// type: SET_USER, type: SET_USER,
// payload: res.data, payload: res.data,
// }) })
// }) })
// .catch((err) => console.error(err)); .catch((err) => console.error(err));
// } }