title: NestJS - Redirect user from guard when check fails
body: <p>I've added some simple, working login functionality to a NestJS app I'm building. I now want to block access to certain routes for logged out users, so I've added a simple <code>AuthGuard</code>, which looks like this;</p>
  
  <pre><code>@Injectable()
  export class AuthGuard implements CanActivate {
      public canActivate(
          context: ExecutionContext,
      ): boolean {
          const request = context.switchToHttp().getRequest();
          const response = context.switchToHttp().getResponse();
          if (typeof request.session.authenticated !== "undefined" &amp;&amp; request.session.authenticated === true) {
              return true;
          }
          return false;
      }
  }
  </code></pre>
  
  <p>This works in the way it blocks the user from accessing routes the guard is applied to, but in doing so it just shows this JSON response;</p>
  
  <pre><code>{
      "statusCode": 403,
      "error": "Forbidden",
      "message": "Forbidden resource"
  }
  </code></pre>
  
  <p>What I want is the user to be redirected to the login page when the guard fails. I've tried replacing the <code>return false</code> with <code>response.redirect("/auth/login");</code> and although it works, in the console I get messages</p>
  
  <p><code>(node:11526) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Can't set headers after they are sent.</code></p>
  
  <p><code>(node:11526) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.</code></p>
  
  <p>So although it works, it doesn't seem to be the best solution.</p>
  
  <p>The route the guard is called on is as follows btw:</p>
  
  <pre><code>@Get()
  @UseGuards(AuthGuard)
  @Render("index")
  public index() {
      return {
          user: {},
      };
  }
  </code></pre>
  
  <p>Is it at all possible to properly redirect a user after the guard fails?</p>