---
title: "Martin Filteau, Eng., PMP (@mfilteau) | Hashnode"
description: "Martin Filteau, Eng., PMP (@mfilteau) writes on Hashnode. Read their latest articles and follow their work."
url: "https://hashnode.com/@mfilteau"
---

@mfilteau

# Martin Filteau, Eng., PMP

@mfilteau · Joined November 2020

## Comments

[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!](https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2/comment/5fad8694fa438d6532c7e735#reply-5fad9827fa438d6532c7e824)

Reply · Article · Nov 12, 2020 · [React & JWT Authentication - the right way!](https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2)

[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?](https://hashnode.com/posts/react-and-jwt-authentication-the-right-way/5f7332311d7a870a345b9db2/comment/5fad8694fa438d6532c7e735)

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