|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import { useAuth } from '@/app/context/authContext'; |
4 | | -import useRefreshToken from '@/app/hooks/useRefreshToken'; |
5 | | -import spotifyApi from '@/app/lib/spotifyApi'; |
6 | | -import { decrypt, encrypt } from '@/app/lib/utils'; |
7 | 3 | import axios, { AxiosResponse } from 'axios'; |
| 4 | +import { decrypt, encrypt } from '@/app/lib/utils'; |
| 5 | +import { useEffect, useRef, useState } from 'react'; |
8 | 6 | import { usePathname, useRouter, useSearchParams } from 'next/navigation'; |
9 | | -import { useEffect, useState } from 'react'; |
| 7 | + |
| 8 | +import spotifyApi from '@/app/lib/spotifyApi'; |
| 9 | +import { useAuth } from '@/app/context/authContext'; |
| 10 | +import useRefreshToken from '@/app/hooks/useRefreshToken'; |
10 | 11 |
|
11 | 12 | const ConnectSpotify = ({ authUrl }: { authUrl: string }) => { |
12 | 13 | const { isAuthInProgress, isLoggedIn, authInProgress, logIn, setUserData } = |
13 | 14 | useAuth(); |
14 | 15 |
|
15 | 16 | const [expires, setExpires] = useState<number | null>(null); |
| 17 | + const isExchangingCode = useRef(false); |
16 | 18 |
|
17 | 19 | const currentPath = usePathname(); |
18 | 20 |
|
19 | 21 | const searchParams = useSearchParams(); |
20 | 22 | const router = useRouter(); |
21 | 23 |
|
22 | 24 | useEffect(() => { |
23 | | - const current = new URLSearchParams(Array.from(searchParams.entries())); |
24 | | - const extractedCode = searchParams.get('code'); |
25 | | - |
26 | | - if (extractedCode && extractedCode !== '') { |
27 | | - if (isAuthInProgress) return; |
28 | | - authInProgress(true); |
29 | | - loginUser(extractedCode); |
30 | | - current.delete('code'); |
31 | | - |
32 | | - const search = current.toString(); |
33 | | - const query = search ? `?${search}` : ''; |
34 | | - |
35 | | - router.push(`${currentPath}${query}`); |
36 | | - return; |
37 | | - } |
38 | | - |
39 | | - refreshAccessToken(); |
| 25 | + (async () => { |
| 26 | + const current = new URLSearchParams(Array.from(searchParams.entries())); |
| 27 | + const extractedCode = searchParams.get('code'); |
| 28 | + |
| 29 | + if (extractedCode && extractedCode !== '') { |
| 30 | + if (isAuthInProgress || isExchangingCode.current) return; |
| 31 | + isExchangingCode.current = true; |
| 32 | + authInProgress(true); |
| 33 | + |
| 34 | + // Attempt to exchange the code. Only remove `code` from the URL |
| 35 | + // if the exchange succeeds. If it fails, keep the code visible |
| 36 | + // so the developer can copy it for debugging. |
| 37 | + const response = await loginUser(extractedCode); |
| 38 | + if (response?.status === 200) { |
| 39 | + current.delete('code'); |
| 40 | + |
| 41 | + const search = current.toString(); |
| 42 | + const query = search ? `?${search}` : ''; |
| 43 | + |
| 44 | + router.push(`${currentPath}${query}`); |
| 45 | + } else { |
| 46 | + // leave the code in the URL for debugging; authInProgress |
| 47 | + // will be set to false in loginUser when a failure occurs |
| 48 | + isExchangingCode.current = false; |
| 49 | + } |
| 50 | + |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + refreshAccessToken(); |
| 55 | + })(); |
40 | 56 | }, []); |
41 | 57 |
|
42 | 58 | async function loginUser(code: string) { |
43 | | - if (!code) return; |
44 | | - const response = await axios.post('/api/auth', { code }); |
45 | | - processResponse(response); |
| 59 | + if (!code) return null; |
| 60 | + try { |
| 61 | + const response = await axios.post('/api/auth', { code }); |
| 62 | + processResponse(response); |
| 63 | + return response; |
| 64 | + } catch (err: any) { |
| 65 | + // If server returned spotify_error, surface it on client console |
| 66 | + console.error('Login exchange failed', err?.response?.data || err); |
| 67 | + authInProgress(false); |
| 68 | + // If the code expired, quickly redirect to start a fresh auth |
| 69 | + const isExpired = err?.response?.data?.spotify_error?.error_description === 'Authorization code expired'; |
| 70 | + if (isExpired) window.location.href = authUrl; |
| 71 | + return err?.response || null; |
| 72 | + } |
46 | 73 | } |
47 | 74 |
|
48 | 75 | async function refreshAccessToken() { |
|
0 commit comments