import React, { useEffect, useState } from 'react'; import { useUser } from '../../hooks/useUser'; import PlexLoginButton from '../PlexLoginButton'; import JellyfinLogin from '../Login/JellyfinLogin'; import axios from 'axios'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import Accordion from '../Common/Accordion'; import { MediaServerType } from '../../../server/constants/server'; const messages = defineMessages({ welcome: 'Welcome to Overseerr', signinMessage: 'Get started by signing in', signinWithJellyfin: 'Use your {mediaServerName} account', signinWithPlex: 'Use your Plex account', }); interface LoginWithMediaServerProps { onComplete: (onComplete: MediaServerType) => void; } const SetupLogin: React.FC = ({ onComplete }) => { const [authToken, setAuthToken] = useState(undefined); const [mediaServerType, setMediaServerType] = useState( MediaServerType.NOT_CONFIGURED ); const { user, revalidate } = useUser(); const intl = useIntl(); // 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 _shouid_ come back with a valid user. useEffect(() => { const login = async () => { const response = await axios.post('/api/v1/auth/plex', { authToken: authToken, }); if (response.data?.email) { revalidate(); } }; if (authToken && mediaServerType == MediaServerType.PLEX) { login(); } }, [authToken, mediaServerType, revalidate]); useEffect(() => { if (user) { onComplete(mediaServerType); } }, [user, mediaServerType, onComplete]); return (
{({ openIndexes, handleClick, AccordionContent }) => ( <>
{ setMediaServerType(MediaServerType.PLEX); setAuthToken(authToken); }} />
)}
); }; export default SetupLogin;