Skip to content

Commit

Permalink
fix(auth): remove cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonsaldan committed Dec 3, 2024
1 parent 2e588a4 commit 5c8a21e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nocturne",
"version": "v2.1.1-beta.3",
"version": "v2.1.1-beta.4",
"private": true,
"scripts": {
"dev": "node server.js",
Expand Down
46 changes: 18 additions & 28 deletions src/pages/api/v1/auth/refresh-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export default async function handler(req) {

try {
const { refresh_token, isCustomAuth } = await req.json();

if (!refresh_token) {
console.error('Missing refresh token');
return new Response(
JSON.stringify({ error: 'Refresh token is required' }),
{
JSON.stringify({ error: 'Refresh token is required' }),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
Expand All @@ -31,25 +31,15 @@ export default async function handler(req) {
if (!supabase) {
console.error('Supabase client not initialized');
return new Response(
JSON.stringify({ error: 'Database connection error' }),
{
JSON.stringify({ error: 'Database connection error' }),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}
);
}

try {
const { error: cleanupError } = await supabase
.from('spotify_credentials')
.delete()
.eq('refresh_token', refresh_token)
.lt('created_at', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString());

if (cleanupError) {
console.error('Error cleaning up old records:', cleanupError);
}

const { data: credentials, error: fetchError } = await supabase
.from('spotify_credentials')
.select('*')
Expand All @@ -65,8 +55,8 @@ export default async function handler(req) {
refreshToken: refresh_token.substring(0, 10) + '...'
});
return new Response(
JSON.stringify({ error: 'Custom credentials not found' }),
{
JSON.stringify({ error: 'Custom credentials not found' }),
{
status: 400,
headers: { 'Content-Type': 'application/json' }
}
Expand All @@ -81,8 +71,8 @@ export default async function handler(req) {
} catch (decryptError) {
console.error('Decryption error:', decryptError);
return new Response(
JSON.stringify({ error: 'Failed to decrypt credentials' }),
{
JSON.stringify({ error: 'Failed to decrypt credentials' }),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}
Expand All @@ -91,8 +81,8 @@ export default async function handler(req) {
} catch (dbError) {
console.error('Database operation error:', dbError);
return new Response(
JSON.stringify({ error: 'Database operation failed' }),
{
JSON.stringify({ error: 'Database operation failed' }),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}
Expand Down Expand Up @@ -124,7 +114,7 @@ export default async function handler(req) {
error: data,
clientIdPrefix: clientId.substring(0, 10) + '...'
});
return new Response(JSON.stringify(data), {
return new Response(JSON.stringify(data), {
status: response.status,
headers: { 'Content-Type': 'application/json' }
});
Expand Down Expand Up @@ -177,8 +167,8 @@ export default async function handler(req) {
access_token: data.access_token,
refresh_token: data.refresh_token,
expires_in: data.expires_in
}),
{
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' }
}
Expand All @@ -187,12 +177,12 @@ export default async function handler(req) {
} catch (error) {
console.error('Unhandled error:', error);
return new Response(
JSON.stringify({
JSON.stringify({
error: 'Failed to refresh access token',
details: error.message,
stack: error.stack
}),
{
stack: error.stack
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}
Expand Down
58 changes: 22 additions & 36 deletions src/pages/api/v1/auth/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function handler(req) {
if (!code) {
console.error('Missing authorization code');
return new Response(
JSON.stringify({ error: 'Authorization code is required' }),
JSON.stringify({ error: 'Authorization code is required' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
Expand All @@ -35,19 +35,19 @@ export default async function handler(req) {
.select('client_id, encrypted_client_secret')
.eq('session_id', sessionId)
.maybeSingle();

if (credentialsError) {
console.error('Database query error:', credentialsError);
return new Response(
JSON.stringify({ error: 'Failed to get credentials', details: credentialsError.message }),
JSON.stringify({ error: 'Failed to get credentials', details: credentialsError.message }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}

if (!credentials) {
console.error('No credentials found in database');
return new Response(
JSON.stringify({ error: 'Credentials not found or expired' }),
JSON.stringify({ error: 'Credentials not found or expired' }),
{ status: 404, headers: { 'Content-Type': 'application/json' } }
);
}
Expand All @@ -58,7 +58,7 @@ export default async function handler(req) {
} catch (decryptError) {
console.error('Decryption error:', decryptError);
return new Response(
JSON.stringify({ error: 'Failed to decrypt credentials' }),
JSON.stringify({ error: 'Failed to decrypt credentials' }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
Expand All @@ -70,7 +70,7 @@ export default async function handler(req) {
if (!useClientId || !useClientSecret) {
console.error('Missing credentials after retrieval');
return new Response(
JSON.stringify({ error: 'Invalid credentials configuration' }),
JSON.stringify({ error: 'Invalid credentials configuration' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
Expand All @@ -94,11 +94,11 @@ export default async function handler(req) {
if (!response.ok) {
console.error('Spotify token exchange failed:', data);
return new Response(
JSON.stringify({
error: 'Token exchange failed',
JSON.stringify({
error: 'Token exchange failed',
spotifyError: data.error,
spotifyErrorDescription: data.error_description
}),
spotifyErrorDescription: data.error_description
}),
{ status: response.status, headers: { 'Content-Type': 'application/json' } }
);
}
Expand All @@ -120,24 +120,10 @@ export default async function handler(req) {
if (updateError) {
console.error('Error updating tokens in database:', updateError);
return new Response(
JSON.stringify({ error: 'Failed to store tokens', details: updateError.message }),
JSON.stringify({ error: 'Failed to store tokens', details: updateError.message }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}

try {
const { error: cleanupError } = await supabase
.from('spotify_credentials')
.delete()
.lt('created_at', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString())
.is('auth_completed', true);

if (cleanupError) {
console.error('Error during cleanup:', cleanupError);
}
} catch (cleanupError) {
console.error('Error cleaning up old records:', cleanupError);
}
}

return new Response(
Expand All @@ -148,27 +134,27 @@ export default async function handler(req) {
token_type: data.token_type,
scope: data.scope,
isPhoneAuth
}),
{
status: 200,
headers: {
}),
{
status: 200,
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-store, must-revalidate'
}
}
}
);

} catch (error) {
console.error('Unhandled token exchange error:', error);
return new Response(
JSON.stringify({
JSON.stringify({
error: 'Failed to process token exchange',
details: error.message,
stack: error.stack
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}
);
}
Expand Down

0 comments on commit 5c8a21e

Please sign in to comment.