Hello Guys,
I am unable to get user session when user is logged in. For example if i want to get topics for specific user. so i need value of req.user.id on topic request. But is undefined. It looks like Bug in the code.
actions/topic.js
export function fetchTopics() {
return {
type: types.GET_TOPICS,
promise: makeTopicRequest('get')
};
}
app/Containers/Votes.jsx
class Vote extends Component {
....
static need = [
fetchTopics
]
...
}
server/config/routes.js
app.get('/topic', topicController.all);
server/controllers/topics.js
export function all(req, res) {
console.log(req.user); // undefined
Topic.find({}).exec((err, topics) => {
if (err) {
console.log('Error in first query');
return res.status(500).send('Something went wrong getting the data');
}
return res.json(topics);
});
}
Any Solution?
Hmm thats strange. Can you console.log req.user right after login?
Also please check whether your url(both protocol and host) and axios request url are the same.
console.log req.user for login and signup works. It works because they are post request.
It looks like is not working with fetch topics, i mean for Get request with promise.
I just clone the master branch and req.user is undefined for topics controller.
ah, it is because there's no user info passed to client on initial render. I think you should put
app/server.jsx
const store = configureStore({
user: {
authenticated,
isWaiting: false,
**userObj: req.user,**
message: '',
isLogin: true
}
}, history);
bold part to use req.user in the client. This is what I do to use req.user object in client.
you can access it via this.props.user.userObj if you use react-redux connect function and map state to props.
@choonkending do you think we need to make PR for this, or leave it for users?
thank you for your reply. So i get userObj filled with req.user but do you have any idea how to pass this in topic controller?
export function all(req, res) {
console.log(req.user); // want to get value here
Topic.find({}).exec((err, topics) => {
if (err) {
console.log('Error in first query');
return res.status(500).send('Something went wrong getting the data');
}
So if you see bellow issues and pull request. we have issue with GET Request.
https://github.com/reactGo/reactGo/issues/281
https://github.com/reactGo/reactGo/issues/171
https://github.com/reactGo/reactGo/pull/225
https://github.com/reactGo/reactGo/pull/184
Sounds like everybody has the same issue. But is there any solution yet as looks like discussion is since march or i am stupid to find one in this repo some where?
@ZeroCho I think it's better to leave it for users to implement if we want to initialize the redux store with user request objects - until we find a suitable solution. Once we cull down all the unneeded features, all these can perhaps be added as separate repositories in reactGo.
Unable to get user session data when user is logged in.
@sidharthchugh Thanks for digging through all that! Yes, it seems that discussion did die down a bit, but would you like to test on https://github.com/reactGo/reactGo/pull/225 to see if it's a suitable solution for you since you have the context? (Or alternatively we can work to come up with a solution alternative to that if you have a better solution).
Unable to pass parameter with get request.
I'll comment on that thread - tons of questions around how to do it. I think I might have an idea on how to help with that.
@choonkending Thankyou for directing me to https://github.com/reactGo/reactGo/pull/225. It really works. As the code is same with your changes in comments. So should i create pull request or comment it in this thread for your review?
I have one more doubt.
For example on successful login on landing page user should direct to profile page. If I use only
dispatch(push('/profile'));
It wont have GET Request and in turn no need function and no fetch function is called.
But when i refresh profile page then it have GET Request.
So i put dispatch(fetchProfiles()); before doing push to have GET Request directly after successful login.
Can you check bellow code and tell it is right approach?
function makeProfileRequest(method, data, api = '/account/profile') {
return request method (api, data);
}
export function fetchProfiles() {
return {
type: types.GET_PROFILES,
promise: makeProfileRequest('get')
};
}
export function manualLogin(data) {
return dispatch => {
dispatch(beginLogin());
return makeUserRequest('post', data, '/login')
.then(response => {
if (response.status === 200) {
dispatch(loginSuccess(response.data.message));
dispatch(fetchProfiles()); // is this code right?
dispatch(push('/profile')); // is this code right?
} else {
dispatch(loginError('Oops! Something went wrong!'));
}
})
.catch(err => {
dispatch(loginError(getMessage(err)));
});
};
}
don't really understand #225 too well. However, certainly think the current GET solution is not sufficient. It feels so black-box, no handlers for Success/Failure (like POST has)... would be really nice to use fetch() onEnter/onLeave for routes, but the promise fires the db-controller twice... No idea why, but having access to GET on route-handlers would be nice. Also, would be able to decrypt fetched-response if there was a Success action/callback...
@jrodl3r you are right. Because currently i have to do hack fix to have GET while pushing to other route. We need to refactor this GET to have all the functions working properly as many people are posting in one or other thread.
@choonkending something is planned?
Sorry it took a while to get to this. Thanks for the good responses!
@sidharthchugh don't have a plan yet. :(
I had thought that dispatching the route change programmatically like you did would fetchProfiles automatically if you specify that in the need function of your profile component.
Then the fetchProfiles wouldn't need to be dispatched from your manualLogin action creator.
I agree it's time to have a look at whether we can improve our prerendermiddleware to be better. This issue has been kicking around for a bit.
@choonkending @ZeroCho
I want to finish my project so just want hack fix to get req.user on GET REQUEST.
@ZeroCho As suggested by you
const store = configureStore({
user: {
authenticated,
isWaiting: false,
userObj: req.user,
message: '',
isLogin: true
}
}, history);
but how i get userObject in mongodb controller when get request is performed.
export function all(req, res) {
console.log(req.user); // undefined
Topic.find({}).exec((err, topics) => {
if (err) {
console.log('Error in first query');
return res.status(500).send('Something went wrong getting the data');
}
Please help guys any bad hack fix also works. i am urgently in a need to finish my project and only this thing left. Please Please !!!!!!
@sidharthchugh Sorry for the late reply! What's the reason you need the req.user in the client?
I'm a bit confused. Do you need https://github.com/reactGo/reactGo/pull/225 to be merged for your purpose or is your problem a different use case?
We're cleaning the project of stale issues therefore i'm closing this. If you still have any issue, please comment here or open a new issue.
The code has changed quite a bit since this issue was opened.
Moreover, please check https://github.com/reactGo/reactGo/issues/922 https://github.com/reactGo/reactGo/issues/888 https://github.com/reactGo/reactGo/issues/909 as i've explain a few related things that i think might help
Most helpful comment
@jrodl3r you are right. Because currently i have to do hack fix to have GET while pushing to other route. We need to refactor this GET to have all the functions working properly as many people are posting in one or other thread.
@choonkending something is planned?