Add authenticate function to controller

This commit is contained in:
jeffvli 2023-05-09 05:48:11 -07:00
parent 975c31635a
commit 63be8c8fb8
3 changed files with 50 additions and 33 deletions

View file

@ -5,12 +5,10 @@ import { useForm } from '@mantine/form';
import { useFocusTrap } from '@mantine/hooks';
import { closeAllModals } from '@mantine/modals';
import { nanoid } from 'nanoid/non-secure';
import { jellyfinApi } from '/@/renderer/api/jellyfin.api';
import { navidromeApi } from '/@/renderer/api/navidrome.api';
import { subsonicApi } from '/@/renderer/api/subsonic.api';
import { AuthenticationResponse } from '/@/renderer/api/types';
import { useAuthStore, useAuthStoreActions } from '/@/renderer/store';
import { ServerType } from '/@/renderer/types';
import { api } from '/@/renderer/api';
const SERVER_TYPES = [
{ label: 'Jellyfin', value: ServerType.JELLYFIN },
@ -18,12 +16,6 @@ const SERVER_TYPES = [
// { label: 'Subsonic', value: ServerType.SUBSONIC },
];
const AUTH_FUNCTIONS = {
[ServerType.JELLYFIN]: jellyfinApi.authenticate,
[ServerType.NAVIDROME]: navidromeApi.authenticate,
[ServerType.SUBSONIC]: subsonicApi.authenticate,
};
interface AddServerFormProps {
onCancel: () => void;
}
@ -48,7 +40,7 @@ export const AddServerForm = ({ onCancel }: AddServerFormProps) => {
const isSubmitDisabled = !form.values.name || !form.values.url || !form.values.username;
const handleSubmit = form.onSubmit(async (values) => {
const authFunction = AUTH_FUNCTIONS[values.type];
const authFunction = api.controller.authenticate;
if (!authFunction) {
return toast.error({ message: 'Selected server type is invalid' });
@ -56,11 +48,19 @@ export const AddServerForm = ({ onCancel }: AddServerFormProps) => {
try {
setIsLoading(true);
const data: AuthenticationResponse = await authFunction(values.url, {
legacy: values.legacyAuth,
password: values.password,
username: values.username,
});
const data: AuthenticationResponse | undefined = await authFunction(
values.url,
{
legacy: values.legacyAuth,
password: values.password,
username: values.username,
},
values.type,
);
if (!data) {
return toast.error({ message: 'Authentication failed' });
}
const serverItem = {
credential: data.credential,

View file

@ -5,12 +5,10 @@ import { useForm } from '@mantine/form';
import { useFocusTrap } from '@mantine/hooks';
import { closeAllModals } from '@mantine/modals';
import { RiInformationLine } from 'react-icons/ri';
import { jellyfinApi } from '/@/renderer/api/jellyfin.api';
import { navidromeApi } from '/@/renderer/api/navidrome.api';
import { subsonicApi } from '/@/renderer/api/subsonic.api';
import { AuthenticationResponse } from '/@/renderer/api/types';
import { useAuthStoreActions } from '/@/renderer/store';
import { ServerListItem, ServerType } from '/@/renderer/types';
import { api } from '/@/renderer/api';
interface EditServerFormProps {
isUpdate?: boolean;
@ -18,12 +16,6 @@ interface EditServerFormProps {
server: ServerListItem;
}
const AUTH_FUNCTIONS = {
[ServerType.JELLYFIN]: jellyfinApi.authenticate,
[ServerType.NAVIDROME]: navidromeApi.authenticate,
[ServerType.SUBSONIC]: subsonicApi.authenticate,
};
const ModifiedFieldIndicator = () => {
return (
<Tooltip label="Field has been modified">
@ -53,7 +45,7 @@ export const EditServerForm = ({ isUpdate, server, onCancel }: EditServerFormPro
const isSubsonic = form.values.type === ServerType.SUBSONIC;
const handleSubmit = form.onSubmit(async (values) => {
const authFunction = AUTH_FUNCTIONS[values.type];
const authFunction = api.controller.authenticate;
if (!authFunction) {
return toast.error({ message: 'Selected server type is invalid' });
@ -61,11 +53,19 @@ export const EditServerForm = ({ isUpdate, server, onCancel }: EditServerFormPro
try {
setIsLoading(true);
const data: AuthenticationResponse = await authFunction(values.url, {
legacy: values.legacyAuth,
password: values.password,
username: values.username,
});
const data: AuthenticationResponse | undefined = await authFunction(
values.url,
{
legacy: values.legacyAuth,
password: values.password,
username: values.username,
},
values.type,
);
if (!data) {
return toast.error({ message: 'Authentication failed' });
}
const serverItem = {
credential: data.credential,