T
TanStack2y ago
spiritual-aqua

how can i check if i iauthenticated

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 use router.state.resolvedLocation as it can // potentially lag behind the actual current location) redirect: location.href, }, }) } },
23 Replies
genetic-orange
genetic-orange2y ago
thats a hook, you need a non hook version of this check
spiritual-aqua
spiritual-aquaOP2y ago
Thanks, and now how can i stay on the same page if i am not auth?
No description
genetic-orange
genetic-orange2y ago
are you sure these are not hooks? also. to stay on the same page, simply do not redirect
spiritual-aqua
spiritual-aquaOP2y ago
problem with hooks are solved if i am not redirect than the /create page opens
genetic-orange
genetic-orange2y ago
well you said you want to stay on the same page?
spiritual-aqua
spiritual-aquaOP2y ago
i want to prevert that
genetic-orange
genetic-orange2y ago
well you do that by redirecting redirect to login or something
spiritual-aqua
spiritual-aquaOP2y ago
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
genetic-orange
genetic-orange2y ago
No description
spiritual-aqua
spiritual-aquaOP2y ago
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
No description
spiritual-aqua
spiritual-aquaOP2y ago
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 @Zion
genetic-orange
genetic-orange2y ago
ohhh you want to stay on the page BEFORE you click on the link
spiritual-aqua
spiritual-aquaOP2y ago
yeas, correct
genetic-orange
genetic-orange2y ago
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
spiritual-aqua
spiritual-aquaOP2y ago
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
genetic-orange
genetic-orange2y ago
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.
spiritual-aqua
spiritual-aquaOP2y ago
why i cant just go back in beforeLoad, i think it should be some way
genetic-orange
genetic-orange2y ago
you can go back, what will that achive? you will go back to the same route you came from
spiritual-aqua
spiritual-aquaOP2y ago
i will be in the previous page, where button was clicked
genetic-orange
genetic-orange2y ago
yeah, what difference does it make? you can also simply not route the user
spiritual-aqua
spiritual-aquaOP2y ago
but gonna handleit in one place, in routing
genetic-orange
genetic-orange2y ago
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)
spiritual-aqua
spiritual-aquaOP2y ago
thank you! ❤️

Did you find this page helpful?