DTP Base provides a scalable and secure Node.js application development harness ready for production service.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

110 lines
2.7 KiB

// auth.js
// Copyright (C) 2024 DTP Technologies, LLC
// All Rights Reserved
'use strict';
import express from 'express';
import { SiteController, SiteError } from '../../lib/site-lib.js';
export default class ChatController extends SiteController {
static get name ( ) { return 'ChatController'; }
static get slug ( ) { return 'chat'; }
constructor (dtp) {
super(dtp, ChatController);
}
async start ( ) {
const {
// csrfToken: csrfTokenService,
// limiter: limiterService,
session: sessionService,
} = this.dtp.services;
const authRequired = sessionService.authCheckMiddleware({ requireLogin: true });
const router = express.Router();
this.dtp.app.use('/chat', router);
router.use(
async (req, res, next) => {
res.locals.currentView = 'auth';
return next();
},
authRequired,
);
router.param('roomId', this.populateRoomId.bind(this));
router.post(
'/room',
// limiterService.create(limiterService.config.chat.postCreateRoom),
this.postCreateRoom.bind(this),
);
router.get(
'/room/create',
this.getRoomCreateView.bind(this),
);
router.get(
'/room/:roomId/join',
// limiterService.create(limiterService.config.chat.getRoomView),
this.getRoomJoin.bind(this),
);
router.get(
'/room/:roomId',
// limiterService.create(limiterService.config.chat.getRoomView),
this.getRoomView.bind(this),
);
return router;
}
async populateRoomId (req, res, next, roomId) {
const { chat: chatService } = this.dtp.services;
try {
res.locals.room = await chatService.getRoomById(roomId);
if (!res.locals.room) {
throw new SiteError(404, "The chat room doesn't exist.");
}
return next();
} catch (error) {
return next(error);
}
}
async postCreateRoom (req, res, next) {
const { chat: chatService } = this.dtp.services;
try {
res.locals.room = await chatService.createRoom(req.user, req.body);
res.redirect(`/chat/room/${res.locals.room._id}`);
} catch (error) {
return next(error);
}
}
async getRoomCreateView (req, res) {
res.render('chat/room/create');
}
async getRoomJoin (req, res, next) {
const { chat: chatService } = this.dtp.services;
try {
await chatService.joinRoom(res.locals.room, req.user);
res.status(200).json({ success: true, room: res.locals.room });
} catch (error) {
this.log.error('failed to join chat room', { error });
return next(error);
}
}
async getRoomView (req, res) {
res.locals.currentView = 'chat-room';
res.render('chat/room/view');
}
}