> For the complete documentation index, see [llms.txt](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/extra-module-1-authentication-authorization/fix-recover-route.md).

# Fix - Recover route

This route became faulty due to recent code changes, let's now fix it.

With the new features we implemented in this module, the route to recover a user stopped working. This is because we are now checking who is trying to perform this operation in order to allow them or not. Well, if the account has been deleted, how can the user identify himself? Let's then fix this route.

Now, in order to recover an account, an <mark style="color:blue;">`email`</mark> and <mark style="color:blue;">`password`</mark> shall be sent. We can then use the <mark style="color:blue;">`CredentialsDto`</mark>. In the <mark style="color:blue;">`UsersController`</mark>, let's alter the <mark style="color:blue;">`recover()`</mark> route by:

* Making it use **only** this DTO
* Making it public
* Altering its path

```typescript
@Public()
@Patch('recover')
recover(@Body() credentialsDto: CredentialsDto) {
  return this.usersService.recover(credentialsDto);
}
```

And this would be the final form of the <mark style="color:blue;">`recover()`</mark> method in the service. Note that it became very similar to the <mark style="color:blue;">`validateLocal()`</mark> method.

```typescript
async recover(credentialsDto: CredentialsDto) {
  const { email, password } = credentialsDto;

  const user = await this.usersRepository.findOne({
    where: { email },
    relations: {
      orders: {
        items: true,
        payment: true,
      },
    },
    withDeleted: true,
  });
  if (!user) {
    throw new UnauthorizedException('Invalid email');
  }

  const isMatch = await this.hashingService.compare(password, user.password);
  if (!isMatch) {
    throw new UnauthorizedException('Invalid password');
  }

  if (!user.isDeleted) {
    throw new ConflictException('User not deleted');
  }

  return this.usersRepository.recover(user);
}
```

However, we now have a problem. This route became inaccessible due to the new path. It is now understood that <mark style="color:blue;">"recover"</mark> is the <mark style="color:blue;">`id`</mark> to be used in order to update a <mark style="color:blue;">`user`</mark>. We could simply put it above the <mark style="color:blue;">`update()`</mark> route to fix this, but there is a better way.

In the main file, when creating the app, we may define options after passing the <mark style="color:blue;">`AppModule`</mark>. One of them is <mark style="color:blue;">`routeConflictPolicy`</mark>, which may show <mark style="color:yellow;">warnings</mark> or throw <mark style="color:red;">errors</mark> in these cases:

* <mark style="color:blue;">`duplicate`</mark>: two routes share the same "signature" (method, path, host, and version)
* <mark style="color:blue;">`shadow`</mark>: two routes risk matching the same request, e.g. <mark style="color:blue;">/:id</mark> and <mark style="color:blue;">/me</mark>

We may then define that **duplicates** should throw an error, and **shadows** should show a warning. With this, we'll see a warning for our route during application bootstrap.

```typescript
routeConflictPolicy: { duplicate: 'error', shadow: 'warn' },
```

Another option, maybe even more interesting, is <mark style="color:blue;">`routeResolutionStrategy`</mark>. With it, we may define the routes' registration order. it offers two choices:

* <mark style="color:blue;">`declaration`</mark>: routes are registered in the order they are defined (**default**)
* <mark style="color:blue;">`specificity`</mark>: more specific routes are registered first

We may then use this option to avoid this problem altogether!

```typescript
routeResolutionStrategy: 'specificity',
```

<mark style="color:green;">**Commit**</mark> - Fixing recover route and auto-sorting routes
