Skip to content

Commit

Permalink
Add error for checking if login was blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Sarmiento committed Nov 13, 2023
1 parent 08f13aa commit de63fda
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
28 changes: 20 additions & 8 deletions src/hooks/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -87,20 +88,31 @@ 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());

useEffect(() => {
(async () => {
if (!accessToken && !apiKey) return null;
if (accessToken) {
const rdUserResponse = await getRealDebridUser(accessToken);
if (rdUserResponse) setRdUser(<RealDebridUser>rdUserResponse);
try {
if (accessToken) {
const rdUserResponse = await getRealDebridUser(accessToken);
if (rdUserResponse) setRdUser(<RealDebridUser>rdUserResponse);
}
} catch (error: any) {
console.error('rd error', error);
setErrors((errors) => errors.set('rd', error));
}
if (apiKey) {
const adUserResponse = await getAllDebridUser(apiKey);
if (adUserResponse) setAdUser(<AllDebridUser>adUserResponse);
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));
}
})();
}, [accessToken, apiKey, router]);
}, [accessToken, apiKey, router, errors]);

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

function IndexPage() {
const router = useRouter();
const { realDebrid: rdUser, allDebrid: adUser } = useCurrentUser();
const { realDebrid: rdUser, allDebrid: adUser, errors } = 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');
}

const handleLogout = (prefix?: string) => {
if (typeof window === 'undefined') {
Expand Down Expand Up @@ -42,6 +50,7 @@ function IndexPage() {
fill="#EDC951"
/>
</svg>
<Toaster position="bottom-right" />
{/* this is made by ChatGPT */}
{rdUser || adUser ? (
<>
Expand Down

0 comments on commit de63fda

Please sign in to comment.