skip to Main Content

Stack

  • Backend: django (dj_rest_auth for authentication)
  • Frontend: nextjs (next-auth for authentication, environment variables are set correctly.)
  • OS: Debian 12 – bookworm
  • Dependencies: redis, memcached, rabbitmq running inside LXD - Ubuntu 22.04 container

Summary

i have a nextjs(13+)+django(4.2+) application up and running on my localhost, now i am at the stage of adding authentication.

i am using next-auth(4.22+) to handle the authentication part.

When i try to sign in, i get this error AxiosError: connect ECONNREFUSED ::1:8000, every other request made by axios go through except the login one.

i have read other answers but none worked for me…

Error

{
  error: AxiosError: connect ECONNREFUSED ::1:8000
      at AxiosError.from (file:///home/yuri/Coding/pos/frontend/node_modules/axios/lib/core/AxiosError.js:89:14)
      at RedirectableRequest.handleRequestError (file:///home/yuri/Coding/pos/frontend/node_modules/axios/lib/adapters/http.js:591:25)
      at RedirectableRequest.emit (node:events:514:28)
      at eventHandlers.<computed> (/home/yuri/Coding/pos/frontend/node_modules/follow-redirects/index.js:14:24)
      at ClientRequest.emit (node:events:514:28)
      at Socket.socketErrorListener (node:_http_client:501:9)
      at Socket.emit (node:events:514:28)
      at emitErrorNT (node:internal/streams/destroy:151:8)
      at emitErrorCloseNT (node:internal/streams/destroy:116:3)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    port: 8000,
    address: '::1',
    syscall: 'connect',
    code: 'ECONNREFUSED',
    errno: -111,
    config: {
      transitional: [Object],
      adapter: [Array],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 0,
      xsrfCookieName: 'csrftoken',
      xsrfHeaderName: 'X-CSRFToken',
      maxContentLength: -1,
      maxBodyLength: -1,
      env: [Object],
      validateStatus: [Function: validateStatus],
      headers: [AxiosHeaders],
      baseURL: 'http://localhost:8000',
      withCredentials: true,
      paramsSerializer: [Object],
      method: 'post',
      url: '/api/auth/login/',
      data: '{"username":"yuri","password":"yuri","redirect":"false","csrfToken":"80d0b198ee09f1abc790d722d42e6fa48a0e38c78a77d4e7c4bd340c9cbb9636","callbackUrl":"http://localhost:3000/auth/sign-in?from=%2Fprofile","json":"true"}'
    },
    request: Writable {
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      _options: [Object],
      _ended: false,
      _ending: true,
      _redirectCount: 0,
      _redirects: [],
      _requestBodyLength: 216,
      _requestBodyBuffers: [Array],
      _onNativeResponse: [Function (anonymous)],
      _currentRequest: [ClientRequest],
      _currentUrl: 'http://localhost:8000/api/auth/login/',
      [Symbol(kCapture)]: false
    },
    cause: Error: connect ECONNREFUSED ::1:8000
        at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
      errno: -111,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '::1',
      port: 8000
    }
  }
}

Iptables

yuri@bookworm ~$ sudo iptables -L                                              
[sudo] password for yuri: 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

/etc/hosts

127.0.0.1       localhost
127.0.1.1       bookworm

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Before, i was using Ubuntu 23.04 but i decided to switch to Debian 12 (bookworm) due to system bloating, and this is the first time i have encountered this error.

How to fix this ? is there any explanation to this issue ?

2

Answers


  1. Chosen as BEST ANSWER

    i have finally figured it out.

    The reason Debian 12 system is using IPv6 (::1) instead of IPv4 (127.0.0.1) when i ping localhost is because Debian (and many other modern operating systems) prefer IPv6 over IPv4 when both are available, as a part of the trend towards the adoption of IPv6.

    According to chatGPT:

    IPv6 is the next-generation Internet Protocol, designed to replace IPv4 due to the exhaustion of available IPv4 addresses. To ensure a smooth transition to IPv6, many operating systems are configured to use IPv6 when it's available and fallback to IPv4 if it's not.

    To configure Debian to prefer IPv4 over IPv6, you can adjust the system's precedence for IPv4 and IPv6 in the network configuration. You can achieve this by modifying the /etc/gai.conf file, which controls the order of preference for different IP address families.

    Here's how you can set Debian to prefer IPv4 over IPv6:

    • Open the /etc/gai.conf file in a text editor with administrative privileges. You might need to use sudo:
    sudo nano /etc/gai.conf
    
    • Locate the section in the file that looks like this:
    #
    #    For sites which prefer IPv4 connections change the last line to
    #
    #precedence ::ffff:0:0/96  100
    

    To

    #
    #    For sites which prefer IPv4 connections change the last line to
    #
    precedence ::ffff:0:0/96  100
    
    • Save the file and exit the text editor, changes will take effect immediately.

    OR

    • Just use 127.0.0.1 and call it a day.

  2. Just use 127.0.0.1 instead of localhost at the time of axios request.

    const { data } = await axios.post('http://127.0.0.1:8000/api/token/', body, config);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search