From 06e4920ee224b27bbcbdd1bda1adefb638a3e9a8 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 29 Jul 2025 11:57:57 +0530 Subject: [PATCH 1/2] fix: create email target when using email otp registration --- app/controllers/api/account.php | 26 +++++++++++++++++++++- tests/e2e/Services/Account/AccountBase.php | 6 ++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 6adbf61d6d..df17051d6d 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -2239,7 +2239,31 @@ App::post('/v1/account/tokens/email') ]); $user->removeAttribute('$sequence'); - Authorization::skip(fn () => $dbForProject->createDocument('users', $user)); + $user = Authorization::skip(fn () => $dbForProject->createDocument('users', $user)); + + try { + $target = Authorization::skip(fn () => $dbForProject->createDocument('targets', new Document([ + '$permissions' => [ + Permission::read(Role::user($user->getId())), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], + 'userId' => $user->getId(), + 'userInternalId' => $user->getSequence(), + 'providerType' => MESSAGE_TYPE_EMAIL, + 'identifier' => $email, + ]))); + $user->setAttribute('targets', [...$user->getAttribute('targets', []), $target]); + } catch (Duplicate) { + $existingTarget = $dbForProject->findOne('targets', [ + Query::equal('identifier', [$email]), + ]); + if (!$existingTarget->isEmpty()) { + $user->setAttribute('targets', $existingTarget, Document::SET_TYPE_APPEND); + } + } + + $dbForProject->purgeCachedDocument('users', $user->getId()); } $tokenSecret = Auth::codeGenerator(6); diff --git a/tests/e2e/Services/Account/AccountBase.php b/tests/e2e/Services/Account/AccountBase.php index 7c83edf3e3..8813e2784f 100644 --- a/tests/e2e/Services/Account/AccountBase.php +++ b/tests/e2e/Services/Account/AccountBase.php @@ -39,6 +39,8 @@ trait AccountBase $this->assertEquals($response['body']['labels'], []); $this->assertArrayHasKey('accessedAt', $response['body']); $this->assertNotEmpty($response['body']['accessedAt']); + $this->assertArrayHasKey('targets', $response['body']); + $this->assertEquals($email, $response['body']['targets'][0]['identifier']); /** * Test for FAILURE @@ -159,7 +161,7 @@ trait AccountBase 'email' => 'otpuser@appwrite.io' ]); - $this->assertEquals(201, $response['headers']['status-code'], ); + $this->assertEquals(201, $response['headers']['status-code']); $this->assertNotEmpty($response['body']['$id']); $this->assertNotEmpty($response['body']['$createdAt']); $this->assertNotEmpty($response['body']['userId']); @@ -209,6 +211,8 @@ trait AccountBase $this->assertEquals($userId, $response['body']['$id']); $this->assertEquals($userId, $response['body']['$id']); $this->assertTrue($response['body']['emailVerification']); + $this->assertArrayHasKey('targets', $response['body']); + $this->assertEquals('otpuser@appwrite.io', $response['body']['targets'][0]['identifier']); $response = $this->client->call(Client::METHOD_POST, '/account/sessions/token', array_merge([ 'origin' => 'http://localhost', From 7ad01fcadae3edaad48ca009b15cbe7e8e1ee1c8 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 29 Jul 2025 12:23:06 +0530 Subject: [PATCH 2/2] linebreak --- app/controllers/api/account.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index df17051d6d..61b71930eb 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -2240,7 +2240,6 @@ App::post('/v1/account/tokens/email') $user->removeAttribute('$sequence'); $user = Authorization::skip(fn () => $dbForProject->createDocument('users', $user)); - try { $target = Authorization::skip(fn () => $dbForProject->createDocument('targets', new Document([ '$permissions' => [