Is there a built in `initialSession` const in the ...
# ory-network
g
Is there a built in
initialSession
const in the
@ory/client
that allows me to set an empty session when creating a React.context? I created something below in the pic, but not sure if this is the best approach. I’m trying to set up a AuthContext that I can use in my other components, but want to be able to render components if there is no session available from the Ory Client, specifically for Kratos.
s
can't you make it
Session | undefined
?
g
I mean… yea I technically can, but I don’t want to because I’d rather not have an
undefined
type as an or in there and anywhere I check on the session, I have to
?
. just doesn’t feel right and could be easily fixed with an initial state IMO
s
it is an easy way to tell if there is a session or not how do you tell with an initial state?
id === ""
? that seems way too brittle IMO
g
That’s a good question, which leads me to another question that I’m still trying to figure out. What is a valid session from ory? That the session is active and/or authenticated and/or not expired?
So you get back the session object from doing the
initializeSelfServiceLoginFlowForBrowsers
and it returns back a Session object, but how can I tell if that object is valid? And on what conditions, if I ever, will need to get a new session?
s
also you should not need to define your own types, the sdk should already come with all typescript definitions
g
That link redirects to
<https://www.ory.sh/docs/kratos/reference/api#operation/toSession>
and the screen is blank for me. What’s the url for the revokeSession?
also you should not need to define your own types, the sdk should already come with all typescript definitions
So you solution is to type the context as <Session|undefined> and initialize to an undefined variable? I’m just trying to get a sense of how this all comes together. Any help is appreciated.
s
I mean this is details of how you implement your application, but I would do that probably then if you have a login/logout button (for example):
Copy code
<Button onClick={session ? logout : login}/>
or
Copy code
<p>Your Email is: {session?.identity.traits.email ?? 'You are not logged in'}</p>
r
If most if not all of your app is gated behind auth, the easy way to get around this is to create layouts that will perform some kind of redirect if not authenticated. e..g.
DashboardLayout
which redirects if
session check has finished loading && !session
, but returns
children
if the session is active. If you get past this mechanism, you can say with certainty a session exists in underlying components - by either prop drilling it, or only initialising your react context for sessions at layout level (the
AuthProvider
patterns is popular for this). A custom
useSession
or
useAuth
hook is also a good candidate here. If your application is less binary and you’re sprinkling in (or removing) functionality based on auth status, there is no way to get around doing an auth check. If syntax/verbosity is an issue, I usually set a derived
isAuthenticated
variable to the context/hook as well (which is
session && session.active
), which makes your implementation less Ory-specific and lets you redefine what it means to be “authenticated” at a later time
g
Perfect. Thanks @User.
🙌 1