22 lines
716 B
TypeScript
22 lines
716 B
TypeScript
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
|
import { AuthService } from './auth.service.js';
|
|
|
|
export type AuthenticatedRequest = {
|
|
header: (name: string) => string | undefined;
|
|
auth: { userId: string; sessionId: string };
|
|
};
|
|
|
|
@Injectable()
|
|
export class AccessTokenGuard implements CanActivate {
|
|
public constructor(private readonly authService: AuthService) {}
|
|
public async canActivate(context: ExecutionContext) {
|
|
const request = context
|
|
.switchToHttp()
|
|
.getRequest<AuthenticatedRequest>();
|
|
request.auth = await this.authService.authenticate(
|
|
request.header('authorization'),
|
|
);
|
|
return true;
|
|
}
|
|
}
|