Is there a built in `initialSession` const in the ...
# ory-network
Is there a built in
const in the
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.
can't you make it
Session | undefined
I mean… yea I technically can, but I don’t want to because I’d rather not have an
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
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
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
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?
also you should not need to define your own types, the sdk should already come with all typescript definitions
That link redirects to
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.
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}/>
Copy code
<p>Your Email is: {session? ?? 'You are not logged in'}</p>
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.
which redirects if
session check has finished loading && !session
, but returns
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
patterns is popular for this). A custom
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
variable to the context/hook as well (which is
session &&
), which makes your implementation less Ory-specific and lets you redefine what it means to be “authenticated” at a later time
Perfect. Thanks @User.
🙌 1