Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use of same Login/Register Component on User and Admin #1493

5 changes: 2 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import MemberDetail from 'screens/MemberDetail/MemberDetail';
import Loader from 'components/Loader/Loader';

// User Portal Components
import UserLoginPage from 'screens/UserPortal/UserLoginPage/UserLoginPage';
import Organizations from 'screens/UserPortal/Organizations/Organizations';
import Home from 'screens/UserPortal/Home/Home';
import People from 'screens/UserPortal/People/People';
Expand Down Expand Up @@ -97,7 +96,7 @@ function app(): JSX.Element {
return (
<>
<Switch>
<Route exact path="/" component={LoginPage} />
<Route exact path="/" component={() => <LoginPage role="admin" />} />
<SecuredRoute path="/orgdash" component={OrganizationDashboard} />
<SecuredRoute path="/orgpeople" component={OrganizationPeople} />
<SecuredRoute path="/orglist" component={OrgList} />
Expand All @@ -115,7 +114,7 @@ function app(): JSX.Element {
<Route exact path="/forgotPassword" component={ForgotPassword} />

{/* User Portal Routes */}
<Route exact path="/user" component={UserLoginPage} />
<Route exact path="/user" component={LoginPage} />
<SecuredRouteForUser
path="/user/organizations"
component={Organizations}
Expand Down
36 changes: 18 additions & 18 deletions src/screens/LoginPage/LoginPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('Talawa-API server fetch check', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand All @@ -137,7 +137,7 @@ describe('Talawa-API server fetch check', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand All @@ -158,7 +158,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand All @@ -185,7 +185,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -274,7 +274,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -317,7 +317,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -360,7 +360,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -400,7 +400,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand All @@ -427,7 +427,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand All @@ -453,7 +453,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -482,7 +482,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -513,7 +513,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -544,7 +544,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand All @@ -565,7 +565,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -594,7 +594,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -623,7 +623,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down Expand Up @@ -652,7 +652,7 @@ describe('Testing Login Page Screen', () => {
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
<LoginPage role="admin" />
</I18nextProvider>
</Provider>
</BrowserRouter>
Expand Down
78 changes: 51 additions & 27 deletions src/screens/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ import Loader from 'components/Loader/Loader';
import { errorHandler } from 'utils/errorHandler';
import styles from './LoginPage.module.css';
import EmailOutlinedIcon from '@mui/icons-material/EmailOutlined';
interface InterfaceComponentProps {
role: string;
}

function loginPage(): JSX.Element {
function loginPage({ role = 'user' }: InterfaceComponentProps): JSX.Element {
const { t } = useTranslation('translation', { keyPrefix: 'loginPage' });
const history = useHistory();

Expand Down Expand Up @@ -113,7 +116,7 @@ function loginPage(): JSX.Element {
useEffect(() => {
const isLoggedIn = localStorage.getItem('IsLoggedIn');
if (isLoggedIn == 'TRUE') {
history.push('/orglist');
history.push(role === 'admin' ? '/orglist' : '/user/organizations');
}
setComponentLoader(false);
}, []);
Expand Down Expand Up @@ -164,6 +167,17 @@ function loginPage(): JSX.Element {
}
};

const validatePassword = (password: string): boolean => {
const lengthCheck = new RegExp('^.{6,}$');
return (
lengthCheck.test(password) &&
passwordValidationRegExp.lowercaseCharRegExp.test(password) &&
passwordValidationRegExp.uppercaseCharRegExp.test(password) &&
passwordValidationRegExp.numericalValueRegExp.test(password) &&
passwordValidationRegExp.specialCharRegExp.test(password)
);
};

const signupLink = async (e: ChangeEvent<HTMLFormElement>): Promise<void> => {
e.preventDefault();

Expand All @@ -182,17 +196,6 @@ function loginPage(): JSX.Element {
const isValidatedString = (value: string): boolean =>
/^[a-zA-Z]+$/.test(value);

const validatePassword = (password: string): boolean => {
const lengthCheck = new RegExp('^.{6,}$');
return (
lengthCheck.test(password) &&
passwordValidationRegExp.lowercaseCharRegExp.test(password) &&
passwordValidationRegExp.uppercaseCharRegExp.test(password) &&
passwordValidationRegExp.numericalValueRegExp.test(password) &&
passwordValidationRegExp.specialCharRegExp.test(password)
);
};

if (
isValidatedString(signfirstName) &&
isValidatedString(signlastName) &&
Expand All @@ -216,16 +219,24 @@ function loginPage(): JSX.Element {
/* istanbul ignore next */
if (signUpData) {
toast.success(
'Successfully Registered. Please wait until you will be approved.'
role === 'admin'
? 'Successfully Registered. Please wait until you will be approved.'
: 'Successfully registered. Please wait for admin to approve your request.'
);

setShowTab('LOGIN');
setSignFormState({
signfirstName: '',
signlastName: '',
signEmail: '',
signPassword: '',
cPassword: '',
});
setShowAlert({
lowercaseChar: true,
uppercaseChar: true,
numericValue: true,
specialChar: true,
});
}
} catch (error: any) {
/* istanbul ignore next */
Expand Down Expand Up @@ -262,6 +273,11 @@ function loginPage(): JSX.Element {
toast.error(t('Please_check_the_captcha'));
return;
}
/* istanbul ignore next */
if (!validatePassword(formState.password)) {
toast.warn(t('password_invalid'));
return;
}

try {
const { data: loginData } = await login({
Expand All @@ -273,21 +289,29 @@ function loginPage(): JSX.Element {

/* istanbul ignore next */
if (loginData) {
if (
loginData.login.user.userType === 'SUPERADMIN' ||
(loginData.login.user.userType === 'ADMIN' &&
loginData.login.user.adminApproved === true)
) {
if (role === 'admin') {
if (
loginData.login.user.userType === 'SUPERADMIN' ||
(loginData.login.user.userType === 'ADMIN' &&
loginData.login.user.adminApproved === true)
) {
localStorage.setItem('token', loginData.login.accessToken);
localStorage.setItem('refreshToken', loginData.login.refreshToken);
localStorage.setItem('id', loginData.login.user._id);
localStorage.setItem('UserType', loginData.login.user.userType);
localStorage.setItem('IsLoggedIn', 'TRUE');
} else {
toast.warn(t('notAuthorised'));
}
} else {
localStorage.setItem('token', loginData.login.accessToken);
localStorage.setItem('userId', loginData.login.user._id);
localStorage.setItem('refreshToken', loginData.login.refreshToken);
localStorage.setItem('id', loginData.login.user._id);
localStorage.setItem('IsLoggedIn', 'TRUE');
localStorage.setItem('UserType', loginData.login.user.userType);
if (localStorage.getItem('IsLoggedIn') == 'TRUE') {
history.push('/orglist');
}
} else {
toast.warn(t('notAuthorised'));
navigator.clipboard.writeText('');
}
if (localStorage.getItem('IsLoggedIn') == 'TRUE') {
history.push(role === 'admin' ? '/orglist' : '/user/organizations');
}
} else {
toast.warn(t('notFound'));
Expand Down
Loading
Loading