Skip to content

Commit

Permalink
make redis an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
asmeikal committed Feb 12, 2024
1 parent db1570f commit 932a5ab
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/security/cookie.jwt-strategy.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/security/cookie.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
5 changes: 5 additions & 0 deletions src/security/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Redis {
setex: (key: string, expiration: number, value: string) => Promise<void>;
del: (key: string) => Promise<void>;
get: (key: string) => Promise<string | null>;
}
4 changes: 2 additions & 2 deletions src/security/security.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 932a5ab

Please sign in to comment.