Chris NowickiOriginal post
Rank 1: Thread
I am working on an app with a cohort team and we are using createEmailPasswordSession ... all of this is being run in nextJs via a context provider.
JavaScript
useEffect(() => { const getSession = async () => { try { await account.getSession('current'); console.log('User is signed in'); } catch (error) { console.log(error); } }; getSession(); }, []);this is great if a session exists. but if it doesn't it returns an error ... is there a better way to check if a session exists.
goal is on page loads to route user to a dashboard or not based on if they are logged in.
Summary
To check if a session exists without causing errors, you can use a conditional check like this:
```js
useEffect(() => {
const getSession = async () => {
const userSession = await account.getSession('current').catch(e => null);
if(userSession) {
console.log('User is signed in');
// Redirect user to dashboard
} else {
console.log('No user session found');
// Redirect user to login page
}
};
getSession();
}, []);
```