Files
gitmost/apps/server/src/collaboration/extensions/authentication.extension.ts
Philipinho c18c9ae02b Refactoring
* replace TypeORM with Kysely query builder
* refactor migrations
* other changes and fixes
2024-03-29 01:46:11 +00:00

41 lines
1.1 KiB
TypeScript

import { Extension, onAuthenticatePayload } from '@hocuspocus/server';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { TokenService } from '../../core/auth/services/token.service';
import { UserRepo } from '@docmost/db/repos/user/user.repo';
@Injectable()
export class AuthenticationExtension implements Extension {
constructor(
private tokenService: TokenService,
private userRepo: UserRepo,
) {}
async onAuthenticate(data: onAuthenticatePayload) {
const { documentName, token } = data;
let jwtPayload = null;
try {
jwtPayload = await this.tokenService.verifyJwt(token);
} catch (error) {
throw new UnauthorizedException('Could not verify jwt token');
}
const userId = jwtPayload.sub;
const workspaceId = jwtPayload.workspaceId;
const user = await this.userRepo.findById(userId, workspaceId);
if (!user) {
throw new UnauthorizedException();
}
//TODO: Check if the page exists and verify user permissions for page.
// if all fails, abort connection
return {
user,
};
}
}