From f14e8bd97083442b32c15db7062479ea8b3f3845 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Thu, 3 Oct 2019 10:58:45 -0400 Subject: [PATCH] Redux saves user data on login --- functions/handlers/users.js | 17 ++++++++++++++ functions/index.js | 3 +++ twistter-frontend/package.json | 2 +- .../src/redux/actions/userActions.js | 22 +++++++++---------- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 1c217e5..21f9b96 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -214,3 +214,20 @@ exports.getUserDetails = (req, res) => { 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 }); + }); +}; diff --git a/functions/index.js b/functions/index.js index eb99cbf..5c728bc 100644 --- a/functions/index.js +++ b/functions/index.js @@ -10,6 +10,7 @@ app.use(cors()); * handlers/users.js * *------------------------------------------------------------------*/ const { + getAuthenticatedUser, getUserDetails, getProfileInfo, login, @@ -34,6 +35,8 @@ app.get("/getProfileInfo", fbAuth, getProfileInfo); // Updates the currently logged in user's profile information app.post("/updateProfileInfo", fbAuth, updateProfileInfo); +app.get("/user", fbAuth, getAuthenticatedUser); + /*------------------------------------------------------------------* * handlers/post.js * *------------------------------------------------------------------*/ diff --git a/twistter-frontend/package.json b/twistter-frontend/package.json index eebb8fb..a83c175 100644 --- a/twistter-frontend/package.json +++ b/twistter-frontend/package.json @@ -37,5 +37,5 @@ "last 1 safari version" ] }, - "proxy": " http://localhost:5001/twistter-e4649/us-central1/api" + "proxy": "https://us-central1-twistter-e4649.cloudfunctions.net/api" } diff --git a/twistter-frontend/src/redux/actions/userActions.js b/twistter-frontend/src/redux/actions/userActions.js index 2d87738..2177a75 100644 --- a/twistter-frontend/src/redux/actions/userActions.js +++ b/twistter-frontend/src/redux/actions/userActions.js @@ -10,7 +10,7 @@ export const loginUser = (loginData, history) => (dispatch) => { const FBIdToken = `Bearer ${res.data.token}`; localStorage.setItem('FBIdToken', FBIdToken); axios.defaults.headers.common['Authorization'] = FBIdToken; - dispatch(getProfileInfo()); + dispatch(getUserData()); dispatch({ type: CLEAR_ERRORS }) // Redirects to home page history.push('/home'); @@ -23,13 +23,13 @@ export const loginUser = (loginData, history) => (dispatch) => { }); } -// export const getProfileInfo = () => (dispatch) => { -// axios.get('/getProfileInfo') -// .then((res) => { -// dispatch({ -// type: SET_USER, -// payload: res.data, -// }) -// }) -// .catch((err) => console.error(err)); -// } \ No newline at end of file +export const getUserData = () => (dispatch) => { + axios.get('/user') + .then((res) => { + dispatch({ + type: SET_USER, + payload: res.data, + }) + }) + .catch((err) => console.error(err)); +} \ No newline at end of file