Next js route from axios interceptors – Invalid hook call. Hooks can only be called inside of the body of a function component

I am trying to add an Axios api interceptor to route the page to default login page if there is no access token.

import axios from 'axios';
import { useRouter } from 'next/navigation'

const router = useRouter();

const api = axios.create({
    baseURL: 'https://eventsapi.umapps.in',
  });
api.interceptors.request.use(
    (config) => {
          // check if token is available
          const accessToken = localStorage.getItem('accessToken');
          if(!accessToken)
          router.push('/')
          return Promise.reject("Login to continue!!");
    },
    (error) => {
        return Promise.reject(error);
    }
);

export default api;

I am getting the below error with the above code.

Unhandled Runtime Error
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

Couldn’t find a proper solution. Any inputs on how to fix this will be much helpful. Thanks!