A production-ready example of integrating Spotify's API with Next.js, demonstrating secure authentication flow and state management without relying on next-auth. This project showcases how to implement a "Connect with Spotify" feature rather than using Spotify as a primary authentication provider.
Check out the live example: Coming Soon!
- 🎯 Key Features
- 🤔 Why This Project?
- 🏗️ Project Structure
- 🚀 Getting Started
- ⚙️ Environment Variables
- 🔒 Security Features
- 🛠️ Implementation Details
- 📚 Key Differences from Other Examples
- 🤝 Contributing
- 📝 License
- Complete Spotify OAuth flow implementation using PKCE
- Token management (access & refresh) using Zustand for state persistence
- Secure backend implementation using Next.js API routes
- Example of creating Spotify playlists on behalf of connected users
- No exposure of client secrets to the frontend
While many Next.js + Spotify examples exist, most focus on creating Spotify clones or use next-auth's Spotify provider. This project fills a different need: implementing Spotify as a secondary connection option where users:
- Can authenticate to your app independently (e.g., email/password)
- Have the option to connect their Spotify account later
- Can perform Spotify actions (e.g., create playlists) once connected
app/
├── api/spotify/ # Spotify API endpoints
│ ├── auth/ # Authentication handlers
│ ├── refresh/ # Token refresh logic
│ └── token/ # Token management
├── components/
│ ├── ui/ # UI components
│ └── SpotifyAuth.tsx # Spotify authentication component
├── lib/
│ ├── pkce.ts # PKCE helper utilities
│ └── utils.ts # General utilities
└── store/spotify/ # Zustand store for Spotify state
├── index.ts
└── types.ts
- Clone this repository
- Set up your Spotify Application:
- Follow the Spotify Web API documentation to create and configure your application
- Add
http://localhost:3000
to your Redirect URIs in the app settings - Note down your Client ID and Client Secret
- Copy
.env.example
to.env.local
and fill in your Spotify API credentials - Install dependencies:
npm install
- Run the development server:
npm run dev
# Development: http://localhost:3000
# Production: https://your-domain.com
SPOTIFY_REDIRECT_URI=
SPOTIFY_CLIENT_ID=your_client_id
SPOTIFY_CLIENT_SECRET=your_client_secret
- All sensitive operations happen server-side via API routes
- PKCE (Proof Key for Code Exchange) implementation for enhanced security
- No client secrets exposed to the frontend
- Secure token management and refresh handling
- User initiates Spotify connection
- PKCE challenge is generated
- User is redirected to Spotify consent screen
- Authorization code is received and exchanged for tokens
- Tokens are stored in Zustand store with persistence
- Zustand is used for frontend state management
- Persistent storage of authentication state
- Automatic token refresh handling
- No dependency on next-auth
- Complete control over the authentication flow
- Separation of primary app authentication from Spotify integration
- Production-ready security considerations
- Clear separation of frontend and backend concerns
Contributions are welcome! Please feel free to submit a Pull Request.
MIT
Built with ❤️ to help developers implement Spotify integration properly