diff --git a/app/config/locale/templates/email-session-alert.tpl b/app/config/locale/templates/email-session-alert.tpl index 5c182a7df5..9855175b6f 100644 --- a/app/config/locale/templates/email-session-alert.tpl +++ b/app/config/locale/templates/email-session-alert.tpl @@ -3,9 +3,9 @@

{{body}}

    -
  1. {{device}}
  2. -
  3. {{ipAddress}}
  4. -
  5. {{country}}
  6. +
  7. {{listDevice}}
  8. +
  9. {{listIpAddress}}
  10. +
  11. {{listCountry}}

{{footer}}

diff --git a/app/config/locale/translations/en.json b/app/config/locale/translations/en.json index 7cc7fc8485..bd922ef754 100644 --- a/app/config/locale/translations/en.json +++ b/app/config/locale/translations/en.json @@ -21,9 +21,9 @@ "emails.sessionAlert.subject": "New session alert for {{project}}", "emails.sessionAlert.hello":"Hello {{user}}", "emails.sessionAlert.body": "We're writing to inform you that a new session has been initiated on your {{b}}{{project}}{{/b}} account, on {{b}}{{dateTime}}{{/b}}. \nHere are the details of the new session: ", - "emails.sessionAlert.device": "Device: {{b}}{{agentDevice}}{{/b}}", - "emails.sessionAlert.ipAddress": "IP Address: {{b}}{{ipAddress}}{{/b}}", - "emails.sessionAlert.country": "Country: {{b}}{{country}}{{/b}}", + "emails.sessionAlert.listDevice": "Device: {{b}}{{agentDevice}}{{/b}}", + "emails.sessionAlert.listIpAddress": "IP Address: {{b}}{{ipAddress}}{{/b}}", + "emails.sessionAlert.listCountry": "Country: {{b}}{{country}}{{/b}}", "emails.sessionAlert.footer": "If you didn't request the sign in, you can safely ignore this email. If you suspect unauthorized activity, please secure your account immediately.", "emails.sessionAlert.thanks": "Thanks,", "emails.sessionAlert.signature": "{{project}} team", @@ -43,7 +43,7 @@ "emails.recovery.subject": "Password Reset", "emails.recovery.hello": "Hello {{user}}", "emails.recovery.body": "Follow this link to reset your {{b}}{{project}}{{/b}} password.", - "emails.recovery.footer": "If you didn’t ask to reset your password, you can ignore this message.", + "emails.recovery.footer": "If you didn't ask to reset your password, you can ignore this message.", "emails.recovery.thanks": "Thanks", "emails.recovery.signature": "{{project}} team", "emails.invitation.subject": "Invitation to %s Team at %s", diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 2f23d08432..3c29364f4b 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -73,9 +73,9 @@ function sendSessionAlert(Request $request, Locale $locale, Document $user, Docu $message ->setParam('{{hello}}', $locale->getText("emails.sessionAlert.hello")) ->setParam('{{body}}', $locale->getText("emails.sessionAlert.body")) - ->setParam('{{device}}', $locale->getText("emails.sessionAlert.device")) - ->setParam('{{ipAddress}}', $locale->getText("emails.sessionAlert.ipAddress")) - ->setParam('{{country}}', $locale->getText("emails.sessionAlert.country")) + ->setParam('{{listDevice}}', $locale->getText("emails.sessionAlert.listDevice")) + ->setParam('{{listIpAddress}}', $locale->getText("emails.sessionAlert.listIpAddress")) + ->setParam('{{listCountry}}', $locale->getText("emails.sessionAlert.listCountry")) ->setParam('{{footer}}', $locale->getText("emails.sessionAlert.footer")) ->setParam('{{signature}}', $locale->getText("emails.sessionAlert.signature")); @@ -129,6 +129,7 @@ function sendSessionAlert(Request $request, Locale $locale, Document $user, Docu $emailVariables = [ 'direction' => $locale->getText('settings.direction'), + 'dateTime' => DateTime::format(new \DateTime(), 'Y-m-d H:i:s'), 'user' => $user->getAttribute('name'), 'project' => $project->getAttribute('name'), 'agentDevice' => $agentDevice['deviceBrand'] ?? $agentDevice['deviceBrand'] ?? 'UNKNOWN', diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index e929d6fb20..95d187cd88 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -1196,9 +1196,11 @@ class AccountCustomClientTest extends Scope */ public function testSessionAlert($data): void { - $email = $data['email'] ?? ''; - $password = $data['password'] ?? ''; + $email = uniqid() . 'session-alert@appwrite.io'; + $password = 'password123'; + $name = 'Session Alert Tester'; + // Enable session alerts $response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $this->getProject()['$id'] . '/auth/session-alerts', array_merge([ 'origin' => 'http://localhost', 'content-type' => 'application/json', @@ -1210,6 +1212,21 @@ class AccountCustomClientTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); + // Create a new account + $response = $this->client->call(Client::METHOD_POST, '/account', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'userId' => ID::unique(), + 'email' => $email, + 'password' => $password, + 'name' => $name, + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + + // Create a session for the new account $response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', array_merge([ 'origin' => 'http://localhost', 'content-type' => 'application/json', @@ -1221,12 +1238,11 @@ class AccountCustomClientTest extends Scope $this->assertEquals(201, $response['headers']['status-code']); - // Check if an email alert was sent + // Check the alert email $lastEmail = $this->getLastEmail(); $this->assertEquals($email, $lastEmail['to'][0]['address']); $this->assertStringContainsString('New session alert', $lastEmail['subject']); - $this->assertStringContainsString($response['body']['$id'], $lastEmail['text']); // Session ID $this->assertStringContainsString($response['body']['ip'], $lastEmail['text']); // IP Address $this->assertStringContainsString($response['body']['osName'], $lastEmail['text']); // OS Name $this->assertStringContainsString($response['body']['clientType'], $lastEmail['text']); // Client Type