From 3c2248ae687e8be26936635f54ab094fca212883 Mon Sep 17 00:00:00 2001
From: zidane-itb <16521420@mahasiswa.itb.ac.id>
Date: Fri, 17 Nov 2023 14:50:31 +0700
Subject: [PATCH] fix subscription

---
 src/web/controller/audio-file-controller.php    | 2 +-
 src/web/presentation/account/login.php          | 3 ++-
 src/web/presentation/home/home.php              | 4 ++++
 src/web/presentation/home/home.view.php         | 4 ++++
 src/web/presentation/music/info/js/albumInfo.js | 4 +++-
 src/web/presentation/music/info/js/musicInfo.js | 3 ++-
 6 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/web/controller/audio-file-controller.php b/src/web/controller/audio-file-controller.php
index 241318f..6246651 100644
--- a/src/web/controller/audio-file-controller.php
+++ b/src/web/controller/audio-file-controller.php
@@ -1,7 +1,7 @@
 <?php
 
 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
-    $checkSubsUrl = 'http://node:3000/services/relaxed/subscription/'.$_GET['id'];
+    $checkSubsUrl = 'http://node:3000/services/relaxed/subscription/'.$_GET['accountId'];
     $checkSubsResp = file_get_contents($checkSubsUrl);
 
     $subsData = json_decode($checkSubsResp, true);
diff --git a/src/web/presentation/account/login.php b/src/web/presentation/account/login.php
index 1cd1705..130ceeb 100644
--- a/src/web/presentation/account/login.php
+++ b/src/web/presentation/account/login.php
@@ -41,7 +41,8 @@ if ($_SERVER["REQUEST_METHOD"] == "GET") {
         if (isset($_GET['r'])) {
             $redirectLoc = $_GET['r'];
         }
-        header("Location: ".$redirectLoc, true, 303);
+        $accountId = authenticateToken($res);
+        header("Location: ".$redirectLoc.'?id='.$accountId, true, 303);
         exit();
     }
     $errMsg = 'Invalid username or password.';
diff --git a/src/web/presentation/home/home.php b/src/web/presentation/home/home.php
index fb2bd98..fb31d1d 100644
--- a/src/web/presentation/home/home.php
+++ b/src/web/presentation/home/home.php
@@ -17,4 +17,8 @@ $accountId = authenticateToken($_COOKIE['token']);
 // Get random songs
 $shownalbums = execSelect('SELECT * FROM album ORDER BY RANDOM() LIMIT 6', []);
 
+if ($_GET['id']) {
+    $accountId = $_GET['id'];
+}
+
 require 'presentation/home/home.view.php';
\ No newline at end of file
diff --git a/src/web/presentation/home/home.view.php b/src/web/presentation/home/home.view.php
index 71a1fe5..45cd446 100644
--- a/src/web/presentation/home/home.view.php
+++ b/src/web/presentation/home/home.view.php
@@ -11,6 +11,10 @@
     <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
     <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
     <script type="text/javascript" src="presentation\home\js\home.js" defer></script>
+    $accountId
+    <script>
+        localStorage.setItem('id', '<?php echo $accountId;?>');
+    </script>
     <title>Home</title>
 </head>
 <body>
diff --git a/src/web/presentation/music/info/js/albumInfo.js b/src/web/presentation/music/info/js/albumInfo.js
index ccec2ff..0c8762c 100644
--- a/src/web/presentation/music/info/js/albumInfo.js
+++ b/src/web/presentation/music/info/js/albumInfo.js
@@ -6,14 +6,16 @@ for (let item of listItems) {
     item.addEventListener('click', (e) => {
         e.preventDefault();
 
+        let accountId = localStorage.getItem('id');
         let id = item.id;
         let xhr = new XMLHttpRequest();
 
-        const url = "/audio?id="+id
+        const url = "/audio?id="+id+"&accountId="+accountId
 
         xhr.open('GET', url, true)
         xhr.onreadystatechange = function() {
             if (xhr.readyState === 4 && xhr.status === 200) {
+                console.log(xhr.responseText)
                 const audio = JSON.parse(xhr.responseText)
 
                 if (audio.hasOwnProperty('ads')) {
diff --git a/src/web/presentation/music/info/js/musicInfo.js b/src/web/presentation/music/info/js/musicInfo.js
index 5ef2687..d28b8a3 100644
--- a/src/web/presentation/music/info/js/musicInfo.js
+++ b/src/web/presentation/music/info/js/musicInfo.js
@@ -7,9 +7,10 @@ playButton && playButton.addEventListener("click", (e) => {
     e.preventDefault()
 
     const query = new URLSearchParams(window.location.search)
+    let accountId = localStorage.getItem('id');
     const id = query.get("id")
     const xhr = new XMLHttpRequest()
-    const url = "/audio?id="+id
+    const url = "/audio?id="+id+"&accountId="+accountId
 
     xhr.open('GET', url, true)
     xhr.onreadystatechange = function() {
-- 
GitLab