mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
2 changed files with 41 additions and 0 deletions
@ -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(); |
||||||
|
}); |
||||||
|
}); |
@ -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…
Reference in new issue