on first load, redirect to login api for spotify to load user's top tracks.
This commit is contained in:
+3
-4
@@ -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
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user