import React, { useEffect, useState } from 'react'; import PlexLoginButton from '../PlexLoginButton'; import { useUser } from '../../hooks/useUser'; import axios from 'axios'; import { useRouter } from 'next/dist/client/router'; import ImageFader from '../Common/ImageFader'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import Transition from '../Transition'; import LanguagePicker from '../Layout/LanguagePicker'; import LocalLogin from './LocalLogin'; import Accordion from '../Common/Accordion'; const messages = defineMessages({ signinheader: 'Sign in to continue', signinwithplex: 'Use your Plex account', signinwithoverseerr: 'Use your Overseerr account', }); const Login: React.FC = () => { const intl = useIntl(); const [error, setError] = useState(''); const [isProcessing, setProcessing] = useState(false); const [authToken, setAuthToken] = useState(undefined); const { user, revalidate } = useUser(); const router = useRouter(); // Effect that is triggered when the `authToken` comes back from the Plex OAuth // We take the token and attempt to login. If we get a success message, we will // ask swr to revalidate the user which _should_ come back with a valid user. useEffect(() => { const login = async () => { setProcessing(true); try { const response = await axios.post('/api/v1/auth/login', { authToken }); if (response.data?.email) { revalidate(); } } catch (e) { setError(e.response.data.message); setAuthToken(undefined); setProcessing(false); } }; if (authToken) { login(); } }, [authToken, revalidate]); // Effect that is triggered whenever `useUser`'s user changes. If we get a new // valid user, we redirect the user to the home page as the login was successful. useEffect(() => { if (user) { router.push('/'); } }, [user, router]); return (
Overseerr Logo

<>

{error}

{({ openIndexes, handleClick, AccordionContent }) => ( <>
setAuthToken(authToken)} />
)}
); }; export default Login;