mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-18 18:51:05 +10:00
35 lines
986 B
TypeScript
35 lines
986 B
TypeScript
import { Extension, onAuthenticatePayload } from '@hocuspocus/server';
|
|
import { UserService } from '../../core/user/user.service';
|
|
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { TokenService } from '../../core/auth/services/token.service';
|
|
|
|
@Injectable()
|
|
export class AuthenticationExtension implements Extension {
|
|
constructor(
|
|
private tokenService: TokenService,
|
|
private userService: UserService,
|
|
) {}
|
|
|
|
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 user = await this.userService.findById(userId);
|
|
|
|
//TODO: Check if the page exists and verify user permissions for page.
|
|
// if all fails, abort connection
|
|
|
|
return {
|
|
user,
|
|
};
|
|
}
|
|
}
|