Is it a good idea to set the current user as a session variable in the meteor / react structure?

I am using the meteor + method to develop a small application, mainly to understand how react works. I am not currently using any form of Flux or Redux.

Over the course of several instances in the application, I have to check if the user using the application is logged in. I am currently doing this with a dataContainer from react-meteor data.

import { createContainer } from 'meteor/react-meteor-data';
...
class NavigationBar extends Component {
    render () {
        return (
            <div>
                this.props.currentUser
            </div>
        )
    }
}
... 
export default createContainer(() => {
    let currentUser = Meteor.user()
    return {
        currentUser,
    };
}, NavigationBar);

      

While this is working fine, I'm starting to get a little annoyed to wrap every component I want to check the current user in. Since I have custom login and logout functionality, I was wondering if there was something wrong (insecure) about setting the current user in the session variable for loglog like this:

> Session.set('currentUser',Meteor.userId());

      

and then just set it to null on logout?

thanks alot

+3


source to share


3 answers


It is good to store information about the user in session variable

or pass them in child component

as props

. But make sure you delete the session variable correctly when the user logs out. In your case:



Session.set('currentUser',null);

      

+2


source


I would say, instead of storing user information in the session, you can store the bool value as true, so if currentUser is not null, you can set it to true, and then you can easily use that boolean variable to control the Display UI on frontend, now I don't know what your project requirements are, but this is a good way to deal with the login state of a user in my opinion or not. I would appreciate any corrections to my answers.



Thank!

+1


source


Saving the current user ID is pretty safe. But make sure it is updated correctly. Like you don't have a user id if the user logs out.

+1


source







All Articles