diff --git a/public/locales/en/translations.json b/public/locales/en/translations.json index 7e1a3da2..7d9959e3 100644 --- a/public/locales/en/translations.json +++ b/public/locales/en/translations.json @@ -117,14 +117,15 @@ "Wrong username or password": "Wrong username or password", "Wrong host or username or password": "Wrong host or username or password", "Network connection is lost": "Network connection is lost", + "Too many failed attempts. Wait a few minutes and try again.": "Too many failed attempts. Wait a few minutes and try again.", + "The server did not answer as expected. Try again in a moment.": "The server did not answer as expected. Try again in a moment.", "Login failed": "Login failed", "Use Another Account": "Use another account", - "New Account title": "Welcome", - "New Account description": "Sign in to NethLink with your NethVoice CTI username and password.", "Account List description": "The accounts you have used on this computer.", "Back": "Back", "User not authorized for NethLink": "User not authorized for NethLink", "Sign in with SSO": "Sign in with SSO", + "SSO description": "Sign in to {{host}} with single sign-on (SSO).", "SSO login failed": "Single Sign-On failed, try again", "SSO user not enabled": "You signed in on the identity provider, but this user is not enabled to use the CTI. Contact your administrator.", "Generic error": "Generic error", @@ -136,9 +137,6 @@ "Enter the 6-digit code (OTP code) from your authenticator app. If you cannot access the app, you can use one recovery OTP code.": "Enter the 6-digit code (OTP code) from your authenticator app. If you cannot access the app, you can use one recovery OTP code.", "Digit": "Digit {{number}}" }, - "Continue": "Continue", - "Add account": "Add account", - "Host step description": "Enter the address of your NethVoice CTI.", "Credentials description": "Sign in to {{host}} with your NethVoice CTI username and password.", "Choose an account": "Choose an account", "Account details": "{{extension}} - {{host}}", diff --git a/public/locales/it/translations.json b/public/locales/it/translations.json index a71a3ea9..ec5e90bf 100644 --- a/public/locales/it/translations.json +++ b/public/locales/it/translations.json @@ -117,14 +117,15 @@ "Wrong username or password": "Nome utente o password errati", "Wrong host or username or password": "Host o Nome utente o password errati", "Network connection is lost": "Connessione di rete persa", + "Too many failed attempts. Wait a few minutes and try again.": "Troppi tentativi non riusciti. Attendi qualche minuto e riprova.", + "The server did not answer as expected. Try again in a moment.": "Il server non ha risposto come previsto. Riprova tra poco.", "Login failed": "Accesso fallito", "Use Another Account": "Usa un altro account", - "New Account title": "Ti diamo il benvenuto", - "New Account description": "Accedere a NethLink con il nome utente e la password di NethVoice CTI.", "Account List description": "Gli account che hai usato su questo computer.", "Back": "Indietro", "User not authorized for NethLink": "Utente non autorizzato per NethLink", "Sign in with SSO": "Accedi con SSO", + "SSO description": "Accedi a {{host}} con il single sign-on (SSO).", "SSO login failed": "Single Sign-On non riuscito, riprova", "SSO user not enabled": "Ti sei autenticato sull'identity provider, ma questo utente non è abilitato all'uso del CTI. Contatta l'amministratore.", "Generic error": "Errore generico", @@ -136,9 +137,6 @@ "Enter the 6-digit code (OTP code) from your authenticator app. If you cannot access the app, you can use one recovery OTP code.": "Inserisci il codice a 6 cifre (codice OTP) dalla tua app di autenticazione. Se non puoi accedere all'app, puoi utilizzare un codice OTP di recupero.", "Digit": "Cifra {{number}}" }, - "Continue": "Continua", - "Add account": "Aggiungi account", - "Host step description": "Inserisci l'indirizzo del tuo NethVoice CTI.", "Credentials description": "Accedi a {{host}} con nome utente e password di NethVoice CTI.", "Choose an account": "Scegli un account", "Account details": "{{extension}} - {{host}}", diff --git a/src/renderer/public/locales/en/translations.json b/src/renderer/public/locales/en/translations.json index 7e1a3da2..7d9959e3 100644 --- a/src/renderer/public/locales/en/translations.json +++ b/src/renderer/public/locales/en/translations.json @@ -117,14 +117,15 @@ "Wrong username or password": "Wrong username or password", "Wrong host or username or password": "Wrong host or username or password", "Network connection is lost": "Network connection is lost", + "Too many failed attempts. Wait a few minutes and try again.": "Too many failed attempts. Wait a few minutes and try again.", + "The server did not answer as expected. Try again in a moment.": "The server did not answer as expected. Try again in a moment.", "Login failed": "Login failed", "Use Another Account": "Use another account", - "New Account title": "Welcome", - "New Account description": "Sign in to NethLink with your NethVoice CTI username and password.", "Account List description": "The accounts you have used on this computer.", "Back": "Back", "User not authorized for NethLink": "User not authorized for NethLink", "Sign in with SSO": "Sign in with SSO", + "SSO description": "Sign in to {{host}} with single sign-on (SSO).", "SSO login failed": "Single Sign-On failed, try again", "SSO user not enabled": "You signed in on the identity provider, but this user is not enabled to use the CTI. Contact your administrator.", "Generic error": "Generic error", @@ -136,9 +137,6 @@ "Enter the 6-digit code (OTP code) from your authenticator app. If you cannot access the app, you can use one recovery OTP code.": "Enter the 6-digit code (OTP code) from your authenticator app. If you cannot access the app, you can use one recovery OTP code.", "Digit": "Digit {{number}}" }, - "Continue": "Continue", - "Add account": "Add account", - "Host step description": "Enter the address of your NethVoice CTI.", "Credentials description": "Sign in to {{host}} with your NethVoice CTI username and password.", "Choose an account": "Choose an account", "Account details": "{{extension}} - {{host}}", diff --git a/src/renderer/public/locales/it/translations.json b/src/renderer/public/locales/it/translations.json index a71a3ea9..ec5e90bf 100644 --- a/src/renderer/public/locales/it/translations.json +++ b/src/renderer/public/locales/it/translations.json @@ -117,14 +117,15 @@ "Wrong username or password": "Nome utente o password errati", "Wrong host or username or password": "Host o Nome utente o password errati", "Network connection is lost": "Connessione di rete persa", + "Too many failed attempts. Wait a few minutes and try again.": "Troppi tentativi non riusciti. Attendi qualche minuto e riprova.", + "The server did not answer as expected. Try again in a moment.": "Il server non ha risposto come previsto. Riprova tra poco.", "Login failed": "Accesso fallito", "Use Another Account": "Usa un altro account", - "New Account title": "Ti diamo il benvenuto", - "New Account description": "Accedere a NethLink con il nome utente e la password di NethVoice CTI.", "Account List description": "Gli account che hai usato su questo computer.", "Back": "Indietro", "User not authorized for NethLink": "Utente non autorizzato per NethLink", "Sign in with SSO": "Accedi con SSO", + "SSO description": "Accedi a {{host}} con il single sign-on (SSO).", "SSO login failed": "Single Sign-On non riuscito, riprova", "SSO user not enabled": "Ti sei autenticato sull'identity provider, ma questo utente non è abilitato all'uso del CTI. Contatta l'amministratore.", "Generic error": "Errore generico", @@ -136,9 +137,6 @@ "Enter the 6-digit code (OTP code) from your authenticator app. If you cannot access the app, you can use one recovery OTP code.": "Inserisci il codice a 6 cifre (codice OTP) dalla tua app di autenticazione. Se non puoi accedere all'app, puoi utilizzare un codice OTP di recupero.", "Digit": "Cifra {{number}}" }, - "Continue": "Continua", - "Add account": "Aggiungi account", - "Host step description": "Inserisci l'indirizzo del tuo NethVoice CTI.", "Credentials description": "Accedi a {{host}} con nome utente e password di NethVoice CTI.", "Choose an account": "Scegli un account", "Account details": "{{extension}} - {{host}}", diff --git a/src/renderer/src/assets/loginPageSpinner.svg b/src/renderer/src/assets/loginPageSpinner.svg deleted file mode 100644 index 797dd6a4..00000000 --- a/src/renderer/src/assets/loginPageSpinner.svg +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/src/renderer/src/components/pageComponents/login/LoginForm.tsx b/src/renderer/src/components/pageComponents/login/LoginForm.tsx index a27a03c9..10be9693 100644 --- a/src/renderer/src/components/pageComponents/login/LoginForm.tsx +++ b/src/renderer/src/components/pageComponents/login/LoginForm.tsx @@ -1,12 +1,13 @@ import * as z from 'zod' import { zodResolver } from '@hookform/resolvers/zod' -import { useForm, SubmitHandler, FieldErrors } from 'react-hook-form' +import { useForm, SubmitHandler } from 'react-hook-form' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faEye as EyeIcon, faEyeSlash as EyeSlashIcon, faCircleNotch, faCircleCheck, + faArrowRightToBracket, } from '@fortawesome/free-solid-svg-icons' import { t } from 'i18next' import { useEffect, useRef, useState } from 'react' @@ -30,13 +31,9 @@ import { requires2FA } from '@shared/utils/jwt' import { PasswordChangeForm } from '../password/PasswordChangeForm' export interface LoginFormProps { - onError: ( - formErrors: FieldErrors, - generalError: Error | undefined, - ) => void handleRefreshConnection: () => void } -export const LoginForm = ({ onError, handleRefreshConnection }) => { +export const LoginForm = ({ handleRefreshConnection }: LoginFormProps) => { const { NethVoiceAPI } = useNethVoiceAPI() const submitButtonRef = useRef(null) const [auth] = useSharedState('auth') @@ -67,6 +64,8 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { useRef<(res: { hostConfig?: HostConfig; error?: string }) => void>() const ssoResultHandlerRef = useRef<(res: { token?: string; error?: string }) => void>() + const submittingRef = useRef(false) + const hostConfigHostRef = useRef() const schema: z.ZodType = z.object({ host: z @@ -92,8 +91,10 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { }) useEffect(() => { - onError(errors, error) - }, [Object.keys(errors).length, error]) + if (!isLoading) { + submittingRef.current = false + } + }, [isLoading]) useEffect(() => { window.electron.receive(IPC_EVENTS.SET_HOST_CONFIG, (res) => @@ -198,26 +199,39 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { window.electron.send(IPC_EVENTS.GET_HOST_CONFIG, host) } - // step 1: read the host authentication capabilities, then show the proper - // credentials step (username/password or SSO button) - function handleHostNext(data: LoginData) { + function startLoading() { + submittingRef.current = true + setIsLoading(true) + } + + function submitNewAccount(data: LoginData) { const host = cleanHost(data.host) if (!host) { setError(() => new Error(t('Login.Wrong host or username or password')!)) return } - setIsLoading(true) + const applyHostConfig = (config: HostConfig) => { + if (config.authenticationMethod === 'password') { + submitCredentials({ ...data, host }) + } else { + setIsLoading(false) + setLoginStep('credentials') + } + } + if (hostConfig && hostConfigHostRef.current === host) { + applyHostConfig(hostConfig) + return + } + startLoading() setError(() => undefined) hostConfigHandlerRef.current = (res) => { - setIsLoading(false) if (res.hostConfig) { setValue('host', host) setHostConfig(res.hostConfig) - setLoginStep('credentials') - if (res.hostConfig.authenticationMethod === 'password') { - focus('username') - } + hostConfigHostRef.current = host + applyHostConfig(res.hostConfig) } else { + setIsLoading(false) Log.warning('LOGIN unable to read the host configuration', res.error) setError(() => new Error(t('Login.Network connection is lost')!)) } @@ -225,15 +239,36 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { window.electron.send(IPC_EVENTS.GET_HOST_CONFIG, host) } + function submitCredentials(data: LoginData) { + let valid = true + if (!isSavedAccount && !data.username?.trim()) { + setFieldError('username', { + message: `${t('Common.This field is required')}`, + }) + valid = false + } + if (!data.password?.trim()) { + setFieldError('password', { + message: `${t('Common.This field is required')}`, + }) + valid = false + } + if (valid) { + handleLogin(data) + } else { + setIsLoading(false) + } + } + // Single Sign-On: the main process runs the SSO flow in a dedicated // window and returns the minted JWT function handleSsoLogin() { - if (isLoading || !hostConfig?.ssoLoginUrl) return + if (submittingRef.current || !hostConfig?.ssoLoginUrl) return const host = selectedAccount && selectedAccount !== NEW_ACCOUNT ? selectedAccount.host : getValues('host') - setIsLoading(true) + startLoading() setError(() => undefined) ssoResultHandlerRef.current = async (res) => { if (!res.token) { @@ -278,7 +313,7 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { if (!isLoading) { const e: Error | undefined = undefined setError(() => e) - setIsLoading(true) + startLoading() const hostReg = /^(?:(https?:\/\/)?([^:/$]{1,})(?::(\d{1,}))?(?:($|\/(?:[^?#]{0,}))?((?:\?(?:[^#]{1,}))?)?(?:(#(?:.*)?)?|$)))$/g const res = hostReg.exec(data.host) @@ -327,6 +362,24 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { setError(() => new Error(t('Login.Wrong username or password')!)) } else if (error.message === 'Network connection lost') { setError(() => new Error(t('Login.Network connection is lost')!)) + } else if (error.message === 'Too many failed attempts') { + setError( + () => + new Error( + t( + 'Login.Too many failed attempts. Wait a few minutes and try again.', + )!, + ), + ) + } else if (error.message === 'Server error') { + setError( + () => + new Error( + t( + 'Login.The server did not answer as expected. Try again in a moment.', + )!, + ), + ) } else if (error.message === 'Unauthorized') { setError( () => new Error(t('Login.Wrong host or username or password')!), @@ -396,28 +449,13 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { } const onSubmitForm: SubmitHandler = (data) => { - const isSavedAccount = !!( - selectedAccount && selectedAccount !== NEW_ACCOUNT - ) - if (!isSavedAccount && loginStep === 'host') { - handleHostNext(data) + if (submittingRef.current) { return } - let valid = true - if (!isSavedAccount && !data.username?.trim()) { - setFieldError('username', { - message: `${t('Common.This field is required')}`, - }) - valid = false - } - if (!data.password?.trim()) { - setFieldError('password', { - message: `${t('Common.This field is required')}`, - }) - valid = false - } - if (valid) { - handleLogin(data) + if (isSavedAccount) { + submitCredentials(data) + } else { + submitNewAccount(data) } } @@ -434,21 +472,10 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { } const isSavedAccount = !!(selectedAccount && selectedAccount !== NEW_ACCOUNT) - const isHostStep = !isSavedAccount && loginStep === 'host' // the host the user signs in to: the saved account's, or the one just entered const currentHost = isSavedAccount ? (selectedAccount as Account).host : getValues('host') - const formTitle = isHostStep - ? selectedAccount === NEW_ACCOUNT - ? t('Login.Add account') - : t('Login.New Account title') - : t('Login.Sign in') - const formDescription = isHostStep - ? t('Login.Host step description') - : currentHost - ? t('Login.Credentials description', { host: currentHost }) - : t('Login.New Account description') const authMethod = isSavedAccount ? (selectedAccount as Account).authenticationMethod || 'password' : hostConfig?.authenticationMethod || 'password' @@ -456,6 +483,30 @@ export const LoginForm = ({ onError, handleRefreshConnection }) => { isSsoMethod(authMethod) && (isSavedAccount || loginStep === 'credentials') const ssoButtonLabel = hostConfig?.ssoButtonLabel || (t('Login.Sign in with SSO') as string) + const showHeading = isSavedAccount || showSsoButton + const fieldLabelClassName = + 'block text-sm font-medium text-textSecondaryNeutral dark:text-textSecondaryNeutralDark' + const submitOnEnter = (e: React.KeyboardEvent) => { + if (e.key === 'Enter') { + e.preventDefault() + submitButtonRef.current?.focus() + handleSubmit(onSubmitForm)(e) + } + } + const loadingIcon = ( +