Files
docmost_custom/server/src/core/user/user.controller.ts
T

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-08-04 16:26:43 +01:00
import {
Controller,
Get,
2023-08-05 16:58:34 +01:00
UseGuards,
HttpCode,
HttpStatus,
2023-08-05 18:36:35 +01:00
Req,
UnauthorizedException,
2023-08-04 16:26:43 +01:00
} from '@nestjs/common';
import { UserService } from './user.service';
2023-08-05 16:58:34 +01:00
import { JwtGuard } from '../auth/guards/JwtGuard';
import { FastifyRequest } from 'fastify';
import { User } from './entities/user.entity';
2023-08-26 23:38:14 +01:00
import { Workspace } from '../workspace/entities/workspace.entity';
2023-08-04 16:26:43 +01:00
2023-08-05 16:58:34 +01:00
@UseGuards(JwtGuard)
2023-08-04 16:26:43 +01:00
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
2023-08-05 16:58:34 +01:00
@HttpCode(HttpStatus.OK)
@Get('me')
async getUser(@Req() req: FastifyRequest) {
const jwtPayload = req['user'];
const user: User = await this.userService.findById(jwtPayload.sub);
2023-08-04 16:26:43 +01:00
2023-08-05 16:58:34 +01:00
if (!user) {
throw new UnauthorizedException('Invalid user');
}
2023-08-04 16:26:43 +01:00
2023-08-05 16:58:34 +01:00
return { user };
2023-08-04 16:26:43 +01:00
}
2023-08-26 23:38:14 +01:00
@HttpCode(HttpStatus.OK)
@Get('info')
async getUserInfo(@Req() req: FastifyRequest) {
const jwtPayload = req['user'];
const data: { workspace: Workspace; user: User } =
await this.userService.getUserInstance(jwtPayload.sub);
return data;
}
2023-08-04 16:26:43 +01:00
}