gray-motherboard-84683
03/12/2022, 5:03 PMinitialSession
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.steep-lamp-91158
Session | undefined
?gray-motherboard-84683
03/12/2022, 5:24 PMundefined
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 IMOsteep-lamp-91158
id === ""
? that seems way too brittle IMOgray-motherboard-84683
03/12/2022, 5:28 PMgray-motherboard-84683
03/12/2022, 5:29 PMinitializeSelfServiceLoginFlowForBrowsers
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?steep-lamp-91158
steep-lamp-91158
gray-motherboard-84683
03/12/2022, 5:35 PM<https://www.ory.sh/docs/kratos/reference/api#operation/toSession>
and the screen is blank for me. What’s the url for the revokeSession?gray-motherboard-84683
03/12/2022, 5:37 PMalso you should not need to define your own types, the sdk should already come with all typescript definitionsSo 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.
steep-lamp-91158
<Button onClick={session ? logout : login}/>
or
<p>Your Email is: {session?.identity.traits.email ?? 'You are not logged in'}</p>
rough-sunset-51786
03/12/2022, 6:08 PMDashboardLayout
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 timegray-motherboard-84683
03/12/2022, 6:29 PM