import { XCircleIcon } from '@heroicons/react/solid'; import axios from 'axios'; import { useRouter } from 'next/dist/client/router'; import React, { useEffect, useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import useSWR from 'swr'; import useSettings from '../../hooks/useSettings'; import { useUser } from '../../hooks/useUser'; import Accordion from '../Common/Accordion'; import ImageFader from '../Common/ImageFader'; import PageTitle from '../Common/PageTitle'; import LanguagePicker from '../Layout/LanguagePicker'; import PlexLoginButton from '../PlexLoginButton'; import Transition from '../Transition'; import LocalLogin from './LocalLogin'; const messages = defineMessages({ signin: 'Sign In', signinheader: 'Sign in to continue', signinwithplex: 'Use your Plex account', signinwithoverseerr: 'Use your {applicationTitle} 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(); const settings = useSettings(); // Effect that is triggered when the `authToken` comes back from the Plex OAuth // We take the token and attempt to sign in. 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/plex', { authToken }); if (response.data?.id) { 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]); const { data: backdrops } = useSWR('/api/v1/backdrops', { refreshInterval: 0, refreshWhenHidden: false, revalidateOnFocus: false, }); return (
`https://www.themoviedb.org/t/p/original${backdrop}` ) ?? [] } />
Logo

{intl.formatMessage(messages.signinheader)}

<>

{error}

{({ openIndexes, handleClick, AccordionContent }) => ( <>
setAuthToken(authToken)} />
{settings.currentSettings.localLogin && (
)} )}
); }; export default Login;