---
title: "Discussion on \"Authentication & Refresh token flow with Nextjs, Typescript, React Query and axios interceptors.\" | Hashnode"
description: "Discussion on \"Authentication & Refresh token flow with Nextjs, Typescript, React Query and axios interceptors.\". Implement user authentication from scratch using React, Next.js, Typescript, React Query and axios interceptors"
url: "https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Authentication & Refresh token flow with Nextjs, Typescript, React Query and axios interceptors.](https://srini-dev.hashnode.dev/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors?utm_source=hashnode&utm_medium=feed)

Implement user authentication from scratch using React, Next.js, Typescript, React Query and axios interceptors

Nov 14, 2022 · 8 min read · srini-dev.hashnode.dev  
Updated Sep 23, 2023  
Read the article

-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#nextjs](https://hashnode.com/tag/nextjs)
-   [#typescript](https://hashnode.com/tag/typescript)
-   [#login-authentication-in-react-js](https://hashnode.com/tag/login-authentication-in-react-js)
-   [#react-query](https://hashnode.com/tag/react-query)

## Written by

[K Srinivas Rao](https://hashnode.com/@srini53168)

@srini53168

  
[View profile](https://hashnode.com/@srini53168)

Frontend Engineer

Frontend Engineer

## Responses 10

Markdown supported · ⌘↵ to post

[Kakamotobi](https://hashnode.com/@Kakamotobi)

[Sep 4, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/64f5f080f6310e9375f96364)

In the `catch` in the response interceptor, wouldn't the toast disappear right away due to `window.location.href = window.location.origin`?

[K Srinivas Rao](https://hashnode.com/@srini53168)

[Sep 4, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/64f5f080f6310e9375f96364#reply-64f5f27ff6310e9375f96393)

Hey,

This code does have some flaws, it is meant for explaining the concept to someone starting out. I'll be writing a series of articles on production grade authentication which is going to cover everything in depth with the latest tools and patterns, stay tuned!

[Kakamotobi](https://hashnode.com/@Kakamotobi)

[Sep 4, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/64f5f080f6310e9375f96364#reply-64f5fbbdf6310e9375f964b1)

Right. I pointed it out because I have the same issue of trying to redirect the user to the signin page while keeping the toast rendered.

[Игорь Чехунов](https://hashnode.com/@Bimbaba)

[Jul 4, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/64a4415c84ae15f092e91c53)

если можно версию с куки, аксес рефреш токен, с защитой роутов и с тайпскриптом.

[Oussama Moutafatin](https://hashnode.com/@moutafatin)

[May 2, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/6450b2227a00e1d8014fb910)

The try-catch inside the interceptor does not catch anything, the interceptor keeps looping within itself

[K Srinivas Rao](https://hashnode.com/@srini53168)

[Sep 4, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/6450b2227a00e1d8014fb910#reply-64f5f23ef6310e9375f9638c)

Hey,

I'll be writing a series of articles on production grade authentication which is going to cover everything in depth with the latest tools and patterns, stay tuned!

[mohammad garmabi](https://hashnode.com/@garmabi07)

[Nov 15](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/6450b2227a00e1d8014fb910#reply-6af9aee68467285acb025583)

The loop usually happens because the /refresh call goes through the same axios instance and hits the same 401 handler. Two fixes: use a separate axios instance (or a skipAuthRefresh flag) for the refresh request, and mark the original request with \_retry = true so it's only replayed once. If the refresh itself fails, reject the queued promises and log out instead of retrying.

[Gabhan O'Loughlin](https://hashnode.com/@Gabhan)

[Apr 14, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/64396f0af49f45ac41003401)

The axiosInstance.ts file no longer works with the latest React Query / Typescript (April 2023). Hopefully you can update the article. As someone who is new to React, your approach makes total sense to me, but I'm struggling to figure out how to make it work.

[K Srinivas Rao](https://hashnode.com/@srini53168)

[Sep 4, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/64396f0af49f45ac41003401#reply-64f5f212f6310e9375f96383)

Hi Gabhan,

I'll be writing a series of articles on production grade authentication which is going to cover everything in depth with the latest tools and patterns, stay tuned!

[Damilola Ogunboyejo](https://hashnode.com/@Damilola2020)

[Apr 3, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/642b6354b76d88fc811e7b2f)

Hi K Srinivas Rao, can I get your email please, I need help with react query and nextjs

[K Srinivas Rao](https://hashnode.com/@srini53168)

[Sep 4, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/642b6354b76d88fc811e7b2f#reply-64f5f227f6310e9375f96388)

You can find it on my blog profile

[Dennis Githinji Wakahia](https://hashnode.com/@densoft)

[Jan 4, 2023](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/63b5cca773ee0387bc7a89b3)

nn

[Gokul Gopi](https://hashnode.com/@GokulG)

[Nov 22, 2022](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/637ca4aef3615bbe07eafcee)

Everything was explained pretty nicely. Now I have much more clarity around this topic 👍. Waiting for the backend part..

[Ahmad Yousif](https://hashnode.com/@Jo89)

[Nov 14, 2022](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/63726a85da4b9943800c6309)

I would do it slightly differently, I would send the rT in a cookie instead of sending it in the response , and in the frontend I resend this cookie on every request for protected route back to the backend and compare it's value with the original rT sent upon authentication which would be stored in the db, and maybe not storing the tokens in the local storage but rather in the memory.

[K Srinivas Rao](https://hashnode.com/@srini53168)

[Nov 15, 2022](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/63726a85da4b9943800c6309#reply-637323c794b18c4621bda14b)

Ah, yes.. Great suggestion... It's never a good idea to store the tokens on frontend. But I wanted to explain only the frontend side of the authentication in a visual manner, that's why I went with it. I'll explore the full stack side of it another day in a more secure way.

[Tyler](https://hashnode.com/@Neziedev)

[Nov 14, 2022](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/637261a45eac58431589737a)

I would appreciate if you could go into the backend as well 🙃. Great article!

[K Srinivas Rao](https://hashnode.com/@srini53168)

[Nov 15, 2022](https://hashnode.com/posts/authentication-refresh-token-flow-with-nextjs-typescript-react-query-and-axios-interceptors/6371d17cfb40eef9d39b7930/comment/637261a45eac58431589737a#reply-637323d594b18c4621bda160)

Sure, coming soon...
