Похожие чаты

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 ответов

26 просмотров

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

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

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

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
any reference of this implementation?
BitBuddha
29
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Недавно Google Project Zero нашёл багу в SQLite с помощью LLM, о чём достаточно было шумно в определённых интернетах, которые сопровождались рассказами, что скоро всех "ибешни...
Alex Sherbakov
5
So much speculation in the last week. So much volatility in price. This is because Hedera has a GC that isn't using the network it's governing. Why aren't people asking why a...
Summit Seeker R
9
Карта сайта