---
title: "Comment by Martin Filteau, Eng., PMP on \"React & JWT Authentication - the right way!\" | Hashnode"
description: "Assuming that the user has logged previously and that we have a valid refresh-token stored in a cookie, when we start the application again, user will be null u"
url: "https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2/comment/5fad8694fa438d6532c7e735"
---

[Discussion](https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2)

Comment [React & JWT Authentication - the right way!](https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2)

[Martin Filteau, Eng., PMP](https://hashnode.com/@mfilteau)

[Nov 12, 2020](https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2/comment/5fad8694fa438d6532c7e735)

Assuming that the user has logged previously and that we have a valid refresh-token stored in a cookie, when we start the application again, user will be null until the token is refreshed.

Since user is null, any protected route will be redirected to login.

How do you usually wait for the first refresh to complete so the user is not redirected to login?

[Martin Filteau, Eng., PMP](https://hashnode.com/@mfilteau)

[Nov 12, 2020](https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2/comment/5fad8694fa438d6532c7e735#reply-5fad9827fa438d6532c7e824)

I figured it out. I've added a state 'waitingForToken' that is initially true.

```
    const [user, setUser] = useState(null);
    const [waitingForToken, setWaitingForToken] = useState(true);
    const refreshToken = useCallback(refresh, []);
```

In refresh(), I set the state to false.

```
    async function refresh() {
        try {
            const {
                data: { user, ...rest },
            } = await axios.post('refresh-token', {}, { withCredentials: true });
            setUser(user);
            setToken(rest);
        }
        finally {
            setWaitingForToken(false);
        }
    }
```

And export it:

```
    return {
        user,
        waitingForToken,
        setUser,
```

Then in my App:

```
const App = () => {
    const { waitingForToken } = AuthContainer.useContainer();
    return (waitingForToken ? <div/> :
        <Switch>
            <Route path="/login" component={Login} />
            <ProtectedRoute exact={true} path="/" component={Dashboard} />
            <ProtectedRoute path="/settings" component={Settings} />
            <ProtectedRoute component={Dashboard} />
        </Switch>                
    );
```

Hope this can help someone!

[Gal Malachi](https://hashnode.com/@galm)

[Nov 13, 2020](https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2/comment/5fad8694fa438d6532c7e735#reply-5fae3a27fa438d6532c7ed53)

Great addition, thanks [Martin Filteau, Eng., PMP](https://hashnode.com/@mfilteau) 💪🏽
