var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Sentry } from '@rlanz/sentry';
import { method } from '../observability/tracing.js';
import { authContext } from './auth_context/provider.js';
export default class AKAuthMiddleware {
    async handle(ctx, next, options = {}) {
        if (options.type)
            ctx.containerResolver.bindValue('auth.jwt.token_type', options.type);
        if (options.isAdmin)
            ctx.containerResolver.bindValue('auth.jwt.is_admin', options.isAdmin);
        const user = await ctx.auth.authenticateUsing(options.guards);
        if (user) {
            Sentry.setUser({
                id: user.id,
                email: user.email,
            });
            const span = Sentry.getActiveSpan();
            if (span) {
                const rootSpan = Sentry.getRootSpan(span);
                rootSpan.setAttribute('user.id', user.id);
                rootSpan.setAttribute('user.email', user.email);
            }
        }
        if (user)
            await authContext.runWithCurrentUser(user, next);
        else
            await next();
    }
}
__decorate([
    method(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Function, Function, Object]),
    __metadata("design:returntype", Promise)
], AKAuthMiddleware.prototype, "handle", null);
//# sourceMappingURL=auth_middleware.js.map