Skip to main content

Checking if a session exists on the frontend

Step 1: Wrap the component in which you want to get the info with ThirdPartyEmailPasswordAuth#

import React from "react";import { ThirdPartyEmailPasswordAuth } from 'supertokens-auth-react/recipe/thirdpartyemailpassword';import Dashboard from "./dashboard";
function ProtectedDashboard(props: any) {
      /*       * requireAuth = false means that even if a session doesn't exist,       * the dashboard component will be shown. The reason we have this set       * here is that if we didn't then doesSessionExist would always be       * true in the Dashboard component, in which case you would not        * have to check for if a session exists.      */      return (            <ThirdPartyEmailPasswordAuth requireAuth={false}>                  <Dashboard />            </ThirdPartyEmailPasswordAuth>      );}

Step 2: This is how to use the session context in a component:#

import React from "react";import { useSessionContext } from 'supertokens-auth-react/recipe/session'; 
// Your dashboard componentfunction Dashboard(props: any) {      let {doesSessionExist} = useSessionContext();
      if (doesSessionExist) {            // TODO..      } else {            // TODO..      }}