refactor: use authService in backend
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
require("dotenv").config();
|
||||
const authService = require("../services/auth/JwtService");
|
||||
const authService = require("../services/auth/AuthService");
|
||||
|
||||
module.exports = {
|
||||
adminLogin(req, res) {
|
||||
|
||||
@@ -1,64 +1,77 @@
|
||||
const ChannelService = require('../services/ChannelService');
|
||||
require('dotenv').config();
|
||||
|
||||
const ADMIN_ENABLED = process.env.ADMIN_ENABLED === 'true';
|
||||
const ChannelService = require("../services/ChannelService");
|
||||
const authService = require("../services/auth/AuthService");
|
||||
|
||||
module.exports = (io, socket) => {
|
||||
// Check if admin mode is required for channel modifications
|
||||
socket.on('add-channel', ({ name, url, avatar, mode, headersJson }) => {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (ADMIN_ENABLED && !socket.user?.isAdmin) {
|
||||
return socket.emit('app-error', { message: 'Admin access required to add channels' });
|
||||
}
|
||||
// Check if admin mode is required for channel modifications
|
||||
socket.on("add-channel", ({ name, url, avatar, mode, headersJson }) => {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (authService.isAdminEnabled() && !socket.user?.isAdmin) {
|
||||
return socket.emit("app-error", {
|
||||
message: "Admin access required to add channels",
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Adding solo channel:', url);
|
||||
const newChannel = ChannelService.addChannel({ name: name, url: url, avatar: avatar, mode: mode, headersJson: headersJson });
|
||||
io.emit('channel-added', newChannel); // Broadcast to all clients
|
||||
} catch (err) {
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
});
|
||||
console.log("Adding solo channel:", url);
|
||||
const newChannel = ChannelService.addChannel({
|
||||
name: name,
|
||||
url: url,
|
||||
avatar: avatar,
|
||||
mode: mode,
|
||||
headersJson: headersJson,
|
||||
});
|
||||
io.emit("channel-added", newChannel); // Broadcast to all clients
|
||||
} catch (err) {
|
||||
socket.emit("app-error", { message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('set-current-channel', async (id) => {
|
||||
try {
|
||||
const nextChannel = await ChannelService.setCurrentChannel(id);
|
||||
io.emit('channel-selected', nextChannel); // Broadcast to all clients
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
});
|
||||
socket.on("set-current-channel", async (id) => {
|
||||
try {
|
||||
const nextChannel = await ChannelService.setCurrentChannel(id);
|
||||
io.emit("channel-selected", nextChannel); // Broadcast to all clients
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit("app-error", { message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('delete-channel', async (id) => {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (ADMIN_ENABLED && !socket.user?.isAdmin) {
|
||||
return socket.emit('app-error', { message: 'Admin access required to delete channels' });
|
||||
}
|
||||
socket.on("delete-channel", async (id) => {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (authService.isAdminEnabled() && !socket.user?.isAdmin) {
|
||||
return socket.emit("app-error", {
|
||||
message: "Admin access required to delete channels",
|
||||
});
|
||||
}
|
||||
|
||||
const lastChannel = ChannelService.getCurrentChannel();
|
||||
const current = await ChannelService.deleteChannel(id);
|
||||
io.emit('channel-deleted', id); // Broadcast to all clients
|
||||
if(lastChannel.id != current.id) io.emit('channel-selected', current);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
});
|
||||
const lastChannel = ChannelService.getCurrentChannel();
|
||||
const current = await ChannelService.deleteChannel(id);
|
||||
io.emit("channel-deleted", id); // Broadcast to all clients
|
||||
if (lastChannel.id != current.id) io.emit("channel-selected", current);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit("app-error", { message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('update-channel', async ({ id, updatedAttributes }) => {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (ADMIN_ENABLED && !socket.user?.isAdmin) {
|
||||
return socket.emit('app-error', { message: 'Admin access required to update channels' });
|
||||
}
|
||||
socket.on("update-channel", async ({ id, updatedAttributes }) => {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (authService.isAdminEnabled() && !socket.user?.isAdmin) {
|
||||
return socket.emit("app-error", {
|
||||
message: "Admin access required to update channels",
|
||||
});
|
||||
}
|
||||
|
||||
const updatedChannel = await ChannelService.updateChannel(id, updatedAttributes);
|
||||
io.emit('channel-updated', updatedChannel); // Broadcast to all clients
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
});
|
||||
};
|
||||
const updatedChannel = await ChannelService.updateChannel(
|
||||
id,
|
||||
updatedAttributes
|
||||
);
|
||||
io.emit("channel-updated", updatedChannel); // Broadcast to all clients
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit("app-error", { message: err.message });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,91 +1,126 @@
|
||||
const PlaylistService = require('../services/PlaylistService');
|
||||
const ChannelService = require('../services/ChannelService');
|
||||
const PlaylistUpdater = require('../services/PlaylistUpdater');
|
||||
const Playlist = require('../models/Playlist');
|
||||
require('dotenv').config();
|
||||
const PlaylistService = require("../services/PlaylistService");
|
||||
const ChannelService = require("../services/ChannelService");
|
||||
const PlaylistUpdater = require("../services/PlaylistUpdater");
|
||||
const Playlist = require("../models/Playlist");
|
||||
const authService = require("../services/auth/AuthService");
|
||||
require("dotenv").config();
|
||||
|
||||
const ADMIN_ENABLED = process.env.ADMIN_ENABLED === 'true';
|
||||
const ADMIN_ENABLED = process.env.ADMIN_ENABLED === "true";
|
||||
|
||||
async function handleAddPlaylist({ playlist, playlistName, mode, playlistUpdate, headers }, io, socket) {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (ADMIN_ENABLED && !socket.user?.isAdmin) {
|
||||
return socket.emit('app-error', { message: 'Admin access required to add playlists' });
|
||||
}
|
||||
|
||||
const channels = await PlaylistService.addPlaylist(playlist, playlistName, mode, playlistUpdate, headers);
|
||||
|
||||
if (channels) {
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-added', channel);
|
||||
});
|
||||
}
|
||||
|
||||
if(playlistUpdate) {
|
||||
PlaylistUpdater.register(new Playlist(playlist, playlistName, mode, playlistUpdate, headers));
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
async function handleAddPlaylist(
|
||||
{ playlist, playlistName, mode, playlistUpdate, headers },
|
||||
io,
|
||||
socket
|
||||
) {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (authService.isAdminEnabled() && !socket.user?.isAdmin) {
|
||||
return socket.emit("app-error", {
|
||||
message: "Admin access required to add playlists",
|
||||
});
|
||||
}
|
||||
|
||||
const channels = await PlaylistService.addPlaylist(
|
||||
playlist,
|
||||
playlistName,
|
||||
mode,
|
||||
playlistUpdate,
|
||||
headers
|
||||
);
|
||||
|
||||
if (channels) {
|
||||
channels.forEach((channel) => {
|
||||
io.emit("channel-added", channel);
|
||||
});
|
||||
}
|
||||
|
||||
if (playlistUpdate) {
|
||||
PlaylistUpdater.register(
|
||||
new Playlist(playlist, playlistName, mode, playlistUpdate, headers)
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit("app-error", { message: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
async function handleUpdatePlaylist({ playlist, updatedAttributes }, io, socket) {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (ADMIN_ENABLED && !socket.user?.isAdmin) {
|
||||
return socket.emit('app-error', { message: 'Admin access required to update playlists' });
|
||||
}
|
||||
|
||||
if (playlist !== updatedAttributes.playlist) {
|
||||
// Playlist URL has changed - delete channels and fetch again
|
||||
await handleDeletePlaylist({ playlist }, io, socket);
|
||||
await handleAddPlaylist({ ...updatedAttributes }, io, socket);
|
||||
return;
|
||||
}
|
||||
|
||||
const channels = await PlaylistService.updatePlaylist(playlist, updatedAttributes);
|
||||
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-updated', channel);
|
||||
});
|
||||
|
||||
PlaylistUpdater.delete(playlist);
|
||||
if(updatedAttributes.playlistUpdate) {
|
||||
PlaylistUpdater.register(new Playlist(playlist, updatedAttributes.playlistName, updatedAttributes.mode, updatedAttributes.playlistUpdate, updatedAttributes.headers));
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
async function handleUpdatePlaylist(
|
||||
{ playlist, updatedAttributes },
|
||||
io,
|
||||
socket
|
||||
) {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (authService.isAdminEnabled() && !socket.user?.isAdmin) {
|
||||
return socket.emit("app-error", {
|
||||
message: "Admin access required to update playlists",
|
||||
});
|
||||
}
|
||||
|
||||
if (playlist !== updatedAttributes.playlist) {
|
||||
// Playlist URL has changed - delete channels and fetch again
|
||||
await handleDeletePlaylist({ playlist }, io, socket);
|
||||
await handleAddPlaylist({ ...updatedAttributes }, io, socket);
|
||||
return;
|
||||
}
|
||||
|
||||
const channels = await PlaylistService.updatePlaylist(
|
||||
playlist,
|
||||
updatedAttributes
|
||||
);
|
||||
|
||||
channels.forEach((channel) => {
|
||||
io.emit("channel-updated", channel);
|
||||
});
|
||||
|
||||
PlaylistUpdater.delete(playlist);
|
||||
if (updatedAttributes.playlistUpdate) {
|
||||
PlaylistUpdater.register(
|
||||
new Playlist(
|
||||
playlist,
|
||||
updatedAttributes.playlistName,
|
||||
updatedAttributes.mode,
|
||||
updatedAttributes.playlistUpdate,
|
||||
updatedAttributes.headers
|
||||
)
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit("app-error", { message: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDeletePlaylist({ playlist }, io, socket) {
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (ADMIN_ENABLED && !socket.user?.isAdmin) {
|
||||
return socket.emit('app-error', { message: 'Admin access required to delete playlists' });
|
||||
}
|
||||
|
||||
const channels = await PlaylistService.deletePlaylist(playlist);
|
||||
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-deleted', channel.id);
|
||||
});
|
||||
io.emit('channel-selected', ChannelService.getCurrentChannel());
|
||||
|
||||
PlaylistUpdater.delete(playlist);
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
try {
|
||||
// Check if user is authenticated as admin from the socket middleware
|
||||
if (authService.isAdminEnabled() && !socket.user?.isAdmin) {
|
||||
return socket.emit("app-error", {
|
||||
message: "Admin access required to delete playlists",
|
||||
});
|
||||
}
|
||||
|
||||
const channels = await PlaylistService.deletePlaylist(playlist);
|
||||
|
||||
channels.forEach((channel) => {
|
||||
io.emit("channel-deleted", channel.id);
|
||||
});
|
||||
io.emit("channel-selected", ChannelService.getCurrentChannel());
|
||||
|
||||
PlaylistUpdater.delete(playlist);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit("app-error", { message: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (io, socket) => {
|
||||
socket.on('add-playlist', data => handleAddPlaylist(data, io, socket));
|
||||
socket.on('update-playlist', data => handleUpdatePlaylist(data, io, socket));
|
||||
socket.on('delete-playlist', playlist => handleDeletePlaylist({ playlist }, io, socket));
|
||||
};
|
||||
socket.on("add-playlist", (data) => handleAddPlaylist(data, io, socket));
|
||||
socket.on("update-playlist", (data) =>
|
||||
handleUpdatePlaylist(data, io, socket)
|
||||
);
|
||||
socket.on("delete-playlist", (playlist) =>
|
||||
handleDeletePlaylist({ playlist }, io, socket)
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const authService = require("../../services/auth/JwtService");
|
||||
const authService = require("../../services/auth/AuthService");
|
||||
|
||||
/**
|
||||
* Socket.io middleware to authenticate users via JWT token
|
||||
|
||||
Reference in New Issue
Block a user