Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat(user): add api declaration for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Jan 27, 2019
1 parent 370afb6 commit 4efed72
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 4 deletions.
1 change: 1 addition & 0 deletions back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start:back:prod": "pm2 start ./pm2.config.js"
},
"dependencies": {
"@breadhead/detil-ts": "^1.0.1",
"@nestjs/common": "^5.6.2",
"@nestjs/core": "^5.6.2",
"@nestjs/swagger": "^2.5.1",
Expand Down
8 changes: 6 additions & 2 deletions back/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'

@Module({ })
import { UserModule } from './user/user.module'

@Module({
imports: [UserModule],
})
export class AppModule implements NestModule {
public configure(consumer: MiddlewareConsumer) {
// pass
}
}
}
2 changes: 1 addition & 1 deletion back/src/config/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ const corsMiddleware = () => {

export default (app: INestApplication) => {
app.use(corsMiddleware())
}
}
2 changes: 1 addition & 1 deletion back/src/config/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export default (app: INestApplication, path: string) => {

const document = SwaggerModule.createDocument(app, options)
SwaggerModule.setup(path, app, document)
}
}
40 changes: 40 additions & 0 deletions back/src/user/presentation/http/controller/AuthController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Body, Controller, Post } from '@nestjs/common'
import {
ApiBadRequestResponse,
ApiCreatedResponse,
ApiOkResponse,
ApiOperation,
ApiUseTags,
} from '@nestjs/swagger'

import { AuthRequest } from '../request/AuthRequest'
import { TokenResponse } from '../response/TokenResponse'
import { PostNoCreate } from './annotations/PostNoCreate'

@Controller('user/auth')
@ApiUseTags('user')
export class AuthController {
@PostNoCreate('sign-in')
@ApiOperation({ title: 'Sign-in by email and password' })
@ApiOkResponse({ description: 'Valid credentials', type: TokenResponse })
@ApiBadRequestResponse({ description: 'Invalid credentials' })
public async signIn(@Body() request: AuthRequest): Promise<TokenResponse> {
const { email, password } = request

return {
token: email + password,
}
}

@Post('sign-up')
@ApiOperation({ title: 'Sign-up by email and password' })
@ApiCreatedResponse({ description: 'User created', type: TokenResponse })
@ApiBadRequestResponse({ description: 'User already exists' })
public async signUp(@Body() request: AuthRequest): Promise<TokenResponse> {
const { email, password } = request

return {
token: email + password,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ComposeMethodDecorators } from '@breadhead/detil-ts'
import { HttpCode, Post } from '@nestjs/common'

export const PostNoCreate = (path: string) =>
ComposeMethodDecorators([Post(path), HttpCode(200)])
9 changes: 9 additions & 0 deletions back/src/user/presentation/http/request/AuthRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiModelProperty } from '@nestjs/swagger'

export class AuthRequest {
@ApiModelProperty({ example: 'email@email.com' })
public readonly email: string

@ApiModelProperty({ example: 'Pas$w0rd' })
public readonly password: string
}
8 changes: 8 additions & 0 deletions back/src/user/presentation/http/response/TokenResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiModelProperty } from '@nestjs/swagger'

import { TokenModel } from '@shared/models/user/TokenModel'

export class TokenResponse implements TokenModel {
@ApiModelProperty({ example: 'token-string-with-signature' })
public readonly token: string
}
12 changes: 12 additions & 0 deletions back/src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'

import { AuthController } from './presentation/http/controller/AuthController'

@Module({
controllers: [AuthController],
})
export class UserModule implements NestModule {
public configure(consumer: MiddlewareConsumer) {
// pass
}
}
3 changes: 3 additions & 0 deletions shared/models/user/TokenModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface TokenModel {
readonly token: string
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,11 @@
lodash "^4.17.10"
to-fast-properties "^2.0.0"

"@breadhead/detil-ts@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@breadhead/detil-ts/-/detil-ts-1.0.1.tgz#8a7a4a242258ffe66d73f526e62d829ddbd2e870"
integrity sha512-7c3TbHj7npQeMO561o+aigkM7HKYZyOaOOQ+qMZbXR3gkfbAhtI7FXczcntbx51QEpOYlNtsJxiuFIKvETbojA==

"@commitlint/cli@^7.2.1":
version "7.2.1"
resolved "https://registry.yarnpkg.com/@commitlint/cli/-/cli-7.2.1.tgz#dbb9eeb1f5015a129bb0801fbc1115eb1dcd513b"
Expand Down

0 comments on commit 4efed72

Please sign in to comment.