Comments (10)
Just for someone who might also want to solve a similar problem. I found this also work well, so you can just import JWT module globally while sparing others:
@Module({
imports: [
{
...JwtModule.register({
secret: process.env.SECRET
}),
global: true
}
],
exports: [JwtModule]
})
export class CoreModule {}
from jwt.
@Global()
@Module({
imports: [
JwtModule.register({
secret: process.env.SECRET
})
],
exports: [JwtModule]
})
export class CoreModule {}
then import CoreModule
once, in your AppModule
.
from jwt.
@rostamiani you can open a PR to improve it https://github.com/nestjs/nest/blob/6ffdef14bc9e4a77d8b4b20a302fd864f31c9124/packages/common/interfaces/modules/module-metadata.interface.ts#L32-L36
This is documented here: https://docs.nestjs.com/modules#module-re-exporting
from jwt.
Read this chapter https://docs.nestjs.com/modules
Mainly these two:
from jwt.
@wxs77577 Have you found a way to import it globally? After reading the two links above I still found it's a bit difficult to do it
from jwt.
@kamilmysliwiec Thanks very much!
from jwt.
Could you just add property to the register method? or something else to match the consistency with other @nestjs packages. Thanks!
from jwt.
The current ts-doc for Module decorator is not good. It just mentions providers not modules:
Optional list of the subset of providers that are provided by this module and should be available in other modules which import this module.
from jwt.
So, unfortunately it looks like the built in JwtModule does not export very well or very easily unless you're doing it from the core project.
I tried overloading the implementation to try and force the module to be global, but sadly, if it's nested within a second layer module, NestJs does not properly respect the global flag (and I also tried exporting the same module and some variants)
{
...
imports: [
{
...JwtModule.register(options),
global: true
}
]
...
}
What I ended up doing was actually creating a custom JwtService / JwtModule that extended the original capability to make it work.
The only downside is that I cannot import the nestjs JwtService but instead just import the custom JwtDomainService everywhere - but since my service just extends the JwtService it should be a 1:1 change
From the module down:
./jwt-domain.module.ts
import { DynamicModule, Global, Module } from "@nestjs/common";
import {
JwtModuleAsyncOptions,
JwtModuleOptions,
} from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { JWT_OPTIONS } from "./config";
import { JwtDomainService } from "./services/jwt-domain.service";
import { JwtStrategy } from "./strategies";
@Global()
@Module({})
export class JwtDomainModule {
static forRoot(options: JwtModuleOptions): DynamicModule {
const module: DynamicModule = {
global: true,
module: JwtDomainModule,
imports: [PassportModule],
providers: [
JwtStrategy,
JwtDomainService,
{
provide: JWT_OPTIONS,
useValue: options,
},
],
exports: [JwtDomainService],
};
return module;
}
static forRootAsync(options: JwtModuleAsyncOptions): DynamicModule {
const module: DynamicModule = {
global: true,
module: JwtDomainModule,
imports: [...(options.imports || []), PassportModule],
providers: [
JwtStrategy,
JwtDomainService,
{
provide: JWT_OPTIONS,
useFactory: async (...args) => {
let config = options.useFactory(...args);
if (config instanceof Promise) config = await config;
return config;
},
inject: options.inject || [],
},
],
exports: [JwtDomainService],
};
return module;
}
}
./config
export const JWT_OPTIONS = "JWT_OPTIONS";
./services
import { Inject, Injectable } from "@nestjs/common";
import { JwtModuleOptions, JwtService } from "@nestjs/jwt";
import { JWT_OPTIONS } from "../config";
@Injectable()
export class JwtDomainService extends JwtService {
constructor(@Inject(JWT_OPTIONS) options: JwtModuleOptions) {
super(options);
}
}
./strategies
import { Inject, Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import { JwtTokenPayload } from "../interfaces";
import { JWT_OPTIONS } from "../config";
import { JwtModuleOptions } from "@nestjs/jwt";
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(@Inject(JWT_OPTIONS) options: JwtModuleOptions) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: options.secret,
jsonWebTokenOptions: { ignoreExpiration: false },
});
}
/// JwtTokenPayload is my custom interface that extends the JwtToken to include private claims
validate(payload: JwtTokenPayload): JwtTokenPayload {
return payload;
}
}
from jwt.
Some of the examples here seem a bit too complicated or hacky. Instead of register
use registerAsync
, and inject the ConfigService
.
@Module({
imports: [
JwtModule.registerAsync({
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
return {
secret: configService.get<string>('JWT_SECRET'),
signOptions: { expiresIn: '7d' },
};
},
}),
],
})
Note, I've configured the ConfigModule
as isGlobal
. If you haven't done this, you'll need to import the module inside of registerAsync
:
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
...
}),
from jwt.
Related Issues (20)
- sign function dosent take secret registred in module HOT 1
- secretOrPrivateKey must have a value HOT 3
- jwt module shoud extends ConfigurableModuleClass HOT 1
- Wrong dependency @types/jsonwebtoken HOT 1
- vulnerability in dependancy jsonwebtoken <=8.5.1 HOT 1
- Updating `jsonwebtoken` dependency HOT 3
- Update `@types/jsonwebtoken` to ^9.0.0 HOT 2
- secretOrPrivateKey must have a value HOT 1
- secretOrPrivateKey has a minimum key size of 2048 bits for RS256 after upgrade to nest/jwt 10.0.2 HOT 1
- How to invalidate a token? HOT 2
- BigInt not supported HOT 1
- type error while using `expiresIn` in `signAsync` HOT 7
- Expose internal Jsonwebtoken errors from @Next/jwt HOT 1
- Issue: Missing 'requestProperty' in @nestjs/jwt Library HOT 1
- JsonWebTokenError: secretOrPublicKey must be an asymmetric key when using RS256 HOT 1
- Error: secretOrPrivateKey must have a value HOT 1
- No valid invalid token for verification HOT 2
- wrong signature for sign() HOT 1
- Unexpected behavior when getting a value from .env file for expiresIn property via the ConfigModule / ConfigService HOT 1
- Can not import JwtService, JwtModule after installed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jwt.