From de63fda4bdec98371dfb0df6154f6b97e1ecef7c Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 13 Nov 2023 16:44:44 +0100 Subject: [PATCH] Add error for checking if login was blocked --- src/hooks/auth.ts | 28 ++++++++++++++++++++-------- src/pages/index.tsx | 11 ++++++++++- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/hooks/auth.ts b/src/hooks/auth.ts index 91b1666..643aa9c 100644 --- a/src/hooks/auth.ts +++ b/src/hooks/auth.ts @@ -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; @@ -87,20 +88,31 @@ export const useCurrentUser = () => { const router = useRouter(); const [accessToken] = useLocalStorage('rd:accessToken'); const [apiKey] = useLocalStorage('ad:apiKey'); + const [errors, setErrors] = useState>(new Map()); useEffect(() => { (async () => { if (!accessToken && !apiKey) return null; - if (accessToken) { - const rdUserResponse = await getRealDebridUser(accessToken); - if (rdUserResponse) setRdUser(rdUserResponse); + try { + if (accessToken) { + const rdUserResponse = await getRealDebridUser(accessToken); + if (rdUserResponse) setRdUser(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(adUserResponse); + try { + if (apiKey) { + const adUserResponse = await getAllDebridUser(apiKey); + if (adUserResponse) setAdUser(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 }; }; diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 287cac6..bb913aa 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -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') { @@ -42,6 +50,7 @@ function IndexPage() { fill="#EDC951" /> + {/* this is made by ChatGPT */} {rdUser || adUser ? ( <>