Skip to content

Commit

Permalink
Fix AllDebrid issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Sarmiento committed Nov 13, 2023
1 parent de63fda commit 08697a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
16 changes: 7 additions & 9 deletions src/hooks/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { clearRdKeys } from '@/utils/clearLocalStorage';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import useLocalStorage from './localStorage';
import { E } from 'vitest/dist/reporters-5f784f42';

interface RealDebridUser {
id: number;
Expand Down Expand Up @@ -88,7 +87,8 @@ export const useCurrentUser = () => {
const router = useRouter();
const [accessToken] = useLocalStorage<string>('rd:accessToken');
const [apiKey] = useLocalStorage<string>('ad:apiKey');
const [errors, setErrors] = useState<Map<string, any>>(new Map());
const [rdError, setRdError] = useState<Error | null>(null);
const [adError, setAdError] = useState<Error | null>(null);

useEffect(() => {
(async () => {
Expand All @@ -99,20 +99,18 @@ export const useCurrentUser = () => {
if (rdUserResponse) setRdUser(<RealDebridUser>rdUserResponse);
}
} catch (error: any) {
console.error('rd error', error);
setErrors((errors) => errors.set('rd', error));
setRdError(new Error(error));
}
try {
if (apiKey) {
const adUserResponse = await getAllDebridUser(apiKey);
if (adUserResponse) setAdUser(<AllDebridUser>adUserResponse);
}
} catch (error) {
console.error('ad error', error);
setErrors((errors) => errors.set('ad', error));
} catch (error: any) {
setAdError(new Error(error));
}
})();
}, [accessToken, apiKey, router, errors]);
}, [accessToken, apiKey, router]);

return { realDebrid: rdUser, allDebrid: adUser, errors };
return { realDebrid: rdUser, allDebrid: adUser, rdError, adError };
};
21 changes: 14 additions & 7 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ import { withAuth } from '@/utils/withAuth';
import Head from 'next/head';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import { Toaster, toast } from 'react-hot-toast';

function IndexPage() {
const router = useRouter();
const { realDebrid: rdUser, allDebrid: adUser, errors } = useCurrentUser();
const { realDebrid: rdUser, allDebrid: adUser, rdError, adError } = useCurrentUser();

if (errors.get('rd')) {
toast.error('Real-Debrid get user info failed, check your email and confirm the login coming from DMM');
}
if (errors.get('ad')) {
toast.error('AllDebrid get user info failed, check your email and confirm the login coming from DMM');
}
useEffect(() => {
if (rdError) {
toast.error(
'Real-Debrid get user info failed, check your email and confirm the login coming from DMM'
);
}
if (adError) {
toast.error(
'AllDebrid get user info failed, check your email and confirm the login coming from DMM'
);
}
}, [rdError, adError]);

const handleLogout = (prefix?: string) => {
if (typeof window === 'undefined') {
Expand Down

0 comments on commit 08697a4

Please sign in to comment.