#how can i check if i iauthenticated

34 messages · Page 1 of 1 (latest)

opal temple
#

import useIsAuthenticated from 'react-auth-kit/hooks/useIsAuthenticated'export const createCollectionsRoute = new Route({ getParentRoute: () => collectionsLayoutRoute, path: '/create', component: CreateCollection, beforeLoad: async ({ location }) => { const isAuthenticated = useIsAuthenticated() if (!isAuthenticated()) { throw redirect({ to: '/collections', search: { // Use the current location to power a redirect after login // (Do not userouter.state.resolvedLocation as it can // potentially lag behind the actual current location) redirect: location.href, }, }) } },

steep crow
#

thats a hook, you need a non hook version of this check

opal temple
steep crow
#

also. to stay on the same page, simply do not redirect

opal temple
#

problem with hooks are solved

opal temple
steep crow
#

well you said you want to stay on the same page?

opal temple
#

i want to prevert that

steep crow
#

well you do that by redirecting

#

redirect to login or something

opal temple
#

i dont have login page, if user tries to access the page that he needs to be loged in, the modal window opens and user can authorize, and i want to prevert the opening of the page that he needs to be loged in

steep crow
opal temple
#

it dosent work for me. if user is not auth i should stay on the same page but right now it routes me to the /create page anyway

#

i need somehow stay on the previous page, where button was clicked, or redirect to it

#

prevent the opening of the new page and stay on page where button was clickde

#

@steep crow

steep crow
#

ohhh

#

you want to stay on the page BEFORE you click on the link

opal temple
#

yeas, correct

steep crow
#

why not just NOT have a link if you are not authed

#

or make it look like a link, but it will open a model instead

opal temple
#

but i have this button everyware, if i gonna do this in thousand of components, its gonna be bad...

#

better to do this in routing

steep crow
#

make a component that handles it
make it get a string (which will be the path), and there you can do the check if the user is authed, render a LINK component or a model. sounds like simple if else to me
I dont think its possible to do it in the routing, since you already moved to /create.

opal temple
#

why i cant just go back in beforeLoad, i think it should be some way

steep crow
#

you can go back, what will that achive?

#

you will go back to the same route you came from

opal temple
#

i will be in the previous page, where button was clicked

steep crow
#

yeah, what difference does it make? you can also simply not route the user

opal temple
#

but gonna handleit in one place, in routing

steep crow
#

I dont understand

#

you have a page, lets call it "nonauthed"
which has a button (link) to another page "create"

you can check in "nonauthed" if the user is authed with a hook.
then, you simply make the button be a link ONLY if he is authed, if not, you will render a "login model" (when clicked)

opal temple
#

thank you!