import React, { useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import Button from '../Common/Button'; import { Field, Form, Formik } from 'formik'; import * as Yup from 'yup'; import axios from 'axios'; import { useToasts } from 'react-toast-notifications'; import useSettings from '../../hooks/useSettings'; import AddEmailModal from './AddEmailModal'; const messages = defineMessages({ username: 'Username', password: 'Password', host: 'Jellyfin URL', email: 'Email', validationhostrequired: 'Jellyfin URL required', validationhostformat: 'Valid URL required', validationemailrequired: 'Email required', validationemailformat: 'Valid email required', validationusernamerequired: 'Username required', validationpasswordrequired: 'Password required', loginerror: 'Something went wrong while trying to sign in.', credentialerror: 'The username or password is incorrect.', signingin: 'Signing in…', signin: 'Sign In', initialsigningin: 'Connecting…', initialsignin: 'Connect', forgotpassword: 'Forgot Password?', }); interface JellyfinLoginProps { revalidate: () => void; initial?: boolean; } const JellyfinLogin: React.FC = ({ revalidate, initial, }) => { const [requiresEmail, setRequiresEmail] = useState(0); const [username, setUsername] = useState(); const [password, setPassword] = useState(); const toasts = useToasts(); const intl = useIntl(); const settings = useSettings(); if (initial) { const LoginSchema = Yup.object().shape({ host: Yup.string() .url(intl.formatMessage(messages.validationhostformat)) .required(intl.formatMessage(messages.validationhostrequired)), email: Yup.string() .email(intl.formatMessage(messages.validationemailformat)) .required(intl.formatMessage(messages.validationemailrequired)), username: Yup.string().required( intl.formatMessage(messages.validationusernamerequired) ), password: Yup.string().required( intl.formatMessage(messages.validationpasswordrequired) ), }); return ( { try { await axios.post('/api/v1/auth/jellyfin', { username: values.username, password: values.password, hostname: values.host, email: values.email, }); } catch (e) { toasts.addToast( intl.formatMessage( e.message == 'Request failed with status code 401' ? messages.credentialerror : messages.loginerror ), { autoDismiss: true, appearance: 'error', } ); } finally { revalidate(); } }} > {({ errors, touched, isSubmitting, isValid }) => (
{errors.host && touched.host && (
{errors.host}
)}
{errors.email && touched.email && (
{errors.email}
)}
{errors.username && touched.username && (
{errors.username}
)}
{errors.password && touched.password && (
{errors.password}
)}
)}
); } else { const LoginSchema = Yup.object().shape({ username: Yup.string().required( intl.formatMessage(messages.validationusernamerequired) ), password: Yup.string().required( intl.formatMessage(messages.validationpasswordrequired) ), }); return (
{requiresEmail == 1 && ( setRequiresEmail(0)} > )} { try { await axios.post('/api/v1/auth/jellyfin', { username: values.username, password: values.password, }); } catch (e) { if (e.message === 'Request failed with status code 406') { setUsername(values.username); setPassword(values.password); setRequiresEmail(1); } else { toasts.addToast( intl.formatMessage( e.message == 'Request failed with status code 401' ? messages.credentialerror : messages.loginerror ), { autoDismiss: true, appearance: 'error', } ); } } finally { revalidate(); } }} > {({ errors, touched, isSubmitting, isValid }) => { return ( <>
{errors.username && touched.username && (
{errors.username}
)}
{errors.password && touched.password && (
{errors.password}
)}
); }}
); } }; export default JellyfinLogin;