-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpage.tsx
More file actions
345 lines (310 loc) · 9.56 KB
/
Copy pathpage.tsx
File metadata and controls
345 lines (310 loc) · 9.56 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
'use client'
import { Suspense, useEffect, useState } from 'react'
import { useSearchParams } from 'next/navigation'
function SpotifyDevContent() {
const searchParams = useSearchParams()
const [authUrl, setAuthUrl] = useState<string | null>(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const [tokens, setTokens] = useState<{
refresh_token: string | null
access_token: string | null
} | null>(null)
const [copied, setCopied] = useState<string | null>(null)
useEffect(() => {
const urlError = searchParams.get('error')
const details = searchParams.get('details')
const authSuccess = searchParams.get('success') === 'true'
if (urlError) {
setError(`${urlError}${details ? `: ${details}` : ''}`)
}
if (!authSuccess) {
return
}
let isActive = true
const loadTokens = async () => {
try {
const response = await fetch('/api/spotify/dev-token', {
cache: 'no-store'
})
if (!response.ok) {
throw new Error('Failed to read OAuth tokens')
}
const data = await response.json()
if (!isActive) return
if (!data.refresh_token && !data.access_token) {
setError(
'No token found. Please retry the authorization flow.'
)
return
}
setTokens({
refresh_token: data.refresh_token ?? null,
access_token: data.access_token ?? null
})
window.history.replaceState({}, '', '/dev/spotify')
} catch {
if (!isActive) return
setError('Failed to load generated tokens')
}
}
loadTokens()
return () => {
isActive = false
}
}, [searchParams])
const handleGetAuthUrl = async () => {
setLoading(true)
setError(null)
try {
const response = await fetch('/api/spotify/auth-url')
const data = await response.json()
if (data.error) {
setError(data.error)
return
}
setAuthUrl(data.authUrl)
} catch {
setError('Failed to get authorization URL')
} finally {
setLoading(false)
}
}
const handleCopy = async (text: string, type: string) => {
await navigator.clipboard.writeText(text)
setCopied(type)
setTimeout(() => setCopied(null), 2000)
}
function handleStartAuth() {
if (authUrl) {
window.location.href = authUrl
}
}
return (
<div className="min-h-screen bg-[#0a0a0a] text-white p-8">
<div className="max-w-2xl mx-auto">
<div className="mb-8">
<h1 className="text-3xl font-bold bg-gradient-to-r from-green-400 to-emerald-500 bg-clip-text text-transparent">
Spotify Token Generator
</h1>
<p className="text-zinc-400 mt-2">
Generate a refresh token for the Spotify API integration
</p>
</div>
{/* Error display */}
{error && (
<div className="mb-6 p-4 rounded-xl bg-red-500/10 border border-red-500/20">
<p className="text-red-400 text-sm">{error}</p>
</div>
)}
{/* Success: Display tokens */}
{tokens?.refresh_token && (
<div className="mb-8 space-y-4">
<div className="p-4 rounded-xl bg-green-500/10 border border-green-500/20">
<p className="text-green-400 text-sm font-medium mb-2">
✓ Authorization successful!
</p>
<p className="text-zinc-400 text-sm">
Copy the refresh token below and add it to your{' '}
<code className="px-1.5 py-0.5 rounded bg-zinc-800 text-zinc-300">
.env
</code>{' '}
file.
</p>
</div>
<div className="space-y-3">
<TokenField
label="Refresh Token"
value={tokens.refresh_token}
envVar="SPOTIFY_REFRESH_TOKEN"
copied={copied === 'refresh'}
onCopy={() =>
handleCopy(tokens.refresh_token!, 'refresh')
}
/>
{tokens.access_token && (
<TokenField
label="Access Token"
value={tokens.access_token}
envVar="(temporary, expires in 1 hour)"
copied={copied === 'access'}
onCopy={() =>
handleCopy(
tokens.access_token!,
'access'
)
}
/>
)}
</div>
<div className="p-4 rounded-xl bg-zinc-900 border border-zinc-800">
<p className="text-zinc-400 text-sm mb-2">
Add this to your{' '}
<code className="text-zinc-300">.env</code>{' '}
file:
</p>
<pre className="text-sm text-emerald-400 bg-zinc-950 p-3 rounded-none overflow-x-auto">
SPOTIFY_REFRESH_TOKEN={tokens.refresh_token}
</pre>
</div>
</div>
)}
{/* Step-by-step flow when no tokens yet */}
{!tokens?.refresh_token && (
<div className="space-y-6">
{/* Step 1: Get auth URL */}
<div className="p-6 rounded-xl bg-zinc-900/50 border border-zinc-800">
<div className="flex items-center gap-3 mb-4">
<div className="w-8 h-8 rounded-full bg-green-500/20 flex items-center justify-center text-green-400 font-bold text-sm">
1
</div>
<h2 className="text-lg font-semibold">
Generate Authorization URL
</h2>
</div>
<p className="text-zinc-400 text-sm mb-4">
First, generate the Spotify authorization URL.
Make sure you have{' '}
<code className="px-1.5 py-0.5 rounded bg-zinc-800 text-zinc-300">
SPOTIFY_CLIENT_ID
</code>{' '}
and{' '}
<code className="px-1.5 py-0.5 rounded bg-zinc-800 text-zinc-300">
SPOTIFY_CLIENT_SECRET
</code>{' '}
in your{' '}
<code className="px-1.5 py-0.5 rounded bg-zinc-800 text-zinc-300">
.env
</code>{' '}
file.
</p>
<button
onClick={handleGetAuthUrl}
disabled={loading}
className="px-4 py-2 rounded-none bg-green-500 hover:bg-green-600 text-black font-medium transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
>
{loading
? 'Loading...'
: 'Get Authorization URL'}
</button>
</div>
{/* Step 2: Authorize with Spotify */}
{authUrl && (
<div className="p-6 rounded-xl bg-zinc-900/50 border border-zinc-800">
<div className="flex items-center gap-3 mb-4">
<div className="w-8 h-8 rounded-full bg-green-500/20 flex items-center justify-center text-green-400 font-bold text-sm">
2
</div>
<h2 className="text-lg font-semibold">
Authorize with Spotify
</h2>
</div>
<p className="text-zinc-400 text-sm mb-4">
Click the button below to authorize your
Spotify account. You'll be redirected back
here with your refresh token.
</p>
<button
onClick={handleStartAuth}
className="px-4 py-2 rounded-none bg-[#1DB954] hover:bg-[#1ed760] text-black font-medium transition-colors flex items-center gap-2"
>
<SpotifyIcon />
Connect Spotify
</button>
</div>
)}
</div>
)}
{/* Requirements info */}
<div className="mt-8 p-4 rounded-xl bg-zinc-900/30 border border-zinc-800/50">
<h3 className="text-sm font-medium text-zinc-300 mb-2">
Required Environment Variables
</h3>
<ul className="text-xs text-zinc-500 space-y-1">
<li>
<code className="text-zinc-400">
SPOTIFY_CLIENT_ID
</code>{' '}
- From Spotify Developer Dashboard
</li>
<li>
<code className="text-zinc-400">
SPOTIFY_CLIENT_SECRET
</code>{' '}
- From Spotify Developer Dashboard
</li>
<li>
<code className="text-zinc-400">
SPOTIFY_REDIRECT_URI
</code>{' '}
- Should be{' '}
<code className="text-zinc-400">
{typeof window !== 'undefined'
? `${window.location.origin}/api/spotify/callback`
: 'http://localhost:3000/api/spotify/callback'}
</code>
</li>
</ul>
</div>
</div>
</div>
)
}
function TokenField({
label,
value,
envVar,
copied,
onCopy
}: {
label: string
value: string
envVar: string
copied: boolean
onCopy: () => void
}) {
return (
<div className="p-4 rounded-xl bg-zinc-900 border border-zinc-800">
<div className="flex items-center justify-between mb-2">
<span className="text-sm font-medium text-zinc-300">
{label}
</span>
<span className="text-xs text-zinc-500">{envVar}</span>
</div>
<div className="flex gap-2">
<input
type="text"
value={value}
readOnly
className="flex-1 px-3 py-2 text-sm bg-zinc-950 border border-zinc-800 rounded-none text-zinc-300 font-mono"
/>
<button
onClick={onCopy}
className="px-3 py-2 rounded-none bg-zinc-800 hover:bg-zinc-700 text-zinc-300 text-sm transition-colors"
>
{copied ? '✓ Copied' : 'Copy'}
</button>
</div>
</div>
)
}
function SpotifyIcon() {
return (
<svg className="w-5 h-5" viewBox="0 0 24 24" fill="currentColor">
<path d="M12 0C5.4 0 0 5.4 0 12s5.4 12 12 12 12-5.4 12-12S18.66 0 12 0zm5.521 17.34c-.24.359-.66.48-1.021.24-2.82-1.74-6.36-2.101-10.561-1.141-.418.122-.779-.179-.899-.539-.12-.421.18-.78.54-.9 4.56-1.021 8.52-.6 11.64 1.32.42.18.479.659.301 1.02zm1.44-3.3c-.301.42-.841.6-1.262.3-3.239-1.98-8.159-2.58-11.939-1.38-.479.12-1.02-.12-1.14-.6-.12-.48.12-1.021.6-1.141C9.6 9.9 15 10.561 18.72 12.84c.361.181.54.78.241 1.2zm.12-3.36C15.24 8.4 8.82 8.16 5.16 9.301c-.6.179-1.2-.181-1.38-.721-.18-.601.18-1.2.72-1.381 4.26-1.26 11.28-1.02 15.721 1.621.539.3.719 1.02.419 1.56-.299.421-1.02.599-1.559.3z" />
</svg>
)
}
export default function SpotifyDevPage() {
return (
<Suspense
fallback={
<div className="min-h-screen bg-[#0a0a0a] text-white p-8">
Loading...
</div>
}
>
<SpotifyDevContent />
</Suspense>
)
}