React localStorage always keep values even restarting app - javascript

I have a fullstack (React & Spring Boot) app and use JWT token. When user login, I set the jwt token and user details in local storage. However, when I restart the React app, the localStorage values are not reset or clear. I also set token expiration to 1 minute for testing on Spring Boot app, but still the same problem.
So, how should I reset localStorage values on every initialization of React app?

Related

react app state management and auth flow with firebase redux

i have created an app in react and firebase for auth used react-redux-firebase to manage state of app . after login the system i can access user state and accordingly sign the user in. but my concern is the state doesn't expires or cleared if app not used for long time and remains until not logged out .i want to clear the state if not app not used after n days.
firebase token expires in 1 hour , so do i have to call firebase onauthstatechanged listener with every call for a token or keep a timer every 50-60 min to refresh the token?? i dont know which way to go with or is there any other better way ?? or any other better authentication flow for the app!
The Firebase SDK automatically refreshes the user's auth token every hour. You don't have to write any code for that. You only need on onAuthStateChanged listener for the entire app - it will always stay in sync with the user's sign-in state, and you also write no additional code for that.

Client side session management in react express app

I am building and react express app, I am not using server side rendering, where I am facing problem in session management in the client side
So problem. Is like this
I have express route /, /login, /logout, /dashboard
And I also have the same routing in react using react router dom
I used express-session to set the useremail as a session variable in /login route of express app
Then I redirect user to /dashboard . Before redirect user to /dashboard I put authentication code where I check session.useremail is set or not
But when user hit /dashboard to server . Server do authentication . But if user navigate to /dashboard in react client app it just show all the dashboard
You need to check if a user is authenticated or not in your client side. This means if session holds a user or not at that time. Checking authentication depends how you set up your client side logic. But first, you need an Express route to check if session has a user or not. Then you can write your client side code.
If you won't use Redux to handle your state, then you need to go to that Express route every time in a component where you want to check the authentication status. After getting your result you can show the component to a client or redirect them somewhere else depending on that result. I am not a pro, maybe there is another easy way to skip going to that route every time. Maybe we can check for a user in the entry point of the application and write authentication information in a key in localStorage. Then going to Express route, we can check that key in localStorage. I prefer using Redux, handling authentication state there.
If you will use Redux, you can write an action creator and reducer to check the authentication status and set your state (like auth) and check the state where you need.

How to get auth state in Reactjs using Expressjs and Passportjs?

I am working on a project that use Reactjs for front-end and Expressjs and Passportjs for server side.
The user information will be stored in cookie-session middleware in server side whenever he/she logged in. But every time the user opens the website, the react app will have to fetch user data by sending a http request to the server side and wait for it to response. This sometimes take some seconds or even longer when the connection is poor.
Is there any way to get user's auth state immediately? Thank in advanced!

Firebase auth - getting a token to persist user password login

My Ionic 3 mobile app i'm currently building allows login with firebase using both Email/Password and Facebook providers.
Everything's working great - but when the user logs in, in order to have a 'remember me' function that prevents them having to log in every time the app is closed (closing the app fully, or the system kills it), i need to be able to get some sort of token, store it, then use it later to take them straight past the authentication screen.
I've managed this already, but currently i'm storing their email and password, and i know this is a horrible thing to do. (I'm using Ionic Storage).
Is there a way to get a token that represents a user, and can be used to re-authenticate them?
I know about custom token logins, but they can only be created in NodeJS using the admin SDK - is there any solution i can run directly on the phone?
Thanks.

Sharing a session between Node.js app and Rails app

I'm running a Rails app on Heroku, and I'm trying to port over all inline Twitter requests (namely oauth authentication) to a Node.js app, because when Twitter is slow, since the Ruby server is blocking, the Twitter requests clog up my app. (My average request takes about 50ms, but my average Twitter Oauth request takes about 1500ms!)
The node.js app and the rails app can access the same database.
What I can't figure out, though, is how to initiate a session with node.js, and then use that session in the rails app. Right now I'm using the default Rails cookie session store.
I'd want it to work something like...
1) user arrives at landing page http://railsapp.com
2) user initiates log-in with http://nodeapp.com/login or http://nodeapplogin.railsapp.com
3) user does the twitter oauth dance with node.js
4) upon successful log in, user is redirected to http://railsapp.com/dashboard WITH an active rails session that can be used throughout the rails app
Any ideas/suggestions?
You're going to want cookie based session key storage, set a global cookie domain to your toplevel 'railsapp.com', share your session store in a way both Node.js and Rails can access it.
I don't know the internals of Node.js session management, but I imagine it should be able to load a db-backed rails session store.

Categories

Resources