import Button from '@app/components/Common/Button'; import useSettings from '@app/hooks/useSettings'; import axios from 'axios'; import { Field, Form, Formik } from 'formik'; import getConfig from 'next/config'; import type React from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import * as Yup from 'yup'; const messages = defineMessages({ username: 'Username', password: 'Password', host: '{mediaServerName} URL', email: 'Email', emailtooltip: 'This can be any valid email. It doesn\'t need to come from your {mediaServerName} instance.', validationhostrequired: '{mediaServerName} 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 toasts = useToasts(); const intl = useIntl(); const settings = useSettings(); const { publicRuntimeConfig } = getConfig(); if (initial) { const LoginSchema = Yup.object().shape({ host: Yup.string() .matches( /^(?:(?:(?:https?):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/, intl.formatMessage(messages.validationhostformat) ) .required( intl.formatMessage(messages.validationhostrequired, { mediaServerName: publicRuntimeConfig.JELLYFIN_TYPE == 'emby' ? 'Emby' : 'Jellyfin', }) ), email: Yup.string() .email(intl.formatMessage(messages.validationemailformat)) .required(intl.formatMessage(messages.validationemailrequired)), username: Yup.string().required( intl.formatMessage(messages.validationusernamerequired) ), password: Yup.string(), }); const mediaServerFormatValues = { mediaServerName: publicRuntimeConfig.JELLYFIN_TYPE == 'emby' ? 'Emby' : 'Jellyfin', }; 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(), }); return (
{ try { await axios.post('/api/v1/auth/jellyfin', { username: values.username, password: values.password, email: values.username, }); } 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 }) => { return ( <>
{errors.username && touched.username && (
{errors.username}
)}
{errors.password && touched.password && (
{errors.password}
)}
); }}
); } }; export default JellyfinLogin;