## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE) file for details
================================================
FILE: nest-auth/.dockerignore
================================================
/dist
/node_modules
================================================
FILE: nest-auth/.gitignore
================================================
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
================================================
FILE: nest-auth/.prettierrc
================================================
{
"singleQuote": true,
"trailingComma": "all"
}
================================================
FILE: nest-auth/Dockerfile
================================================
FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
EXPOSE 3000
CMD [ "npm", "start" ]
================================================
FILE: nest-auth/README.md
================================================
[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
[travis-url]: https://travis-ci.org/nestjs/nest
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
[linux-url]: https://travis-ci.org/nestjs/nest
A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
## Description [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. ## Installation ```bash $ npm install ``` ## Running the app ```bash # development $ npm run start # watch mode $ npm run start:dev # production mode $ npm run start:prod ``` ## Test ```bash # unit tests $ npm run test # e2e tests $ npm run test:e2e # test coverage $ npm run test:cov ``` ## Support Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). ## Stay in touch - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) - Website - [https://nestjs.com](https://nestjs.com/) - Twitter - [@nestframework](https://twitter.com/nestframework) ## License Nest is [MIT licensed](LICENSE). ================================================ FILE: nest-auth/docker-compose.yml ================================================ version: '3' services: nodejs: build: context: ./ dockerfile: Dockerfile container_name: nodejs restart: always environment: - DATABASE_HOST=mongo - PORT=3000 ports: - '3000:3000' depends_on: [mongo] mongo: container_name: mongo image: mongo ports: - '27017:27017' volumes: - mongo_data:/data/db volumes: mongo_data: {} ================================================ FILE: nest-auth/nest-cli.json ================================================ { "language": "ts", "collection": "@nestjs/schematics", "sourceRoot": "src" } ================================================ FILE: nest-auth/nodemon-debug.json ================================================ { "watch": ["src"], "ext": "ts", "ignore": ["src/**/*.spec.ts"], "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts" } ================================================ FILE: nest-auth/nodemon.json ================================================ { "watch": ["dist"], "ext": "js", "exec": "node dist/main" } ================================================ FILE: nest-auth/package.json ================================================ { "name": "nest-auth", "version": "0.0.1", "description": "", "author": "", "license": "MIT", "scripts": { "build": "tsc -p tsconfig.build.json", "format": "prettier --write \"src/**/*.ts\"", "start": "ts-node -r tsconfig-paths/register src/main.ts", "start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ", "start:debug": "nodemon --config nodemon-debug.json", "prestart:prod": "rimraf dist && npm run build", "start:prod": "node dist/main.js", "lint": "tslint -p tsconfig.json -c tslint.json", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@nestjs/common": "8.2.6", "@nestjs/core": "8.2.6", "@nestjs/mongoose": "9.0.2", "@nestjs/passport": "8.1.0", "@nestjs/platform-express": "8.2.6", "bcrypt": "5.0.1", "dotenv": "14.3.2", "jsonwebtoken": "8.5.1", "mongoose": "6.1.8", "passport": "0.5.2", "passport-jwt": "4.0.0", "reflect-metadata": "0.1.13", "rimraf": "3.0.2", "rxjs": "7.5.2" }, "devDependencies": { "@nestjs/testing": "8.2.6", "@types/express": "4.17.13", "@types/jest": "27.4.0", "@types/jsonwebtoken": "8.5.8", "@types/mongoose": "5.11.96", "@types/node": "17.0.12", "@types/passport-local-mongoose": "6.1.0", "@types/supertest": "2.0.11", "concurrently": "7.0.0", "jest": "27.4.7", "nodemon": "2.0.15", "prettier": "2.5.1", "supertest": "6.2.2", "ts-jest": "27.1.3", "ts-node": "10.4.0", "tsconfig-paths": "3.12.0", "tslint": "5.20.1", "typescript": "4.5.5", "wait-on": "6.0.0" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, "coverageDirectory": "../coverage", "testEnvironment": "node" }, "keywords": [ "Nestjs", "Typescript", "Authentification", "Passport", "JWT" ] } ================================================ FILE: nest-auth/src/app.controller.ts ================================================ import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get() getHello(): string { return this.appService.getHello(); } } ================================================ FILE: nest-auth/src/app.module.ts ================================================ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { UserModule } from './user/user.module'; import { MongooseModule } from '@nestjs/mongoose'; import { AuthModule } from './auth/auth.module'; @Module({ imports: [ UserModule, MongooseModule.forRoot(`mongodb://${process.env.DATABASE_HOST}/nestauth`), AuthModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule {} ================================================ FILE: nest-auth/src/app.service.ts ================================================ import { Injectable } from '@nestjs/common'; @Injectable() export class AppService { getHello(): string { return 'Hello World!'; } } ================================================ FILE: nest-auth/src/auth/auth.controller.ts ================================================ import { Controller, Post, Body, Get, UseGuards } from '@nestjs/common'; import { UserService } from '../user/user.service'; import { AuthService } from './auth.service'; import { LoginUserDto } from '../user/dto/login-user.dto'; import { AuthGuard } from '@nestjs/passport'; import { Payload } from '../types/payload'; import { CreateUserDto } from '../user/dto/create-user.dto'; @Controller('auth') export class AuthController { constructor(private userService: UserService, private authService: AuthService) {} @Get() @UseGuards(AuthGuard('jwt')) tempAuth() { return {auth: 'works'}; } @Post('login') async login(@Body() userDTO: LoginUserDto) { const user = await this.userService.findByLogin(userDTO); const payload: Payload = { email: user.email, }; const token = await this.authService.signPayload(payload); return { user, token }; } @Post('register') async register(@Body() userDTO: CreateUserDto) { const user = await this.userService.create(userDTO); const payload: Payload = { email: user.email, }; const token = await this.authService.signPayload(payload); return { user, token }; } } ================================================ FILE: nest-auth/src/auth/auth.module.ts ================================================ import { Module } from '@nestjs/common'; import { AuthService } from './auth.service'; import { AuthController } from './auth.controller'; import { UserModule } from '../user/user.module'; import { JwtStrategy } from './jwt.strategy'; @Module({ imports: [UserModule], providers: [AuthService, JwtStrategy], controllers: [AuthController], }) export class AuthModule {} ================================================ FILE: nest-auth/src/auth/auth.service.ts ================================================ import { Injectable } from '@nestjs/common'; import { UserService } from '../user/user.service'; import { Payload } from 'src/types/payload'; import { sign } from 'jsonwebtoken'; @Injectable() export class AuthService { constructor(private userService: UserService) {} async signPayload(payload: Payload) { return sign(payload, 'secretKey', { expiresIn: '12h' }); } async validateUser(payload: Payload) { return await this.userService.findByPayload(payload); } } ================================================ FILE: nest-auth/src/auth/jwt.strategy.ts ================================================ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { Strategy, ExtractJwt, VerifiedCallback } from 'passport-jwt'; import { AuthService } from './auth.service'; import 'dotenv/config' @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor(private authService: AuthService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: process.env.SECRET_KEY, }); } async validate(payload: any, done: VerifiedCallback) { const user = await this.authService.validateUser(payload); if (!user) { return done( new HttpException('Unauthorized access', HttpStatus.UNAUTHORIZED), false, ); } return done(null, user, payload.iat); } } ================================================ FILE: nest-auth/src/main.ts ================================================ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.enableCors(); await app.listen(process.env.PORT); } bootstrap(); ================================================ FILE: nest-auth/src/models/user.schema.ts ================================================ import * as mongoose from 'mongoose'; import * as bcrypt from 'bcrypt'; export const UserSchema = new mongoose.Schema({ email: { type: String, unique: true, required: true, }, password: { type: String, required: true, }, }); UserSchema.pre('save', async function(next: mongoose.HookNextFunction) { try { if (!this.isModified('password')) { return next(); } // tslint:disable-next-line: no-string-literal const hashed = await bcrypt.hash(this['password'], 10); // tslint:disable-next-line: no-string-literal this['password'] = hashed; return next(); } catch (err) { return next(err); } }); ================================================ FILE: nest-auth/src/types/payload.ts ================================================ export interface Payload { email: string; expiresIn?: string; } ================================================ FILE: nest-auth/src/types/user.ts ================================================ import { Document } from 'mongoose'; export interface User extends Document { readonly email: string; readonly password: string; } ================================================ FILE: nest-auth/src/user/dto/create-user.dto.ts ================================================ export class CreateUserDto { readonly email: string; readonly password: string; } ================================================ FILE: nest-auth/src/user/dto/login-user.dto.ts ================================================ export class LoginUserDto { readonly email: string; readonly password: string; } ================================================ FILE: nest-auth/src/user/user.module.ts ================================================ import { Module } from '@nestjs/common'; import { UserService } from './user.service'; import { UserSchema } from '../models/user.schema'; import { MongooseModule } from '@nestjs/mongoose'; @Module({ imports: [MongooseModule.forFeature([{name: 'User', schema: UserSchema}])], providers: [UserService], controllers: [], exports: [UserService, MongooseModule.forFeature([{name: 'User', schema: UserSchema}])], }) export class UserModule {} ================================================ FILE: nest-auth/src/user/user.service.ts ================================================ import { Injectable, Logger, HttpException, HttpStatus } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { User } from '../types/user'; import { CreateUserDto } from '../user/dto/create-user.dto'; import { LoginUserDto } from '../user/dto/login-user.dto'; import * as bcrypt from 'bcrypt'; @Injectable() export class UserService { constructor(@InjectModel('User') private readonly userModel: ModelA progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
## Description [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. ## Installation ```bash $ npm install ``` ## Running the app ```bash # development $ npm run start # watch mode $ npm run start:dev # production mode $ npm run start:prod ``` ## Test ```bash # unit tests $ npm run test # e2e tests $ npm run test:e2e # test coverage $ npm run test:cov ``` ## Support Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). ## Stay in touch - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) - Website - [https://nestjs.com](https://nestjs.com/) - Twitter - [@nestframework](https://twitter.com/nestframework) ## License Nest is [MIT licensed](LICENSE). ================================================ FILE: nestjs-typeorm/docker-compose.yaml ================================================ version: '3.7' services: mongodb: image: mongo:latest ports: - 27017:27017 volumes: - mongodb_data:/data/db volumes: mongodb_data: ================================================ FILE: nestjs-typeorm/nest-cli.json ================================================ { "language": "ts", "collection": "@nestjs/schematics", "sourceRoot": "src" } ================================================ FILE: nestjs-typeorm/nodemon-debug.json ================================================ { "watch": ["src"], "ext": "ts", "ignore": ["src/**/*.spec.ts"], "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts" } ================================================ FILE: nestjs-typeorm/nodemon.json ================================================ { "watch": ["dist"], "ext": "js", "exec": "node dist/main" } ================================================ FILE: nestjs-typeorm/package.json ================================================ { "name": "nestjs-typeorm", "version": "0.0.1", "description": "", "author": "", "license": "MIT", "scripts": { "build": "tsc -p tsconfig.build.json", "format": "prettier --write \"src/**/*.ts\"", "start": "ts-node -r tsconfig-paths/register src/main.ts", "start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ", "start:debug": "nodemon --config nodemon-debug.json", "prestart:prod": "rimraf dist && npm run build", "start:prod": "node dist/main.js", "lint": "tslint -p tsconfig.json -c tslint.json", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@nestjs/common": "8.2.6", "@nestjs/core": "8.2.6", "@nestjs/platform-express": "8.2.6", "@nestjs/typeorm": "8.0.3", "mongodb": "^3.7.3", "reflect-metadata": "0.1.13", "rimraf": "3.0.2", "rxjs": "7.5.2", "typeorm": "0.2.41" }, "devDependencies": { "@nestjs/testing": "8.2.6", "@types/express": "4.17.13", "@types/jest": "27.4.0", "@types/node": "17.0.12", "@types/supertest": "2.0.11", "concurrently": "7.0.0", "jest": "27.4.7", "nodemon": "2.0.15", "prettier": "2.5.1", "supertest": "6.2.2", "ts-jest": "27.1.3", "ts-node": "10.4.0", "tsconfig-paths": "3.12.0", "tslint": "5.20.1", "typescript": "4.5.5", "wait-on": "6.0.0" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, "coverageDirectory": "../coverage", "testEnvironment": "node" }, "keywords": [ "Nestjs", "Typescript", "Typeorm", "Nestjs typeorm" ] } ================================================ FILE: nestjs-typeorm/src/app.controller.ts ================================================ import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get() getHello(): string { return this.appService.getHello(); } } ================================================ FILE: nestjs-typeorm/src/app.module.ts ================================================ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { TypeOrmModule } from '@nestjs/typeorm'; import { join } from 'path'; import { ItemModule } from './item/item.module'; @Module({ imports: [TypeOrmModule.forRoot({ type: 'mongodb', host: 'localhost', database: 'test', entities: [join(__dirname, '**/**.entity{.ts,.js}')], synchronize: true, }), ItemModule], controllers: [AppController], providers: [AppService], }) export class AppModule {} ================================================ FILE: nestjs-typeorm/src/app.service.ts ================================================ import { Injectable } from '@nestjs/common'; @Injectable() export class AppService { getHello(): string { return 'Hello World!'; } } ================================================ FILE: nestjs-typeorm/src/item/item.controller.ts ================================================ import { Controller, Get, Param, Post, Body, Put, Delete, HttpException, HttpStatus } from '@nestjs/common'; import { ItemService } from './item.service'; import { Item } from './item.entity'; import { ItemDto } from './item.dto'; @Controller('item') export class ItemController { constructor(private itemService: ItemService) {} @Get() async findAllItems(): Promise