sample(): remove old auth sample

This commit is contained in:
Kamil Myśliwiec
2019-07-11 22:41:22 +02:00
parent f899064410
commit 6980da363b
35 changed files with 0 additions and 226 deletions

View File

@@ -1,21 +0,0 @@
# dependencies
/node_modules
# IDE
/.idea
/.awcache
/.vscode
# misc
npm-debug.log
# example
/quick-start
# tests
/test
/coverage
/.nyc_output
# dist
/dist

View File

@@ -1,34 +0,0 @@
{
"name": "nest-typescript-starter",
"version": "1.0.0",
"description": "Nest TypeScript starter repository",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"start": "ts-node src/main",
"prestart:prod": "npm run build",
"start:prod": "node dist/main.js"
},
"dependencies": {
"@nestjs/common": "6.5.2",
"@nestjs/core": "6.5.2",
"@nestjs/jwt": "6.1.1",
"@nestjs/platform-express": "6.5.2",
"@nestjs/passport": "6.1.0",
"passport": "0.4.0",
"passport-http-bearer": "1.0.1",
"passport-jwt": "4.0.0",
"reflect-metadata": "0.1.13",
"rxjs": "6.5.2",
"typescript": "3.5.3"
},
"devDependencies": {
"@types/jest": "24.0.15",
"@types/node": "10.14.12",
"jest": "24.8.0",
"supertest": "4.0.2",
"ts-jest": "24.0.2",
"ts-node": "8.3.0",
"tslint": "5.18.0"
}
}

View File

@@ -1,7 +0,0 @@
import { Module } from '@nestjs/common';
import { AuthModule } from './auth/auth.module';
@Module({
imports: [AuthModule],
})
export class ApplicationModule {}

View File

@@ -1,20 +0,0 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from './auth.service';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Get('token')
async createToken(): Promise<any> {
return await this.authService.createToken();
}
@Get('data')
@UseGuards(AuthGuard())
findAll() {
// this route is restricted by AuthGuard
// JWT strategy
}
}

View File

@@ -1,21 +0,0 @@
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({
secret: 'secretKey',
signOptions: {
expiresIn: 3600,
},
}),
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy],
})
export class AuthModule {}

View File

@@ -1,23 +0,0 @@
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { JwtPayload } from './interfaces/jwt-payload.interface';
@Injectable()
export class AuthService {
constructor(private readonly jwtService: JwtService) {}
async createToken() {
const user: JwtPayload = { email: 'test@email.com' };
const accessToken = this.jwtService.sign(user);
return {
expiresIn: 3600,
accessToken,
};
}
async validateUser(payload: JwtPayload): Promise<any> {
// put some validation logic here
// for example query user by id/email/username
return {};
}
}

View File

@@ -1,22 +0,0 @@
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;
}
}

View File

@@ -1,3 +0,0 @@
export interface JwtPayload {
email: string;
}

View File

@@ -1,23 +0,0 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { AuthService } from './auth.service';
import { JwtPayload } from './interfaces/jwt-payload.interface';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'secretKey',
});
}
async validate(payload: JwtPayload) {
const user = await this.authService.validateUser(payload);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}

View File

@@ -1,8 +0,0 @@
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
await app.listen(3000);
}
bootstrap();

View File

@@ -1,8 +0,0 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"],
"compilerOptions": {
"types": []
}
}

View File

@@ -1,17 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./"
},
"exclude": ["node_modules"]
}

View File

@@ -1,19 +0,0 @@
{
"defaultSeverity": "error",
"extends": ["tslint:recommended"],
"jsRules": {
"no-unused-expression": true
},
"rules": {
"quotemark": [true, "single"],
"member-access": [false],
"ordered-imports": [false],
"max-line-length": [true, 150],
"member-ordering": [false],
"interface-name": [false],
"arrow-parens": false,
"no-console": false,
"object-literal-sort-keys": false
},
"rulesDirectory": []
}