From c6ab0471671e2c024d92886f72a2f49a5bc8b442 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Thu, 13 Jun 2024 11:42:07 -0400 Subject: [PATCH] fix(server): oauth linking error message (#10287) --- server/src/services/auth.service.spec.ts | 6 +++--- server/src/services/auth.service.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/server/src/services/auth.service.spec.ts b/server/src/services/auth.service.spec.ts index f9c3ed08cf..7aa03e6bdd 100644 --- a/server/src/services/auth.service.spec.ts +++ b/server/src/services/auth.service.spec.ts @@ -395,12 +395,12 @@ describe('AuthService', () => { userMock.getAdmin.mockResolvedValue(userStub.user1); userMock.create.mockResolvedValue(userStub.user1); - await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual( - loginResponseStub.user1oauth, + await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).rejects.toThrow( + BadRequestException, ); expect(userMock.update).not.toHaveBeenCalled(); - expect(userMock.create).toHaveBeenCalled(); + expect(userMock.create).not.toHaveBeenCalled(); }); it('should allow auto registering by default', async () => { diff --git a/server/src/services/auth.service.ts b/server/src/services/auth.service.ts index 0f2add4337..182933de1a 100644 --- a/server/src/services/auth.service.ts +++ b/server/src/services/auth.service.ts @@ -198,8 +198,12 @@ export class AuthService { // link existing user if (!user) { const emailUser = await this.userRepository.getByEmail(profile.email); - if (emailUser && !emailUser.oauthId) { - user = await this.userRepository.update(emailUser.id, { oauthId: profile.sub }); + if (emailUser) { + if (emailUser.oauthId) { + throw new BadRequestException('User already exists, but is linked to another account.'); + } else { + user = await this.userRepository.update(emailUser.id, { oauthId: profile.sub }); + } } }