Nestjs how to redirect without an intermediate link

Viewed 3492

In my little application, I want to validate user email. For that, I send an email to the user with a link. The user click on the link and I would like to display a confirmation or not page to the user. For that I have used the redirect function like below :

  @Get('confirm/:username/:id/:token')
  @Redirect('127.0.0.1', 200)
  async confirmEmail(
    @Param('username') username: string,
    @Param('id') id: number,
    @Param('token') token: string,
  ) {
    this._logger.setMethod(this.confirmEmail.name);
    const url = await this._authService.confirmEmail(username, id, token);
    return { url: url };
  }

When I execute the rdirect function, the user show the following page :

OK. Redirecting to http://127.0.0.1:4200/register/registering-ok

He has to click on this link to display the confirmation page. Is it a way to have not this intermediary page and directly the final page?

Thanks for your comments.

1 Answers

You have to get instance of Response and call redirect on it

Here is example:

import { Response } from 'express';
import { Res } from '@nestjs/common';

...

  @Get('confirm/:username/:id/:token')
  async confirmEmail(
    @Param('username') username: string,
    @Param('id') id: number,
    @Param('token') token: string,
    @Res() res: Response
  ) {
    this._logger.setMethod(this.confirmEmail.name);
    const url = await this._authService.confirmEmail(username, id, token);
    res.redirect(url);
  }
Related