Spotify API Authentication Service

This module handles the OAuth 2.0 authentication process with Spotify's Web API, including authorization URL generation, token exchange, and authentication flow management.

Features:

  • OAuth 2.0 authorization code flow implementation
  • Authorization URL generation with configurable scopes
  • Secure token exchange process
  • Automatic token storage upon successful authentication
  • Comprehensive error handling during authentication
  • Security features including state parameter support

Usage:

import { getAuthorizationUrl, exchangeCodeForTokens } from '@/services/spotify/auth';

// Generate the authorization URL for the user to visit
const redirectUri = 'http://localhost:3000/callback';
const scopes = ['user-read-playback-state', 'user-modify-playback-state'];
const authUrl = getAuthorizationUrl(redirectUri, scopes);

// After redirect, exchange the received code for tokens
const tokens = await exchangeCodeForTokens(code, redirectUri);

Functions

getAuthorizationUrl
exchangeCodeForTokens