Похожие чаты

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

25 просмотров

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

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

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

Ready for some fun AND a chance to win TKO Tokens? Join us for exciting minigames in our Telegram group! 🕒 Don’t miss out—games start on today 25 October 2024, at 8 PM! Ge...
Milkyway | Tokocrypto
255
Добрый вечер. Есть вопрос, а может и предложение. Был у меня диалог в другой группе о делфи и я задался вопросом: "А нельзя ли в делфи цвет //коментария и {комментария} сде...
Kraszx
24
How are we going bro about the Raids ??
🅿️abby_FX
13
Мдя, прикол, боевая сборка запускается (именно под отладчиком) после F9 примерно полторы минуты (97 секунд если быть точным). Начал копать - проблема детектится сразу - зависа...
Александр (Rouse_) Багель
38
Всем привет! Подскажи, пожалуйста, как передать в TComboBox сразу значение и id записи. На Delphi я делал так: ComboBox1.Items.AddObject('Какое-то значение', Pointer(id запис...
Евгений
13
Keep saying it, it’s Trump or WW3. Iran will be emboldened if Harris wins by cheating. Israel will have to take the initiative against Iran. How has BTC faired everytime the...
Adz | Ferrum Network Product & Service Consultant @ Ferrum Network
1
я так понимаю, я так подозреваю, что создание такого плагина для человека, кто умеет писать плагины для делфи потребует минут 5-10 времени. но это мое подозрение. хотелось бы ...
Kraszx
7
Товарищи, кто работа с iphelper? Или может я в самой логике ошибки фигачу, не пойму.... var ifTable : PMIB_IFTABLE; size, corSize: DWORD; Buffer ...
Warfarellen
4
Здравствуйте, вопрос по структурам данных. Были у вас случаи, когда пришлось писать деревья или двунаправленные списки?
/ /
50
Коллеги, добрый вечер. Создаю коллекцию от TFPGMap, ключ - перечисление, значение - целое. Нужно отсортировать коллекцию по значению. Как это можно сделать?
Kirill Filippenok
11
Карта сайта