From 15e246929bdbc2b7b5bdab7a84bd7882b79d5cb1 Mon Sep 17 00:00:00 2001 From: Ryan Cohen Date: Sun, 20 Nov 2022 19:07:32 +0900 Subject: [PATCH] fix(api): handle auth for accounts where the plex id may have been set to null (#3125) also made some changes to hopefully alleviate this issue from happening at all in the future --- server/entity/User.ts | 4 ++-- server/routes/auth.ts | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/server/entity/User.ts b/server/entity/User.ts index 0c540fee..7dbdb31b 100644 --- a/server/entity/User.ts +++ b/server/entity/User.ts @@ -39,7 +39,7 @@ export class User { return users.map((u) => u.filter(showFiltered)); } - static readonly filteredFields: string[] = ['email']; + static readonly filteredFields: string[] = ['email', 'plexId']; public displayName: string; @@ -73,7 +73,7 @@ export class User { @Column({ type: 'integer', default: UserType.PLEX }) public userType: UserType; - @Column({ nullable: true, select: false }) + @Column({ nullable: true, select: true }) public plexId?: number; @Column({ nullable: true, select: false }) diff --git a/server/routes/auth.ts b/server/routes/auth.ts index cf4a4e86..cb6db87c 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -64,13 +64,28 @@ authRoutes.post('/plex', async (req, res, next) => { await userRepository.save(user); } else { const mainUser = await userRepository.findOneOrFail({ - select: { id: true, plexToken: true, plexId: true }, + select: { id: true, plexToken: true, plexId: true, email: true }, where: { id: 1 }, }); const mainPlexTv = new PlexTvAPI(mainUser.plexToken ?? ''); + if (!account.id) { + logger.error('Plex ID was missing from Plex.tv response', { + label: 'API', + ip: req.ip, + email: account.email, + plexUsername: account.username, + }); + + return next({ + status: 500, + message: 'Something went wrong. Try again.', + }); + } + if ( account.id === mainUser.plexId || + (account.email === mainUser.email && !mainUser.plexId) || (await mainPlexTv.checkUserAccess(account.id)) ) { if (user) {