diff --git a/README.md b/README.md index 7f91e29..2fc111b 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ Remember to enable [cookie support](https://docs.nestjs.com/techniques/cookies) When using cookies, you can replace the access token with an opaque token if your access token may be too big for HTTP headers. -To enable the opaque token, configure the `redis` option. +To enable the opaque token, install `ioredis` as a dependency, +and configure the `redis` option. The access token will be stored on the configured redis server, and will be replaced in the cookies with a randomly generated token. diff --git a/package.json b/package.json index 6ec117e..ddc10c8 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,9 @@ }, "peerDependencies": { "@nestjs/common": "^10.3.2", - "@nestjs/core": "^10.3.2", + "@nestjs/core": "^10.3.2" + }, + "optionalDependencies": { "ioredis": "^5.3.2" }, "dependencies": { diff --git a/src/security/cookie.jwt-strategy.ts b/src/security/cookie.jwt-strategy.ts index e12dd0e..701408c 100644 --- a/src/security/cookie.jwt-strategy.ts +++ b/src/security/cookie.jwt-strategy.ts @@ -1,6 +1,6 @@ import { Strategy } from "passport-custom"; import { PassportStrategy } from "@nestjs/passport"; -import { Redis } from "ioredis"; +import { Redis } from "./redis"; import { JwtService } from "@nestjs/jwt"; import { REDIS_INJECTION_KEY, diff --git a/src/security/cookie.service.ts b/src/security/cookie.service.ts index 90f55e1..602573d 100644 --- a/src/security/cookie.service.ts +++ b/src/security/cookie.service.ts @@ -4,7 +4,7 @@ import { SECURITY_CONFIG_INJECTION_KEY, } from "../constants"; import moment from "moment"; -import { Redis } from "ioredis"; +import { Redis } from "./redis"; import { randomUUID } from "crypto"; import { SecurityModuleCookieOptions } from "./options"; import { ModuleRef } from "@nestjs/core"; diff --git a/src/security/redis.ts b/src/security/redis.ts new file mode 100644 index 0000000..a3564e1 --- /dev/null +++ b/src/security/redis.ts @@ -0,0 +1,5 @@ +export interface Redis { + setex: (key: string, expiration: number, value: string) => Promise; + del: (key: string) => Promise; + get: (key: string) => Promise; +} diff --git a/src/security/security.module.ts b/src/security/security.module.ts index 2fbdb3a..6daf34c 100644 --- a/src/security/security.module.ts +++ b/src/security/security.module.ts @@ -14,7 +14,6 @@ import { SecurityModuleOptions, } from "./options"; import { CookieJwtStrategy } from "./cookie.jwt-strategy"; -import Redis from "ioredis"; import { HeaderJwtStrategy } from "./header.jwt-strategy"; export interface SecurityModuleCookieAsyncOptions { @@ -88,8 +87,9 @@ export class SecurityModule { providers.push({ provide: REDIS_INJECTION_KEY, inject: options.inject, - useFactory(...args: any[]) { + useFactory: async (...args: any[]) => { const config = options.useFactory(...args); + const { Redis } = await import("ioredis"); return new Redis({ connectionName: "security", keyPrefix: "security",