Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions packages/apps/esm-login-app/src/login/login.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useConnectivity,
useSession,
} from '@openmrs/esm-framework';
import { getSessionStore } from '@openmrs/esm-api';
import { type ConfigSchema } from '../config-schema';
import Logo from '../logo.component';
import Footer from '../footer.component';
Expand Down Expand Up @@ -39,14 +40,28 @@ const Login: React.FC = () => {
const usernameInputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (!user) {
if (loginProvider.type === 'oauth2') {
openmrsNavigate({ to: loginProvider.loginUrl });
} else if (!username && location.pathname === '/login/confirm') {
navigate('/login');
async function redirectIfLoggedIn() {
if (!user) {
if (loginProvider.type === 'oauth2') {
openmrsNavigate({ to: loginProvider.loginUrl });
} else if (!username && location.pathname === '/login/confirm') {
navigate('/login');
}
} else {
const session = getSessionStore().getState();
const locationSet = session?.session?.sessionLocation != null;

if (locationSet) {
const to = loginLinks?.loginSuccess || '/home';
openmrsNavigate({ to });
} else {
navigate('/login/location');
}
}
}
}, [username, navigate, location, user, loginProvider]);

redirectIfLoggedIn();
}, [user, loginProvider, username, location.pathname, navigate, loginLinks]);

useEffect(() => {
if (showPasswordOnSeparateScreen) {
Expand Down