on first load, redirect to login api for spotify to load user's top tracks.

This commit is contained in:
Joshua Ashton
2025-02-02 12:07:01 -07:00
parent c15cba445a
commit be854ce3c5
3 changed files with 32 additions and 30 deletions
BIN
View File
Binary file not shown.
+3 -4
View File
@@ -7,7 +7,7 @@ $dotenv->safeLoad();
session_start();
$client_id = $_ENV['SPOTIFY_CLIENT_ID'];
$client_secret = $_ENV['SPOTIFY_CLIENT_SECRET'];
$auth_code = $_GET['code'];
$_SESSION['auth_code'] = $_GET['code'];
$client_credentials = get_client_credentials($client_id, $client_secret);
@@ -112,6 +112,7 @@ function get_access_token($auth_code, $client_id, $client_secret)
function get_albums($auth_code, $client_id, $client_secret)
{
if ($_SESSION['data'] == null) {
$access_token = get_access_token($auth_code, $client_id, $client_secret);
$url = 'https://api.spotify.com/v1/me/top/tracks';
@@ -136,7 +137,6 @@ function get_albums($auth_code, $client_id, $client_secret)
curl_close($ch);
$song_data = [];
if ($_SESSION['data'] == null) {
foreach ($top_songs as $song) {
$song_data[] = array(
'image' => $song['album']['images'][0]['url'],
@@ -148,7 +148,6 @@ function get_albums($auth_code, $client_id, $client_secret)
);
}
echo "saving data to $_SESSION";
$_SESSION['data'] = $song_data;
} else {
$song_data = $_SESSION['data'];
@@ -165,5 +164,5 @@ function get_albums($auth_code, $client_id, $client_secret)
}
}
get_albums($auth_code, $client_id, $client_secret);
get_albums($_SESSION['auth_code'], $client_id, $client_secret);
?>
+6 -3
View File
@@ -1,6 +1,10 @@
<?php
session_start();
include ('login.php');
if (!isset($_GET['code'])) {
header('Location: http://localhost:9753/login');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
@@ -13,8 +17,7 @@ include ('login.php');
<body>
<h1>My Favorite Albums</h1>
<button id="switcher" onclick=modeSwitch('core')>Core</button>
<button id="switcher" onclick=modeSwitch('core')>Explore</button>
<div id="content">
<?php include ('explore.php'); ?>
</div>