A React application that demonstrates Spotify Web API authentication and displays user profile information and playlists with the ability to add songs to playlists. Please note that this is purely for educational purposes.
- Spotify OAuth 2.0 authentication using PKCE flow
- Display user profile information including:
- Display name
- Profile image
- User ID
- Country
- Follower count
- Spotify URI and profile links
- Browse user playlists including:
- Playlist images and names
- Track counts
- Owner information
- Public/private status
- Collaborative playlists
- Pagination support
- Add songs to playlists:
- Search for songs by name, artist, or album
- Select multiple tracks to add
- Choose from user's playlists
- Real-time feedback and error handling
- Modern React with TypeScript
- Responsive design with Spotify branding
- Navigation between Profile and Playlists views
- Go to Spotify Developer Dashboard
- Log in with your Spotify account
- Click "Create App"
- Fill in the app details:
- App name: "Spotify Client App" (or any name you prefer)
- App description: "A React app to view Spotify profile data and manage playlists"
- Redirect URI:
http://127.0.0.1:5173/callback
- Website:
http://127.0.0.1:5173
- Click "Save"
- After creating the app, you'll see your Client ID
- Copy the Client ID
- Create a
.env
file in the root directory - Add your Spotify Client ID:
VITE_SPOTIFY_CLIENT_ID=your_actual_client_id_here
npm install
npm run dev
The application will be available at http://127.0.0.1:5173
- Authentication Flow: The app uses Spotify's OAuth 2.0 PKCE (Proof Key for Code Exchange) flow for secure authentication
- User Authorization: Users are redirected to Spotify to authorize the application with required scopes
- Profile Data: After authorization, the app fetches and displays the user's profile information
- Playlist Data: Users can browse their playlists with pagination support
- Song Management: Users can search for songs and add them to their playlists
- State Management: Authentication state is managed using React hooks and localStorage
The application requests the following Spotify scopes:
user-read-private
: Access user's private informationuser-read-email
: Access user's email addressplaylist-read-private
: Access user's private playlistsplaylist-read-collaborative
: Access user's collaborative playlistsplaylist-modify-public
: Modify user's public playlistsplaylist-modify-private
: Modify user's private playlists
src/
├── components/
│ ├── Login.tsx # Login component
│ ├── Profile.tsx # Profile display component
│ ├── Playlists.tsx # Playlists display component
│ ├── AddSongToPlaylist.tsx # Add songs to playlist component
│ └── Callback.tsx # OAuth callback handler
├── hooks/
│ └── useSpotifyAuth.ts # Custom hook for auth state
├── services/
│ └── spotifyAuth.ts # Spotify API service
├── types.ts # TypeScript interfaces
├── App.tsx # Main app component with navigation
└── main.tsx # App entry point with routing
- React 19
- TypeScript
- React Router DOM
- Spotify Web API
- Vite (build tool)
GET /v1/me
- Get current user's profileGET /v1/users/{user_id}/playlists
- Get user's playlistsGET /v1/search
- Search for tracksPOST /v1/playlists/{playlist_id}/tracks
- Add tracks to playlist
- The app uses PKCE flow for enhanced security
- Access tokens are stored in localStorage (for demo purposes)
- In production, consider using more secure token storage methods
- The redirect URI is configured to prevent unauthorized redirects
-
"Invalid redirect URI" error: Make sure the redirect URI in your Spotify app settings exactly matches
http://127.0.0.1:5173/callback
-
"Invalid client" error: Verify that your Client ID is correctly set in the
.env
file -
"Insufficient scope" error: Make sure your Spotify app has the required scopes enabled
-
"Forbidden" error when adding songs: Ensure you have
playlist-modify-public
andplaylist-modify-private
scopes -
CORS errors: The app runs on localhost, so CORS shouldn't be an issue, but make sure you're using the correct redirect URI
- Check the browser console for any error messages
- Verify that your Spotify app has all the required scopes enabled
- Make sure you're logged into the correct Spotify account when testing
- The playlist modification feature requires the user to have playlists they can modify
- Search results are limited to 10 tracks per search for performance
This project is for educational purposes. Please follow Spotify's API terms of service when using their APIs.