# Redirect/Log out flow

**URL:** <https://community.auth0.com/t/redirect-log-out-flow/119175>\
**Category:** Get Help\
**Tags:** authentication-sessions, sessions\
**Created:** [October 23, 2023, 3:55pm UTC](https://community.auth0.com/t/redirect-log-out-flow/119175 "2023-10-23T15:55:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![virajvaitha1995](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/virajvaitha1995/32/51319_2.png) [@virajvaitha1995](https://community.auth0.com/u/virajvaitha1995)\
**Post date:** [October 23, 2023, 3:55pm UTC](https://community.auth0.com/t/redirect-log-out-flow/119175/1 "2023-10-23T15:55:46Z")

</div>

Hey all

Im new to auth0, and have a website that users log into.

It’s important on each page, i check if they are logged in otherwise force them to ‘/’ path. This is how i’ve impmented it succesfully, but is it correct/best practices?

if (user) {

return ( placeholder}

}  
return (  
router.push(‘/’)  
)  
}

This is how my web app checks if a user is logged in, otherwise it redirects. Am I doing this correctly? I cant find the documentation or explanation for this usecase, and just want to follow best practices.

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [October 24, 2023, 2:42pm UTC](https://community.auth0.com/t/redirect-log-out-flow/119175/3 "2023-10-24T14:42:31Z")

</div>

Hi @virajvaitha1995

Welcome back in the Auth0 Community!

Thank you for posting your question. Generally what you are looking for are protected routes. If the user is not logged \<=\> they don’t have permission to visit your page.

In terms of provided code snippet the few things that could make it “better” are:

- **Avoid Side Effects in Render** :
  - Directly calling a redirect (like `router.push('/')`) in the body of a component can lead to unexpected behaviors and can be considered a side effect.
  - Instead, consider using React’s `useEffect` hook to handle side effects like redirection.

- **Error Handling** : What happens if the redirection fails? Or what if there’s an error with the authentication state? It might be good to have some error handling in place.

I will leave for you a few code snippets that can help you follow the best practise (assuming you are using React).

> **[React Authentication By Example: Using React Router 6](https://developer.auth0.com/resources/guides/spa/react/basic-authentication#quick-react-setup)**
>
> Learn how to add authentication features to React v18 apps: add login, logout, and sign-up. Learn how to integrate Auth0 with React Router v6 to protect React pages using router guards and how to call protected APIs.

> **[React Router 6 Code Sample: Basic Authentication](https://developer.auth0.com/resources/code-samples/spa/react/basic-authentication)**
>
> Code sample showing how to protect a simple React single-page application using React Router 6, Auth0, and JavaScript.

Thanks  
Dawid

---

<div class="post-metadata">

**Author:** ![virajvaitha1995](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/virajvaitha1995/32/51319_2.png) [@virajvaitha1995](https://community.auth0.com/u/virajvaitha1995)\
**Post date:** [October 26, 2023, 12:34pm UTC](https://community.auth0.com/t/redirect-log-out-flow/119175/4 "2023-10-26T12:34:38Z")

</div>

> [@dawid.matuszczyk](#):
>
> Welcome back in the Auth0 Community!
> 
> Thank you for posting your question. Generally what you are looking for are protected routes. If the user is not logged \<=\> they don’t have permission to visit your page.
> 
> In terms of provided code snippet the few things that could make it “better” are:
> 
> - **Avoid Side Effects in Render** :
> - Directly calling a redirect (like `router.push('/')`) in the body of a component can lead to unexpected behaviors and can be considered a side effect.
> - Instead, consider using React’s `useEffect` hook to handle side effects like redirection.

Thanks so much for your response  
I am using NextJS auth0, was a little difficult to follow examples as i am new to frontend.

If you get time, can you confirm the new code is inline with best practices.

Kind regards,

Viraj

```auto
 const { user, error, isLoading } = useUser();

  // Redirect if not authenticated
  useEffect(() => {
    if (!isLoading && !user) {
      router.push('/');
    }
  }, [isLoading, user]);

  // Show loading state
  if (isLoading) return <div></div>;

  // Show error state
  if (error) return <div>{error.message}</div>;

  // If user is authenticated
  if (user) {
    return (
      <main>

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [November 9, 2023, 12:34pm UTC](https://community.auth0.com/t/redirect-log-out-flow/119175/5 "2023-11-09T12:34:42Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
