# Override Authentication deprecated

The annotation uses the to check the authentication strategy.

To customize this behavior, the right way is to override the default AuthenticatedMiddleware then implement directly your authentication strategy (with passport.js for example).

WARNING

This usage is deprecated in favor of . See Authentication page.

# Use case

import {Controller, Get, Authenticated} from "@tsed/common";

@Controller("/mypath")
class MyCtrl {
  @Get("/")
  @Authenticated({role: "admin"})
  public getResource(){}
}
1
2
3
4
5
6
7
8

# Example

import {Unauthorized} from "@tsed/exceptions";
import {IMiddleware, EndpointInfo, Req, Middleware} from "@tsed/common";

@Middleware()
export class AuthenticatedMiddleware implements IMiddleware {
  public use(@Req() request: Req, @EndpointInfo() endpoint: EndpointInfo) {
    const options = endpoint.get(AuthenticatedMiddleware) || {};
    // @ts-ignore
    if (!request.isAuthenticated(options)) {
      throw new Unauthorized("Unauthorized");
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

TIP

By default, the server imports automatically your middlewares matching with this rule ${rootDir}/middlewares/**/*.ts (See componentScan configuration).

.
├── src
│   ├── controllers
│   ├── services
│   ├── middlewares
│   └── Server.ts
└── package.json
1
2
3
4
5
6
7

If not, just import your middleware in your server or edit the componentScan configuration.

import {Configuration} from "@tsed/common";
import "./src/other/directory/MyAuthMiddleware";

@Configuration({
    ...
})
export class Server {
}
1
2
3
4
5
6
7
8