feat(linked-accounts): support linking/unlinking emby accounts

This commit is contained in:
Michael Thomas
2024-08-01 11:44:28 -04:00
parent be07f5cf49
commit 538c46872d
4 changed files with 91 additions and 34 deletions

View File

@@ -358,8 +358,14 @@ userSettingsRoutes.delete<{ id: string }>(
'/linked-accounts/plex',
isOwnProfileOrAdmin(),
async (req, res, next) => {
const settings = getSettings();
const userRepository = getRepository(User);
// Make sure Plex login is enabled
if (settings.main.mediaServerType !== MediaServerType.PLEX) {
return res.status(500).json({ error: 'Plex login is disabled' });
}
try {
const user = await userRepository.findOne({
where: { id: Number(req.params.id) },
@@ -415,8 +421,11 @@ userSettingsRoutes.post<{ username: string; password: string }>(
return next({ status: 401, message: 'Unauthorized' });
}
// Make sure jellyfin login is enabled
if (settings.main.mediaServerType !== MediaServerType.JELLYFIN) {
return res.status(500).json({ error: 'Jellyfin login is disabled' });
if (
settings.main.mediaServerType !== MediaServerType.JELLYFIN &&
settings.main.mediaServerType !== MediaServerType.EMBY
) {
return res.status(500).json({ error: 'Jellyfin/Emby login is disabled' });
}
// Do not allow linking of an already linked account
@@ -426,8 +435,7 @@ userSettingsRoutes.post<{ username: string; password: string }>(
})
) {
return res.status(422).json({
error:
'The specified Jellyfin account is already linked to a Jellyseerr user',
error: 'The specified account is already linked to a Jellyseerr user',
});
}
@@ -462,15 +470,17 @@ userSettingsRoutes.post<{ username: string; password: string }>(
})
) {
return res.status(422).json({
error:
'The specified Jellyfin account is already linked to a Jellyseerr user',
error: 'The specified account is already linked to a Jellyseerr user',
});
}
const user = req.user;
// valid jellyfin user found, link to current user
user.userType = UserType.JELLYFIN;
user.userType =
settings.main.mediaServerType === MediaServerType.EMBY
? UserType.EMBY
: UserType.JELLYFIN;
user.jellyfinUserId = account.User.Id;
user.jellyfinUsername = account.User.Name;
user.jellyfinAuthToken = account.AccessToken;
@@ -479,7 +489,7 @@ userSettingsRoutes.post<{ username: string; password: string }>(
return res.status(204).send();
} catch (e) {
logger.error('Failed to link Jellyfin account to user.', {
logger.error('Failed to link account to user.', {
label: 'API',
ip: req.ip,
error: e,
@@ -500,8 +510,17 @@ userSettingsRoutes.delete<{ id: string }>(
'/linked-accounts/jellyfin',
isOwnProfileOrAdmin(),
async (req, res, next) => {
const settings = getSettings();
const userRepository = getRepository(User);
// Make sure jellyfin login is enabled
if (
settings.main.mediaServerType !== MediaServerType.JELLYFIN &&
settings.main.mediaServerType !== MediaServerType.EMBY
) {
return res.status(500).json({ error: 'Jellyfin/Emby login is disabled' });
}
try {
const user = await userRepository.findOne({
where: { id: Number(req.params.id) },