Browse Source

feat: introduce jwt strategy

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
2116da8fa3
  1. 18
      packages/nocodb-nest/src/strategies/jwt.strategy.spec.ts
  2. 23
      packages/nocodb-nest/src/strategies/jwt.strategy.ts

18
packages/nocodb-nest/src/strategies/jwt.strategy.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JwtStrategy } from './jwt.strategy';
describe('JwtStrategy', () => {
let provider: JwtStrategy;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [JwtStrategy],
}).compile();
provider = module.get<JwtStrategy>(JwtStrategy);
});
it('should be defined', () => {
expect(provider).toBeDefined();
});
});

23
packages/nocodb-nest/src/strategies/jwt.strategy.ts

@ -0,0 +1,23 @@
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { AuthService } from './auth.service';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: 'your-secret-key',
});
}
async validate(payload: any) {
const user = await this.authService.validateUser(payload);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Loading…
Cancel
Save