diff --git a/coffee.module b/coffee.module
index e3bc8ae..5d4400b 100644
--- a/coffee.module
+++ b/coffee.module
@@ -8,13 +8,11 @@
 use Drupal\Core;
 
 /**
- * Implements hook_page_build().
+ * Implements hook_page_attachments().
  */
-function coffee_page_build(&$page) {
-  // Access check.
-  $account = \Drupal::currentUser();
-  if ($account->hasPermission('access coffee')) {
-    $page['#attached']['library'][] = 'coffee/drupal.coffee';
+function coffee_page_attachments(&$attachments) {
+  if (\Drupal::currentUser()->hasPermission('access coffee')) {
+    $attachments['#attached']['library'][] = 'coffee/drupal.coffee';
   }
 }
 
diff --git a/src/Tests/CoffeeTest.php b/src/Tests/CoffeeTest.php
index 577de9a..f6b211a 100644
--- a/src/Tests/CoffeeTest.php
+++ b/src/Tests/CoffeeTest.php
@@ -83,4 +83,26 @@ class CoffeeTest extends WebTestBase {
     $this->assertEqual($expected, $config, 'The configuration options have been properly saved');
   }
 
+  /**
+   * Tests that the coffee assets are loaded properly.
+   */
+  public function testCoffeeAssets() {
+    // Ensure that the coffee assets are not loaded for users without the
+    // adequate permission.
+    $this->drupalGet('');
+    $this->assertNoRaw('modules/coffee/js/coffee.js');
+
+    // Ensure that the coffee assets are loaded properly for users with the
+    // adequate permission.
+    $this->drupalLogin($this->coffeeUser);
+    $this->drupalGet('');
+    $this->assertRaw('modules/coffee/js/coffee.js');
+
+    // Ensure that the coffee assets are not loaded for users without the
+    // adequate permission.
+    $this->drupalLogin($this->webUser);
+    $this->drupalGet('');
+    $this->assertNoRaw('modules/coffee/js/coffee.js');
+  }
+  
 }
