Skip to content

Commit

Permalink
sample(nestjs) update auth sample
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Aug 26, 2018
1 parent 4d13ee8 commit 5a9d6d6
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 18 deletions.
6 changes: 3 additions & 3 deletions sample/11-swagger/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions sample/11-swagger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
"start:prod": "node dist/main.js",
"test": "jest --config=jest.json",
"test:watch": "jest --watch --config=jest.json",
"test:coverage":
"jest --config=jest.json --coverage --coverageDirectory=coverage",
"test:coverage": "jest --config=jest.json --coverage --coverageDirectory=coverage",
"e2e": "jest --config=e2e/jest-e2e.json --forceExit",
"e2e:watch": "jest --watch --config=e2e/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^5.1.0",
"@nestjs/core": "^5.1.0",
"@nestjs/microservices": "^5.1.0",
"@nestjs/swagger": "^2.4.2",
"@nestjs/swagger": "^2.4.4",
"@nestjs/testing": "^5.1.0",
"@nestjs/websockets": "^5.1.0",
"class-transformer": "^0.1.7",
Expand Down
12 changes: 6 additions & 6 deletions sample/11-swagger/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Controller, Get, Post, Body, Param } from '@nestjs/common';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import {
ApiUseTags,
ApiBearerAuth,
ApiResponse,
ApiOperation,
ApiResponse,
ApiUseTags,
} from '@nestjs/swagger';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';
import { Cat } from './interfaces/cat.interface';

@ApiBearerAuth()
@ApiUseTags('cats')
Expand Down
4 changes: 2 additions & 2 deletions sample/19-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"dependencies": {
"@nestjs/common": "^5.1.0",
"@nestjs/core": "^5.1.0",
"@nestjs/jwt": "0.0.1",
"@nestjs/passport": "^1.0.6",
"@nestjs/jwt": "0.1.1",
"@nestjs/passport": "^1.1.0",
"passport": "^0.4.0",
"passport-http-bearer": "^1.0.1",
"passport-jwt": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion sample/19-auth/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class AuthController {
}

@Get('data')
@UseGuards(AuthGuard('jwt'))
@UseGuards(AuthGuard())
findAll() {
// this route is restricted
}
Expand Down
2 changes: 2 additions & 0 deletions sample/19-auth/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { JwtStrategy } from './jwt.strategy';

@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secretOrPrivateKey: 'secretKey',
signOptions: {
Expand Down
22 changes: 22 additions & 0 deletions sample/19-auth/src/auth/guards/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
ExecutionContext,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
canActivate(context: ExecutionContext) {
// Add your custom authentication logic here
// for example, call super.logIn(request) to establish a session.
return super.canActivate(context);
}

handleRequest(err, user, info) {
if (err || !user) {
throw err || new UnauthorizedException();
}
return user;
}
}
6 changes: 3 additions & 3 deletions sample/19-auth/src/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
});
}

async validate(payload: JwtPayload, done: Function) {
async validate(payload: JwtPayload) {
const user = await this.authService.validateUser(payload);
if (!user) {
return done(new UnauthorizedException(), false);
throw new UnauthorizedException();
}
done(null, user);
return user;
}
}

0 comments on commit 5a9d6d6

Please sign in to comment.