From 866381fc54b6ddc1c2a28a70e0a217b377cfa450 Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Mon, 1 Jun 2020 03:40:41 +0200 Subject: [PATCH] add static files --- .gitignore | 1 + Dockerfile | 2 +- docker-compose.yml | 5 +++-- package.json | 1 + public/.gitkeep | 0 src/app.imports.ts | 7 +++++++ src/service/auth/auth.service.ts | 15 ++++++++++----- src/service/user/boot.service.ts | 5 ++++- yarn.lock | 7 +++++++ 9 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 public/.gitkeep diff --git a/.gitignore b/.gitignore index bfbdabe..8f27565 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # compiled output /dist /node_modules +/public # Logs logs diff --git a/Dockerfile b/Dockerfile index 2b73b4c..84c55ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ COPY . . RUN yarn install --frozen-lockfile RUN yarn build -FROM node:12-alpine +FROM node:12 WORKDIR /usr/src/app diff --git a/docker-compose.yml b/docker-compose.yml index 99b0908..169ce89 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,12 +17,13 @@ services: # environment: # MONGODB_URI: mongodb://mongo/ohmyform # MAILER_URI: smtp://mail:1025 -# command: yarn start:dev +# PORT: 3000 + #command: yarn start:dev # links: # - mongo # - mail # ports: -# - "6000:3000" +# - "6100:3000" # depends_on: # - mongo mail: diff --git a/package.json b/package.json index e2bdb97..e35868c 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "@nestjs/mongoose": "^6.4.0", "@nestjs/passport": "^7.0.0", "@nestjs/platform-express": "^7.0.0", + "@nestjs/serve-static": "^2.1.1", "@types/html-to-text": "^5.1.0", "apollo-server-express": "^2.13.0", "bcrypt": "^4.0.1", diff --git a/public/.gitkeep b/public/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/app.imports.ts b/src/app.imports.ts index ce4ea85..b63e8c1 100644 --- a/src/app.imports.ts +++ b/src/app.imports.ts @@ -5,6 +5,7 @@ import { GraphQLModule } from '@nestjs/graphql'; import { JwtModule, JwtModuleOptions } from '@nestjs/jwt'; import { MongooseModule } from '@nestjs/mongoose'; import { MongooseModuleOptions } from '@nestjs/mongoose/dist/interfaces/mongoose-options.interface'; +import { ServeStaticModule } from '@nestjs/serve-static'; import crypto from 'crypto'; import { Request } from 'express-serve-static-core'; import { IncomingHttpHeaders } from 'http'; @@ -40,6 +41,12 @@ export const imports = [ timeout: 5000, maxRedirects: 10, }), + ServeStaticModule.forRoot({ + rootPath: join(__dirname, '..', 'public'), + exclude: [ + + ] + }), ConfigModule.forRoot({ load: [ () => { diff --git a/src/service/auth/auth.service.ts b/src/service/auth/auth.service.ts index baf7d0b..1c3a2d2 100644 --- a/src/service/auth/auth.service.ts +++ b/src/service/auth/auth.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; +import { PinoLogger } from 'nestjs-pino/dist'; import { AuthJwtModel } from '../../dto/auth/auth.jwt.model'; import { UserDocument } from '../../schema/user.schema'; import { UserService } from '../user/user.service'; @@ -11,17 +12,21 @@ export class AuthService { private userService: UserService, private jwtService: JwtService, private passwordService: PasswordService, + private logger: PinoLogger, ) {} async validateUser(username: string, password: string): Promise { - console.log('check user??', username) - // TODO only allow login for verified users! - const user = await this.userService.findByUsername(username); - if (user && await this.passwordService.verify(password, user.passwordHash, user.salt)) { - return user; + try { + const user = await this.userService.findByUsername(username); + if (user && await this.passwordService.verify(password, user.passwordHash, user.salt)) { + return user; + } + } catch (e) { + this.logger.error(`failed to verify user? ${e.message}`) } + return null; } diff --git a/src/service/user/boot.service.ts b/src/service/user/boot.service.ts index ee06e93..b441ba8 100644 --- a/src/service/user/boot.service.ts +++ b/src/service/user/boot.service.ts @@ -41,12 +41,15 @@ export class BootService implements OnApplicationBootstrap { return } catch (e) {} - await this.createService.create({ + const user = await this.createService.create({ username, email, password, }) + user.set('roles', ['user', 'admin', 'superuser']) + await user.save() + this.logger.info('new root user created') } } diff --git a/yarn.lock b/yarn.lock index aa078bf..f975323 100644 --- a/yarn.lock +++ b/yarn.lock @@ -768,6 +768,13 @@ "@angular-devkit/schematics" "9.0.6" fs-extra "8.1.0" +"@nestjs/serve-static@^2.1.1": + version "2.1.1" + resolved "https://registry.yarnpkg.com/@nestjs/serve-static/-/serve-static-2.1.1.tgz#b18573bb14a52fc9ebc82fc887d1bc0846f1d18c" + integrity sha512-hkrCm7R8BWE8k9iiFlRtBIw+/VR4SMWos3yErRvC12y1ADI7gWkbu46+urx8jlcMnOEQW/xTAWY51xe6miBvHQ== + dependencies: + path-to-regexp "0.1.7" + "@nestjs/testing@^7.0.0": version "7.0.9" resolved "https://registry.yarnpkg.com/@nestjs/testing/-/testing-7.0.9.tgz#c6b342dd34f060f18406bd7eb753edc5ecbb35c7"