UNPKG

@brightlayer-ui/react-auth-workflow

Version:

Re-usable workflow components for Authentication and Registration within Eaton applications.

49 lines (48 loc) 2.62 kB
/** * @packageDocumentation * @module AuthContextProvider */ import React, { useEffect } from 'react'; import { AuthContext } from './context.js'; import { I18nextProvider, useTranslation } from 'react-i18next'; import { i18nAuthInstance } from './i18nAuthInstance.js'; import { ErrorContext } from '../ErrorContext/index.js'; import { AuthDictionaries } from './AuthDictionaries/index.js'; import { SharedDictionaries } from '../SharedDictionaries/index.js'; const AuthContextProviderContent = (props) => { const { children, errorConfig, ...authContextProps } = props; const { t } = useTranslation(); const mergedErrorConfig = { t: t, title: 'bluiCommon:MESSAGES.ERROR', error: 'bluiAuth:LOGIN.INVALID_CREDENTIALS', ...errorConfig, dialogConfig: { dismissLabel: 'bluiCommon:ACTIONS.OKAY', ...(errorConfig?.dialogConfig ?? {}), }, }; return (React.createElement(AuthContext.Provider, { value: { ...authContextProps } }, React.createElement(ErrorContext.Provider, { value: mergedErrorConfig }, children))); }; export const AuthContextProvider = (props) => { const i18nInstance = props.i18n ?? i18nAuthInstance; const { language, i18n = i18nInstance, children, ...other } = props; if (props.i18n) { i18n.addResourceBundle('zh', 'bluiAuth', AuthDictionaries.chinese.translation, true, false); i18n.addResourceBundle('zh', 'bluiCommon', SharedDictionaries.chinese.translation, true, false); i18n.addResourceBundle('en', 'bluiAuth', AuthDictionaries.english.translation, true, false); i18n.addResourceBundle('en', 'bluiCommon', SharedDictionaries.english.translation, true, false); i18n.addResourceBundle('fr', 'bluiAuth', AuthDictionaries.french.translation, true, false); i18n.addResourceBundle('fr', 'bluiCommon', SharedDictionaries.french.translation, true, false); i18n.addResourceBundle('pt', 'bluiAuth', AuthDictionaries.portuguese.translation, true, false); i18n.addResourceBundle('pt', 'bluiCommon', SharedDictionaries.portuguese.translation, true, false); i18n.addResourceBundle('es', 'bluiAuth', AuthDictionaries.spanish.translation, true, false); i18n.addResourceBundle('es', 'bluiCommon', SharedDictionaries.spanish.translation, true, false); } useEffect(() => { void i18n.changeLanguage(language); }, [i18n, language]); return (React.createElement(I18nextProvider, { i18n: i18n }, React.createElement(AuthContextProviderContent, { ...other, language: language }, children))); };