Fix Spotify auth, playlist creation, and Expo config

This commit is contained in:
Eduardo Silva
2026-05-21 11:58:25 +01:00
parent 9222d3a483
commit a587b3a1bd
8 changed files with 377 additions and 136 deletions

View File

@@ -4,7 +4,8 @@ import { SafeAreaView } from 'react-native-safe-area-context';
import { X, MapPin, ArrowRight, Navigation } from 'lucide-react-native';
import { colors } from '../../utils/colors';
import { supabase } from '../../services/supabase';
import { getSpotifyAccessToken } from '../../auth/spotifyToken';
import { getSpotifyAccessToken, refreshSpotifyToken } from '../../auth/spotifyToken';
import { OLLAMA_API_URL } from '../../services/ollama';
// @ts-ignore
export default function NewTripScreen({ navigation }) {
@@ -56,20 +57,52 @@ export default function NewTripScreen({ navigation }) {
console.log(`PLAYLIST_API_CONTENT_TYPE [${label}]:`, res.headers.get("content-type"));
console.log(`PLAYLIST_API_RAW_RESPONSE [${label}]:`, rawText.substring(0, 300) + (rawText.length > 300 ? "..." : ""));
if (!res.ok) {
throw new Error(`Spotify API returned status ${res.status} for [${label}]: ${rawText.substring(0, 150)}`);
}
const contentType = res.headers.get("content-type") || "";
if (!contentType.includes("application/json")) {
throw new Error(`Playlist API returned non-JSON response for [${label}]: ${rawText.substring(0, 150)}`);
}
try {
return JSON.parse(rawText);
} catch (e) {
throw new Error(`Playlist API returned non-JSON response [${label}]: ${rawText}`);
throw new Error(`Failed to parse JSON response for [${label}]: ${rawText.substring(0, 150)}`);
}
};
// A. Get provider token
const providerToken = await getSpotifyAccessToken();
let providerToken = await getSpotifyAccessToken();
console.log("SPOTIFY_ACCESS_TOKEN_EXISTS:", !!providerToken);
if (providerToken) {
// Proactively check if token is valid, or refresh it
console.log("Validating Spotify token...");
let testRes = await fetch('https://api.spotify.com/v1/me', {
headers: { Authorization: `Bearer ${providerToken}` }
});
if (testRes.status === 401) {
console.log("Spotify token is invalid/expired (401), attempting to refresh...");
const newToken = await refreshSpotifyToken();
if (newToken) {
providerToken = newToken;
} else {
console.log("Failed to refresh Spotify token.");
providerToken = null;
}
} else if (!testRes.ok) {
const testErr = await testRes.text();
console.error("Spotify validation request failed:", testRes.status, testErr);
providerToken = null;
}
}
if (!providerToken) {
console.log("Spotify token missing, skipping playlist generation.");
Alert.alert('Aviso', 'Spotify token missing, please login again');
console.log("Spotify token missing or expired, skipping playlist generation.");
Alert.alert('Spotify Desligado', 'O token do Spotify expirou ou está em falta. Por favor reconecte o Spotify no Perfil.');
} else {
// B. Fetch Spotify User ID
const spotifyUserRes = await fetch('https://api.spotify.com/v1/me', {
@@ -83,7 +116,7 @@ export default function NewTripScreen({ navigation }) {
const spotifyUserId = spotifyUserData.id;
// C. Call Ollama server
const ollamaRes = await fetch("http://89.114.196.110:11434/api/chat", {
const ollamaRes = await fetch(`${OLLAMA_API_URL}/api/chat`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({