Skip to content

Commit 89c2cc2

Browse files
gauthier-thbonswouar
authored andcommitted
fix: resolve error when setup on second attempt (#1061)
1 parent 965cbf8 commit 89c2cc2

File tree

1 file changed

+68
-40
lines changed

1 file changed

+68
-40
lines changed

server/routes/auth.ts

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -299,54 +299,84 @@ authRoutes.post('/jellyfin', async (req, res, next) => {
299299
where: { jellyfinUserId: account.User.Id },
300300
});
301301

302-
if (!user && !(await userRepository.count())) {
302+
const missingAdminUser = !user && !(await userRepository.count());
303+
if (
304+
missingAdminUser ||
305+
settings.main.mediaServerType === MediaServerType.NOT_CONFIGURED
306+
) {
303307
// Check if user is admin on jellyfin
304308
if (account.User.Policy.IsAdministrator === false) {
305309
throw new ApiError(403, ApiErrorCode.NotAdmin);
306310
}
307311

308-
logger.info(
309-
'Sign-in attempt from Jellyfin user with access to the media server; creating initial admin user for Overseerr',
310-
{
311-
label: 'API',
312-
ip: req.ip,
313-
jellyfinUsername: account.User.Name,
314-
}
315-
);
312+
if (
313+
body.serverType !== MediaServerType.JELLYFIN &&
314+
body.serverType !== MediaServerType.EMBY
315+
) {
316+
throw new Error('select_server_type');
317+
}
318+
settings.main.mediaServerType = body.serverType;
316319

317-
// User doesn't exist, and there are no users in the database, we'll create the user
318-
// with admin permissions
319-
switch (body.serverType) {
320-
case MediaServerType.EMBY:
321-
settings.main.mediaServerType = MediaServerType.EMBY;
322-
user = new User({
323-
email: body.email || account.User.Name,
320+
if (missingAdminUser) {
321+
logger.info(
322+
'Sign-in attempt from Jellyfin user with access to the media server; creating initial admin user for Jellyseerr',
323+
{
324+
label: 'API',
325+
ip: req.ip,
324326
jellyfinUsername: account.User.Name,
325-
jellyfinUserId: account.User.Id,
326-
jellyfinDeviceId: deviceId,
327-
jellyfinAuthToken: account.AccessToken,
328-
permissions: Permission.ADMIN,
329-
avatar: `/avatarproxy/${account.User.Id}`,
330-
userType: UserType.EMBY,
331-
});
327+
}
328+
);
332329

333-
break;
334-
case MediaServerType.JELLYFIN:
335-
settings.main.mediaServerType = MediaServerType.JELLYFIN;
336-
user = new User({
337-
email: body.email || account.User.Name,
330+
// User doesn't exist, and there are no users in the database, we'll create the user
331+
// with admin permissions
332+
333+
user = new User({
334+
id: 1,
335+
email: body.email || account.User.Name,
336+
jellyfinUsername: account.User.Name,
337+
jellyfinUserId: account.User.Id,
338+
jellyfinDeviceId: deviceId,
339+
jellyfinAuthToken: account.AccessToken,
340+
permissions: Permission.ADMIN,
341+
avatar: `/avatarproxy/${account.User.Id}`,
342+
userType:
343+
body.serverType === MediaServerType.JELLYFIN
344+
? UserType.JELLYFIN
345+
: UserType.EMBY,
346+
});
347+
348+
await userRepository.save(user);
349+
} else {
350+
logger.info(
351+
'Sign-in attempt from Jellyfin user with access to the media server; editing admin user for Jellyseerr',
352+
{
353+
label: 'API',
354+
ip: req.ip,
338355
jellyfinUsername: account.User.Name,
339-
jellyfinUserId: account.User.Id,
340-
jellyfinDeviceId: deviceId,
341-
jellyfinAuthToken: account.AccessToken,
342-
permissions: Permission.ADMIN,
343-
avatar: `/avatarproxy/${account.User.Id}`,
344-
userType: UserType.JELLYFIN,
345-
});
356+
}
357+
);
358+
359+
// User alread exist but settings.json is not configured, we'll edit the admin user
360+
361+
user = await userRepository.findOne({
362+
where: { id: 1 },
363+
});
364+
if (!user) {
365+
throw new Error('Unable to find admin user to edit');
366+
}
367+
user.email = body.email || account.User.Name;
368+
user.jellyfinUsername = account.User.Name;
369+
user.jellyfinUserId = account.User.Id;
370+
user.jellyfinDeviceId = deviceId;
371+
user.jellyfinAuthToken = account.AccessToken;
372+
user.permissions = Permission.ADMIN;
373+
user.avatar = `/avatarproxy/${account.User.Id}`;
374+
user.userType =
375+
body.serverType === MediaServerType.JELLYFIN
376+
? UserType.JELLYFIN
377+
: UserType.EMBY;
346378

347-
break;
348-
default:
349-
throw new Error('select_server_type');
379+
await userRepository.save(user);
350380
}
351381

352382
// Create an API key on Jellyfin from this admin user
@@ -368,8 +398,6 @@ authRoutes.post('/jellyfin', async (req, res, next) => {
368398
settings.jellyfin.apiKey = apiKey;
369399
await settings.save();
370400
startJobs();
371-
372-
await userRepository.save(user);
373401
}
374402
// User already exists, let's update their information
375403
else if (account.User.Id === user?.jellyfinUserId) {

0 commit comments

Comments
 (0)