class BearerToken extends Schema.String.annotations({
examples: [
'Bearer 01KB5EQD32KP4C52PFFWPZ4PYK'
]
}) {
static make(token: string) {
return `Bearer ${token}`
}
}
class AuthenticationMiddleware extends HttpApiMiddleware.Tag<AuthenticationMiddleware>()(
'AuthenticationMiddleware',
{
provides: CurrentUser,
failure: Schema.Union(UnauthorizedError, UnexpectedError),
security: {
token: HttpApiSecurity.bearer.pipe(
HttpApiSecurity.annotate(OpenApi.Description, 'bearer token')
)
}
}
) {}
const GetProfileEndpoint = HttpApiEndpoint.get('getProfile', '/auth/profile')
// even tried setting headers manually but that didn't work as expected because it seems like those same headers are expected in the response or something š¤¦āāļø
// .setHeaders(
// Schema.Struct({
// Authorization: BearerToken
// })
// )
.middleware(AuthenticationMiddleware)
.addSuccess(GetProfileSuccessResponse)
.addError(UnauthorizedError, { status: StatusCodes.UNAUTHORIZED })
.addError(UnexpectedError, { status: StatusCodes.INTERNAL_SERVER_ERROR })
class BearerToken extends Schema.String.annotations({
examples: [
'Bearer 01KB5EQD32KP4C52PFFWPZ4PYK'
]
}) {
static make(token: string) {
return `Bearer ${token}`
}
}
class AuthenticationMiddleware extends HttpApiMiddleware.Tag<AuthenticationMiddleware>()(
'AuthenticationMiddleware',
{
provides: CurrentUser,
failure: Schema.Union(UnauthorizedError, UnexpectedError),
security: {
token: HttpApiSecurity.bearer.pipe(
HttpApiSecurity.annotate(OpenApi.Description, 'bearer token')
)
}
}
) {}
const GetProfileEndpoint = HttpApiEndpoint.get('getProfile', '/auth/profile')
// even tried setting headers manually but that didn't work as expected because it seems like those same headers are expected in the response or something š¤¦āāļø
// .setHeaders(
// Schema.Struct({
// Authorization: BearerToken
// })
// )
.middleware(AuthenticationMiddleware)
.addSuccess(GetProfileSuccessResponse)
.addError(UnauthorizedError, { status: StatusCodes.UNAUTHORIZED })
.addError(UnexpectedError, { status: StatusCodes.INTERNAL_SERVER_ERROR })