@nestjs/common#Response TypeScript Examples

The following examples show how to use @nestjs/common#Response. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: auth.controller.ts    From Cromwell with MIT License 6 votes vote down vote up
@Post('login')
    @UseGuards(ThrottlerGuard)
    @Throttle(10, 30)
    @ApiOperation({
        description: 'Authenticates a human user via cookies.',
    })
    @ApiBody({ type: LoginDto })
    @ApiResponse({
        status: 200,
        type: UserDto
    })
    async login(@Request() req: TRequestWithUser, @Response() response: FastifyReply, @Body() input: LoginDto) {

        let authInfo: TLoginInfo = null;
        try {
            authInfo = await this.authService.logIn(input);
        } catch (error) {
            logger.error(error);
        }

        if (!authInfo) {
            response.status(403);
            response.send({ message: 'Login failed', statusCode: 403 });
            return;
        }

        req.user = authInfo.userInfo;

        if (authInfo.refreshToken && authInfo.accessToken) {
            this.authService.setAccessTokenCookie(response, req,
                this.authService.getAccessTokenInfo(authInfo.accessToken));

            this.authService.setRefreshTokenCookie(response, req,
                this.authService.getRefreshTokenInfo(authInfo.refreshToken));
        }
        response.code(200).send(authInfo.userDto);
    }
Example #2
Source File: auth.controller.ts    From Cromwell with MIT License 6 votes vote down vote up
@UseGuards(JwtAuthGuard)
    @Post('log-out')
    @ApiOperation({
        description: 'Logs user out who was logged via cookies',
    })
    async logOut(@Request() request: TRequestWithUser, @Response() response: FastifyReply) {
        if (request.user) {
            try {
                await this.authService.removeRefreshTokens(request.user);
            } catch (error) {
                logger.error(error);
            }
        }

        this.authService.clearTokenCookies(response, request);
        response.code(200).send(true);
    }
Example #3
Source File: cms.controller.ts    From Cromwell with MIT License 6 votes vote down vote up
@Get('download-public-file')
    @UseGuards(JwtAuthGuard)
    @Roles('administrator', 'guest', 'author')
    @ApiOperation({
        description: 'Downloads a file from specified subfolder of "public" files',
        parameters: [
            { name: 'inPath', in: 'query' },
            { name: 'fileName', in: 'query' }]
    })
    @ApiResponse({
        status: 200,
    })
    @ApiForbiddenResponse({ description: 'Forbidden.' })
    async downloadFile(@Query('inPath') inPath: string, @Query('fileName') fileName: string,
        @Response() response: FastifyReply) {
        logger.log('CmsController::downloadFile');
        return this.cmsService.downloadFile(response, inPath, fileName);
    }
Example #4
Source File: cms.controller.ts    From Cromwell with MIT License 6 votes vote down vote up
@Post('export-db')
    @UseGuards(JwtAuthGuard)
    @Roles('administrator', 'guest')
    @ApiOperation({
        description: `Exports DB into Excel file`,
    })
    @ApiBody({ type: ExportOptionsDto })
    @ApiResponse({
        status: 200,
    })
    async exportDB(@Body() input: ExportOptionsDto, @Response() response: FastifyReply) {
        if (!Array.isArray(input.tables)) input.tables = [];

        try {
            const file = await this.migrationService.exportDB(input.tables as any);
            response.type('text/html').send(file);
        } catch (error) {
            logger.error(error);
            response.code(500).send({ message: error + '' });
        }
    }
Example #5
Source File: social-auth.controller.ts    From nestjs-angular-starter with MIT License 6 votes vote down vote up
@Get(':provider')
  async socialLogin(
    @Param('provider') provider: string,
    @Request() req?: AppRequest,
    @Response() res?: AppResponse,
  ): Promise<LoginResponse> {
    let user: UserProfile;

    // If this is not unit testing and we have obtained a request
    if (req) {
      // Wait for the passport middleware to run
      await middlewareToPromise(
        passport.authenticate(`${provider}-token`, { session: false }),
        req,
        res,
      ); // Authenticate using the provider suitable (google-token, facebook-token)

      // Now handle the user this middleware obtained
      user = req.user;
    }

    const token = this.authService.generateToken(user);
    // Because we injected the request here, we must return the JSON because NestJS expects us to handle the request
    res.json({ token, user });

    return {
      token,
      user,
    };
  }
Example #6
Source File: healthz.controller.ts    From NextJS-NestJS-GraphQL-Starter with MIT License 6 votes vote down vote up
@Get('/')
  healthCheck(@Response() res) {
    if (this.connection.readyState === 1) {
      res.status(HttpStatus.OK).json({ db: { status: 'up' } });
    } else {
      res
        .status(HttpStatus.INTERNAL_SERVER_ERROR)
        .json({ db: { status: 'down' } });
    }
  }
Example #7
Source File: auth.controller.ts    From radiopanel with GNU General Public License v3.0 5 votes vote down vote up
@Post('/login/:authMethodUuid')
	public async dynamicLogin(@Request() req, @Response() res): Promise<any> {
		res.cookie('loggedIn', true);
		return res.redirect('/');
	}
Example #8
Source File: auth.controller.ts    From radiopanel with GNU General Public License v3.0 5 votes vote down vote up
@Get('/login/:authMethodUuid')
	public async dynamicLoginReturn(@Request() req, @Response() res): Promise<any> {
		res.cookie('loggedIn', true);
		return res.redirect('/');
	}