Node-Redis: ready check failed - NOAUTH Authentication required

Viewed 24126

I have a strange redis behavior:

const redis = require('redis');
const { REDIS_URL: redisUrl, REDIS_PASSWORD: redisPassword } = process.env;

const client = redis.createClient(redisUrl, {
  no_ready_check: true,
  auth_pass: redisPassword
});

client.on('connect', () => {
  redisPassword && client.auth(redisPassword);
});

client.on('error', err => {
  global.console.log(err.message)
});

But all the time I receive following error:

throw er; // Unhandled 'error' event

ReplyError: Ready check failed: NOAUTH Authentication required.

Why unhandled ? I set onerror handler
Why Ready check failed ? I disabled it in options

3 Answers

If you have redis uri saved as string. You need decompose it to object. For ioredis you can use function

export function decomposeRedisUrl(url) {
  const [[, , password, host, port]] = [...(url.matchAll(/redis:\/\/(([^@]*)@)?(.*?):(\d*)/g))];
  return { password, host, port };
}

There are tests for this function:

it("redis url should be decomposed correctly with password", () => {
  expect(decomposeRedisUrl("redis://pass@host.com:9183")).to.eql({
    password: "pass",
    host: "host.com",
    port: "9183",
  });
});

it("redis url should be decomposed correctly without password", () => {
  expect(decomposeRedisUrl("redis://localhost:6379")).to.eql({
    password: undefined,
    host: "localhost",
    port: "6379",
  });
});

and usage

import Redis from "ioredis";

async function getKeysFromRedisUrl(url) {
  const rc = new Redis(decomposeRedisUrl(url));
  const keys = await rc.keys("*");
  rc.disconnect();
  return keys;
}

describe("Redis can connect", () => {
  it("with cloud", async () => {
    expect(await getKeysFromRedisUrl("redis://pass@host.com:9183")).to.be.an("array");
  });
  it("with local redis instance", async () => {
    expect(await getKeysFromRedisUrl("redis://localhost:6379")).to.be.an("array");
  });
});
  • user name is not handled in this function

if you're using docker to run Redis, check if your docker-compose has the command: redis-server --requirepass redis

Then check your .env file to make sure you're using it. It was the problem here and I was able to fix it by adding the password at .env file.

Related