hundreds-psychiatrist-38730
08/15/2024, 8:17 PMreact-spa
example within @ory/Elements
in Dashboard.tsx
there is a .toSession()
within useEffect()
for checking if the user is logged in.
which makes total sense, you want to lock that page 😛
Here is where i'm getting confused, Settings.tsx
which i also see should be behind a session check, do not have any checks, it only checks if there is a flowId
set
isn't that a flaw? 🤔bland-eye-99092
08/16/2024, 6:27 AMhundreds-psychiatrist-38730
08/16/2024, 6:55 AMwitty-fountain-21673
08/22/2024, 8:21 AM_secured.tsx
route that checks context.session.active
. and just returns Outlet
. All secured pages go under src/routes/_secured/
🙂hundreds-psychiatrist-38730
08/22/2024, 4:40 PMimport {createFileRoute, Outlet, redirect} from '@tanstack/react-router'
export const Route = createFileRoute('/_auth')({
beforeLoad: ({ context }) => {
if (!context?.auth?.session?.active) {
throw redirect({
to: '/login'
})
}
},
component: AuthLayout,
})
function AuthLayout() {
return (
<Outlet />
)
}
is my _auth.tsx
in this case.
my "struggle" is the part around setSession()
etc. 🙂witty-fountain-21673
08/22/2024, 4:50 PMhundreds-psychiatrist-38730
08/22/2024, 4:52 PMwitty-fountain-21673
08/22/2024, 4:53 PMwitty-fountain-21673
08/22/2024, 4:55 PMhundreds-psychiatrist-38730
08/22/2024, 5:09 PMtoSession()
Right now, i have a tsx file auth.tsx
where i build an interface and export a function useAuth()
(like tanstack router suggest to do in their routes system).
that context is basically loaded on every page.
and here is my problem, in that context i run a useEffect()
that contains the big part that set the user session, create logout flow.
So if i'm not loggedin, it does 2 requests to /whoami
i get a 401 unauth back which makes sense.
But there might be some pages i don't need to check authentication on, e.g. /about
but the context will still do it.
I tried wrap .toSession()
promise around a if statement if (session?.active) {}
but then login wouldn't really work.
So i was kinda thinking, does it make sense to wrap that code in bool
e.g. named isAuthenticated
then from my login page, do this
const submitFlow = (values: z.infer<typeof formSchema>) => {
// something unexpected went wrong and the flow was not set
if (!flow) return navigate({to: "/login"})
const body = {
csrf_token: values.csrf_token,
identifier: values.email,
method: "password",
password: values.password
} as UpdateLoginFlowBody
// we submit the flow to Ory with the form data
sdk
.updateLoginFlow({ flow: flow.id, updateLoginFlowBody: body })
.then(() => {
// we successfully submitted the login flow, so lets redirect to the dashboard
<auth.tsx>.isAuthenticated = true
navigate({ to: "/dashboard", replace: true })
})
.catch(sdkErrorHandler)
}
Then, I guess when redirected to /dashboard
it will run the code in my useEffect()
to then do .toSession() etc..
does it make sense?witty-fountain-21673
08/22/2024, 5:10 PMut there might be some pages i don’t need to check authentication on, e.g.read my comment above aboutbut the context will still do it./about
_.secured.tsx
and routes/_secured/
hundreds-psychiatrist-38730
08/22/2024, 5:10 PMhundreds-psychiatrist-38730
08/22/2024, 5:11 PMwitty-fountain-21673
08/22/2024, 5:12 PMwitty-fountain-21673
08/22/2024, 5:12 PMwitty-fountain-21673
08/22/2024, 5:18 PMBut there might be some pages i don’t need to check authentication on, e.g.I think this is fine. However it sounds like you’re storing other stuff in the context that you want to use inbut the context will still do it./about
/about
. In that case i’ll keep the auth context for auth stuff only and create a new context&hook for everything else. As a result, you wont get those whoami callswitty-fountain-21673
08/22/2024, 5:19 PM