-
Notifications
You must be signed in to change notification settings - Fork 0
/
Login JS.js
217 lines (173 loc) · 6.58 KB
/
Login JS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const clientId = 'dd8233fe305a40698c7596d33b5232ef';
const redirectUrl = 'https://kubarozwadowski.github.io/MelodyMatch/mainPage.html';
const authorizationEndpoint = 'https://accounts.spotify.com/authorize';
const scope = 'user-read-private user-read-email';
const backendUrl = 'https://shimmering-shortbread-025f27.netlify.app/';
const tokenExchangeEndpoint = 'https://accounts.spotify.com/api/token';
// Function to handle authorization callback
async function handleAuthorizationCallback() {
console.log('Handling authorization callback...');
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
if (code) {
console.log('Authorization code:', code);
const _accessToken = await exchangeCodeForToken(code);
setAccessToken(_accessToken);
// Exchange the authorization code for an access token
console.log('Access token:', _accessToken);
// Save access token to cookies
setCookie('accessToken', _accessToken);
// Fetch user data and update the DOM
fetchUserData(_accessToken);
// Fetch additional Spotify data and save it to the backend
fetchSpotifyData();
} else {
console.error('No authorization code found in the URL.');
}
}
// Function to exchange the authorization code for an access token
async function exchangeCodeForToken(code) {
const response = await fetch(tokenExchangeEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `grant_type=authorization_code&code=${code}&redirect_uri=${redirectUrl}&client_id=${clientId}`,
});
const data = await response.json();
return data.access_token;
}
// Function to set a cookie
function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`;
}
// Function to get a cookie
function getCookie(name) {
const cookies = document.cookie.split(';').map(cookie => cookie.trim().split('='));
const cookie = cookies.find(cookie => cookie[0] === name);
return cookie ? cookie[1] : null;
}
// ... (rest of your code remains the same)
let accessToken; // Variable to store the access token
// Function to set the access token
function setAccessToken(token) {
accessToken = token;
}
// Function to fetch Spotify data and log to the console
async function fetchSpotifyData() {
console.log('Fetching Spotify data...');
if (accessToken) {
const userDataResponse = await fetch('https://api.spotify.com/v1/me', {
headers: {
'Authorization': 'Bearer ' + accessToken,
},
});
if (userDataResponse.ok) {
const userData = await userDataResponse.json();
console.log('Spotify Data:', userData);
// Save Spotify data to the backend
saveSpotifyDataToBackend(userData);
} else {
console.error('Failed to fetch user data from Spotify API');
}
} else {
console.error('Access token not found.');
}
}
// Function to save Spotify data to the backend
async function saveSpotifyDataToBackend(userData) {
try {
// Save Data to backend
await fetch(backendUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userData),
});
// Update the DOM with the fetched data
// ...
} catch (error) {
console.error('Error saving user data to the backend:', error);
}
}
// Function to get users from the backend
async function getUsersFromBackend() {
try {
const response = await fetch(backendUrl);
const usersData = await response.json();
// Display user data on the frontend as needed
console.log('Users Data from Backend:', usersData);
} catch (error) {
console.error('Error fetching users from the backend:', error);
}
}
// Other functions in your code...
// Assume that somewhere in your code, after successful login, you call handleAuthorizationCallback
function handleLoginCallback() {
// Handle the callback after successful login (e.g., token exchange)
handleAuthorizationCallback();
}
function redirectToSpotifyAuthorize() {
const authUrl = new URL(authorizationEndpoint);
const params = {
response_type: 'code',
client_id: clientId,
scope: scope,
redirect_uri: redirectUrl,
};
authUrl.search = new URLSearchParams(params).toString();
window.location.href = authUrl.toString(); // Redirect the user to the authorization server for login
}
function loginWithSpotifyClick() {
redirectToSpotifyAuthorize();
}
// Function to fetch additional user data
// Function to fetch additional user data
async function fetchUserData(accessToken) {
try {
// Fetch user data using Spotify API
const userDataResponse = await fetch('https://api.spotify.com/v1/me', {
headers: {
'Authorization': 'Bearer ' + accessToken,
},
});
if (!userDataResponse.ok) {
throw new Error('Failed to fetch user data from Spotify API');
}
const userData = await userDataResponse.json();
// Save ID and name to cookies
setCookie('spotifyUserId', userData.id, 7);
setCookie('spotifyUserName', userData.display_name || 'Anonymous', 7);
// Save Data to backend
saveSpotifyDataToBackend(userData);
// Update the DOM with the fetched data
document.getElementById('profileImage').src = userData.images[0].url;
document.getElementById('personName').textContent = userData.display_name || 'Anonymous';
document.getElementById('personBio').textContent = userData.bio || 'No bio available';
// Assuming top genres, artists, and tracks are arrays of strings
updateList('topGenresList', userData.top_genres);
updateList('topArtistsList', userData.top_artists);
updateList('topSongsList', userData.top_tracks);
} catch (error) {
console.error('Error fetching user data:', error);
}
}
// Function to update a list in the DOM
function updateList(listId, items) {
const list = document.getElementById(listId);
list.innerHTML = ''; // Clear existing items
items.forEach(item => {
const listItem = document.createElement('li');
listItem.classList.add('list-group-item');
listItem.textContent = item;
list.appendChild(listItem);
});
}
function getData() {
fetch(backendUrl);
}
// Call handleAuthorizationCallback on page load to check if there is a code in the url params
handleAuthorizationCallback();