0

I want to save a token in the redis cache after user sign-in in my app. The cache config.service.ts file:

@Injectable()
export class CacheConfigService {
  constructor(private configService: ConfigService) {}

  get url(): string {
    console.log(this.configService.get<string>('redis.url'));
    return this.configService.get<string>('redis.url') as string;
  }
}

The cacheModule file:

@Module({
  imports: [
    CacheModule.register<RedisClientOptions>({
      // @ts-ignore
      store: createRedisStore,
      socket: {
        host: 'localhost',
        port: 6379,
      },
      isGlobal: true,
      // url: 'redis://' + process.env.REDIS_HOST + ':' + process.env.REDIS_PORT,
    }),
  ],
})

The user signin method:

emailPasswordSignInPOST: async (input: any) => {
  if (
    originalImplementation.emailPasswordSignInPOST === undefined
  ) {
    throw Error('Should never come here');
  }
  let response: any =
    await originalImplementation.emailPasswordSignInPOST(input);

  const formFields: any = input.formFields;
  const inputObject: any = {};

  for (let index = 0; index < formFields.length; index++) {
    const element = formFields[index];
    inputObject[element.id] = element.value;
  }
  const { email } = inputObject;
  const user = await this.userService.findOneByEmail(email);
  const id = user?.id;

  const payload = { email, id };
  const secret = 'mysecret';
  const token = jwt.sign(payload, secret, {
    expiresIn: '2h',
  });
  response.token = token;
  const cacheKey = 'Token';
  await this.redisClient.set(cacheKey, token, {
    EX: 60 * 60 * 24,
  });
  return response;
},

Please note that this is a different module. When I send signin request from postman than it logs this error in the console: "Error: The client is closed"

I ran the app with the command DEBUG=* npm run start:dev to see the logs about redis but there is no log about redis.

John Oliver
  • 125
  • 1
  • 11
  • Does this answer your question? [Redis NodeJs server error,client is closed](https://stackoverflow.com/questions/70185436/redis-nodejs-server-error-client-is-closed) – Leibale Eidelman Jan 04 '23 at 14:59
  • @LeibaleEidelman no this is different – John Oliver Jan 04 '23 at 15:25
  • Which store are you using? https://www.npmjs.com/package/cache-manager-redis-store? – Leibale Eidelman Jan 04 '23 at 16:29
  • @LeibaleEidelman Yes, I am using the library that you have mentioned. It is not throwing the error: "The client is closed" now as I have set the `legacyMode: true` but it is not saving the cache data in the redis and it is not throwing any error either. I have tried the method `redisStore.on` method but it does not log anything – John Oliver Jan 04 '23 at 17:08
  • I'm pretty sure that `cache-manager-redis-store` is not working well with legacy mode, try without it – Leibale Eidelman Jan 04 '23 at 17:54
  • If i remove legacyMode:true option then it throws the error: the client is closed. But with that option it doesn't throw the error but it is not saving data to the redis – John Oliver Jan 05 '23 at 02:47
  • I think you need to await `createRedisStore` , have you tried following this https://github.com/dabroek/node-cache-manager-redis-store#single-store ? – Leibale Eidelman Jan 05 '23 at 03:45

0 Answers0