Похожие чаты

I am using useEffect in private route component to check

if current session of user exists in DB or not but after refresh redux state gets reset so how to implement private route using redux and passportjs session auth? Any idea why this is happening

18 ответов

31 просмотр

Use the concept of "route guard" to apply what david said

Adi- Автор вопроса
Amos
Use the concept of "route guard" to apply what dav...

yes I solved the problem of private routes on frontend using react router but as I am using httpOnly cookie session based auth I am making request to /auth endpoint which also checks if current logged in user session is valid or not. So everytime user navigates to private route I first make API call to check user session in DB. Just wanted to know is this solution good or do you recommend anything else ? cc: @blink_bat

Adi
yes I solved the problem of private routes on fron...

You can wrap your react-router switch in a HOC which when rendered (this will render only the first time you open a private route, or when you refresh on a private route), will make an API call to check if the user is auth'd or not. There might be better ways around

Adi- Автор вопроса
Amit Parameshwar
You can wrap your react-router switch in a HOC whi...

but if user refreshes browser or manually changes url in browser that time request will be fired for validating user

Adi- Автор вопроса
Amit Parameshwar
yes

see this code https://pastebin.com/LXSha8pc in private route I am making call to check if user has valid session now you are suggesting to have some kind of auth provider which wraps private routes or have state in redux is that what you are suggesting? I am making additional call to /auth route to check if current loggedin user has valid session or not can we eliminate this additional call to /auth route?

Adi
see this code https://pastebin.com/LXSha8pc in pri...

yea id suggest using redux to save the data you get from /auth route, now with some extra code, everytime the user switches to other private routes, /auth api call wont be called

Adi- Автор вопроса
Amit Parameshwar
yea id suggest using redux to save the data you ge...

If usser navigates by clicking button i.e history.push() that time redux state wont get reset but if user refreshes page or changes url manually then it will have to make call to /auth endpoint is that correct??

Adi- Автор вопроса
Amit Parameshwar
yes

So basically it is not possible to eliminate making calls to /auth endpoint inside private route is that correct?

Adi- Автор вопроса
Amit Parameshwar
yes

So do I need to maintain /auth endpoint as well as auth middleware in api route both ??

Adi
So basically it is not possible to eliminate makin...

You would need to make API call to check if the user is auth'd or not everytime a private route is rendered unless you can manage it in state, like redux.

Adi
So do I need to maintain /auth endpoint as well as...

Regardless of what you are doing in frontend, I'd recommend to keep /auth route and auth middleware for every private routes

Adi- Автор вопроса
Adi
Why both ? Just curious?

From my perspective, /auth route is supposed to return crucial user data which is used for subsequent API calls. And auth middleware just to make sure that the requester is auth'd and authorised for the resource

Adi- Автор вопроса
Amit Parameshwar
From my perspective, /auth route is supposed to re...

In my case auth endpoint is returning only isLoggedIn variable from server. So if user session is valid auth endpoint will return isLoggedIn true or else false

Adi- Автор вопроса
Adi
In my case auth endpoint is returning only isLogge...

This isLoggedIn variable is then stored in redux state variable which is used to navigate private routes in react router

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта