Skip to content

Commit

Permalink
Merge pull request #55 from usenocturne/develop/v2.1.1-beta.2
Browse files Browse the repository at this point in the history
Develop/v2.1.1 beta.2
  • Loading branch information
brandonsaldan authored Nov 30, 2024
2 parents 606e0df + 0668201 commit 13a23d5
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 124 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"color-thief-browser": "^2.0.2",
"lodash": "^4.17.21",
"lucide-react": "^0.441.0",
"next": "14.2.10",
"next": "^14.2.10",
"qrcode.react": "^4.1.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
3 changes: 0 additions & 3 deletions src/lib/supabaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export const getSupabaseClient = () => {
supabaseInstance = createClient(supabaseUrl, supabaseAnonKey, {
auth: {
persistSession: false
},
headers: {
'Accept': 'application/json'
}
});

Expand Down
47 changes: 0 additions & 47 deletions src/lib/tokenManager.js

This file was deleted.

32 changes: 0 additions & 32 deletions src/middleware.js

This file was deleted.

51 changes: 19 additions & 32 deletions src/pages/_app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import AuthSelection from "../components/AuthSelection";
import ButtonMappingOverlay from "../components/ButtonMappingOverlay";
import classNames from "classnames";
import { ErrorCodes } from "../constants/errorCodes";
import { tokenManager } from "../lib/tokenManager";

const inter = Inter({ subsets: ["latin", "latin-ext"] });

Expand Down Expand Up @@ -105,6 +104,7 @@ export default function App({ Component, pageProps }) {
useEffect(() => {
const initializeAuth = async () => {
if (typeof window === "undefined") return;

if (window.location.search.includes("code")) return;

const savedRefreshToken = localStorage.getItem("spotifyRefreshToken");
Expand All @@ -113,9 +113,7 @@ export default function App({ Component, pageProps }) {

if (savedRefreshToken && savedAuthType) {
try {
const refreshData = await tokenManager.refreshToken(() =>
refreshAccessToken()
);
const refreshData = await refreshAccessToken();

if (refreshData?.access_token) {
setAccessToken(refreshData.access_token);
Expand Down Expand Up @@ -143,7 +141,7 @@ export default function App({ Component, pageProps }) {
tempId: savedTempId,
});

if (savedAuthType === "custom" && tokenManager.shouldCleanup()) {
if (savedAuthType === "custom") {
await supabase
.from("spotify_credentials")
.update({
Expand Down Expand Up @@ -188,17 +186,6 @@ export default function App({ Component, pageProps }) {
"spotifyTokenExpiry",
new Date(Date.now() + 3600 * 1000).toISOString()
);

const tokenCheckInterval = setInterval(async () => {
const refreshData = await tokenManager.refreshToken(() =>
refreshAccessToken()
);
if (refreshData?.access_token) {
setAccessToken(refreshData.access_token);
}
}, 5 * 60 * 1000);

return () => clearInterval(tokenCheckInterval);
}
}, [accessToken]);

Expand Down Expand Up @@ -1378,18 +1365,28 @@ export default function App({ Component, pageProps }) {
const tempId = localStorage.getItem("spotifyTempId");
const authType = localStorage.getItem("spotifyAuthType");

if (
authType === "custom" &&
refreshToken &&
tempId &&
tokenManager.shouldCleanup()
) {
if (authType === "custom" && refreshToken && tempId) {
await supabase.from("spotify_credentials").delete().match({
temp_id: tempId,
refresh_token: refreshToken,
});
}

localStorage.removeItem("spotifyAccessToken");
localStorage.removeItem("spotifyRefreshToken");
localStorage.removeItem("spotifyTokenExpiry");
localStorage.removeItem("spotifyAuthType");
localStorage.removeItem("spotifyTempId");
localStorage.removeItem("spotifySessionId");

setAccessToken(null);
setRefreshToken(null);
setAuthState({
authSelectionMade: false,
authType: null,
});
} catch (error) {
console.error("Error during session cleanup:", error);
const authItems = [
"spotifyAccessToken",
"spotifyRefreshToken",
Expand All @@ -1398,18 +1395,8 @@ export default function App({ Component, pageProps }) {
"spotifyTempId",
"spotifySessionId",
];

authItems.forEach((item) => localStorage.removeItem(item));

setAccessToken(null);
setRefreshToken(null);
setAuthState({
authSelectionMade: false,
authType: null,
});
} catch (error) {
console.error("Error during session cleanup:", error);
localStorage.clear();
setAccessToken(null);
setRefreshToken(null);
setAuthState({
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/v1/auth/qr/create.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createClient } from '@supabase/supabase-js';
import { encrypt } from '@/lib/cryptoUtils';
export const runtime = 'experimental-edge';
import packageInfo from '../../../../../../package.json';

export default async function handler(req) {
if (req.method !== 'POST') {
Expand Down Expand Up @@ -51,8 +50,7 @@ export default async function handler(req) {
session_id: sessionId,
temp_id: tempId,
created_at: new Date().toISOString(),
expires_at: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(),
version: packageInfo.version
expires_at: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString()
}
]);

Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/v1/auth/refresh-token.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { supabase } from '@/lib/supabaseClient';
import { encrypt, decrypt } from '@/lib/cryptoUtils';
export const runtime = 'experimental-edge';
import packageInfo from '../../../../../package.json';

export default async function handler(req) {
if (req.method !== 'POST') {
Expand Down Expand Up @@ -151,8 +150,7 @@ export default async function handler(req) {
last_used: now.toISOString(),
first_used_at: oldRecord?.first_used_at || now.toISOString(),
token_refresh_count: (oldRecord?.token_refresh_count || 0) + 1,
user_agent: req.headers.get('user-agent') || null,
version: packageInfo.version
user_agent: req.headers.get('user-agent') || null
};

const { data: updatedRecord, error: updateError } = await supabase
Expand Down
4 changes: 1 addition & 3 deletions src/pages/api/v1/auth/token.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createClient } from '@supabase/supabase-js';
import { encrypt, decrypt } from '@/lib/cryptoUtils';
export const runtime = 'experimental-edge';
import packageInfo from '../../../../../package.json';

export default async function handler(req) {
if (req.method !== 'POST') {
Expand Down Expand Up @@ -112,8 +111,7 @@ export default async function handler(req) {
token_expiry: new Date(Date.now() + data.expires_in * 1000).toISOString(),
auth_completed: true,
first_used_at: new Date().toISOString(),
last_used: new Date().toISOString(),
version: packageInfo.version
last_used: new Date().toISOString()
})
.eq('session_id', sessionId);

Expand Down

0 comments on commit 13a23d5

Please sign in to comment.