Search code examples
node.jspostgresqlreactjsreact-routerstormpath

How should I persist authenticated users in my database when using an authentication API?


I realize this is a bit of noob question but im not exactly sure what is the best solution. I have a REACT/Node.js application that is using a Authentication API (Stormpath in this case). The app is working. I have logins for local users as well as social logins for facebook and Google. But my app will have a local database (postgres most likely) where I will need to persist the authenticated user so I can associate things in my application with this user while they are logged in. How should I be doing this, or I guess where should I be doing this? Should my landing page (post login) have some dynamic event (pageload or whatever) that calls some middleware to insert or verify the existing user? Is there a hook I may be missing from my API provider where I can just grab the user. I have looked but have been unable to trigger any middleware rest endpoints I've setup in my app from what I think are Auth API endpoints that would provide the user information Im looking for. If I could just get to my middleware I'd have no problem writing the function to insert/verify the user in my local database. I've actually already written and tested that so I know it works. I just want a little advice or someone to nudge me in the right direction. I'm working on my own without any help right now so any advice would be greatly appreciated.

FYI I am using the Stormpath-sdk-react.


Solution

  • There are various lifecycle functions in react such as componentWillMount() and componentDidMount(). In componentWillMount() you can hit Api or use local db and check authenticated user and in componentDidMount() you can cancel the request.

    componentWillMount() is invoked before render() and componentDidMount() after render()