HM-115. Добавлена ручка для получения информации о себе по токену, ручка по редактированию инфы о себе по токену

This commit is contained in:
vigdorov
2020-08-08 09:15:01 +03:00
parent f79e581dbf
commit 2c08d7d8c7
4 changed files with 57 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import {
UserResponse,
CreateUserRequest,
UpdateUserRequest,
UpdateUserSelf,
} from './users.schema';
import { Request } from 'express';
import {
@ -48,7 +49,7 @@ export class UsersController {
return this.userService.findAll();
}
@Get(':login')
@Get('search/:login')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(FIND_ONE_SUCCESS)
@ApiResponse(FIND_ONE_NOT_FOUND)
@ -62,6 +63,22 @@ export class UsersController {
return await this.userService.findOne(request.params.login);
}
@Get('me')
@Header(...ALLOW_ORIGIN_ALL)
async findMe(@Req() request: Request): Promise<UserResponse> {
await this.authService.checkRequest(request);
return this.userService.findMe(request.headers.authorization);
}
@Post('edit-me')
@Header(...ALLOW_ORIGIN_ALL)
async findEdit(@Req() request: Request<null, UpdateUserSelf>): Promise<UserResponse> {
await this.authService.checkRequest(request);
return this.userService.updateSelf(request.headers.authorization, request.body);
}
@Post()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(CREATE_SUCCESS)
@ -107,7 +124,7 @@ export class UsersController {
}
@Options([
'', ':login'
'', 'search/:login', ':login', 'me', 'edit-me'
])
@Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)

View File

@ -21,6 +21,11 @@ export class UpdateUserRequest {
avatar: string;
}
export class UpdateUserSelf {
@ApiProperty()
avatar: string;
}
export class UserResponse {
@ApiProperty()
login: string;
@ -81,6 +86,9 @@ export class User extends Document {
required: true,
unique: true,
type: String,
minlength: 4,
lowercase: true,
validate: new RegExp(/^[a-z][a-z0-9_-]*$/)
})
login: string;

View File

@ -2,7 +2,7 @@ import {Model, Connection, Document} from 'mongoose';
import {Injectable, NotFoundException, BadGatewayException, ConflictException, BadRequestException} from '@nestjs/common';
import {InjectConnection} from '@nestjs/mongoose';
import {DB_NAME, USERS_CONTROLLER, SECRET_JWT_ACCESS_KEY, SECRET_JWT_REFRESH_KEY} from 'src/consts';
import {User, UserSchema, CreateUserRequest, UserResponse, UserModel, UpdateUserRequest, TokenResponse} from './users.schema';
import {User, UserSchema, CreateUserRequest, UserResponse, UserModel, UpdateUserRequest, TokenResponse, UpdateUserSelf} from './users.schema';
import * as bcrypt from 'bcrypt';
import * as jwt from 'jsonwebtoken';
@ -203,4 +203,17 @@ export class UserService {
const searchUser = await this.findUser(token.login);
return searchUser && this.checkToken(token, agent);
}
async findMe(access_token: string): Promise<UserResponse> {
const token = jwt.decode(access_token) as Token;
return await this.findOne(token.login);
}
async updateSelf(access_token: string, updateUser: UpdateUserSelf): Promise<UserResponse> {
const {login} = jwt.decode(access_token) as Token;
return await this.update({
...updateUser,
login,
});
}
}

View File

@ -2,14 +2,14 @@
GET http://localhost:4002/users HTTP/1.1
### Получить одного пользователя
GET http://localhost:4002/users/admin HTTP/1.1
GET http://localhost:4002/users/search/gfhHfgDHDU89 HTTP/1.1
### Создать пользователя
POST http://localhost:4002/users HTTP/1.1
content-type: application/json
{
"login": "string",
"login": "gfhHfgDHDU89",
"avatar": "string",
"password": "string"
}
@ -24,7 +24,7 @@ content-type: application/json
}
### Удалить пользователя
DELETE http://localhost:4002/users/string HTTP/1.1
DELETE http://localhost:4002/users/dfg_sstrwer HTTP/1.1
### Авторизоватся
POST http://localhost:4002/auth HTTP/1.1
@ -50,3 +50,16 @@ content-type: application/json
{
"access_token": "eyаJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6InN0cmluZyIsImhvc3QiOiJsb2NhbGhvc3Q6NDAwMiIsImFnZW50IjoidnNjb2RlLXJlc3RjbGllbnQiLCJpYXQiOjE1OTYyMzE1MzQsImV4cCI6MTU5NjIzMTY1NH0.muSl2TE2gQ78UxfaufO5SWszN5h0yYbPvR5_1PB-d2c"
}
###
GET http://localhost:4002/users/me HTTP/1.1
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6InN0cmluZyIsImFnZW50IjoidnNjb2RlLXJlc3RjbGllbnQiLCJpYXQiOjE1OTY4NjYxMDEsImV4cCI6MTU5Njg2NjEyMX0.Dz6wYzkHjC1LA2l3C1LfXeV1bAi3326qPnnBM06_bek
###
POST http://localhost:4002/users/edit-me HTTP/1.1
content-type: application/json
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6InN0cmluZyIsImFnZW50IjoidnNjb2RlLXJlc3RjbGllbnQiLCJpYXQiOjE1OTY4NjYxMDEsImV4cCI6MTU5Njg2NjEyMX0.Dz6wYzkHjC1LA2l3C1LfXeV1bAi3326qPnnBM06_bek
{
"avatar": "hui"
}