diff --git a/ui/src/layout/Login.jsx b/ui/src/layout/Login.jsx index a7763cff3..9e79d3c45 100644 --- a/ui/src/layout/Login.jsx +++ b/ui/src/layout/Login.jsx @@ -1,7 +1,8 @@ -import React, { useState, useCallback } from 'react' +import React, { useState, useCallback, useEffect } from 'react' import PropTypes from 'prop-types' import { Field, Form } from 'react-final-form' import { useDispatch } from 'react-redux' +import { useHistory } from 'react-router-dom' import Button from '@material-ui/core/Button' import Card from '@material-ui/core/Card' import CardActions from '@material-ui/core/CardActions' @@ -11,6 +12,7 @@ import TextField from '@material-ui/core/TextField' import { ThemeProvider, makeStyles } from '@material-ui/core/styles' import { createMuiTheme, + useCheckAuth, useLogin, useNotify, useTranslate, @@ -322,6 +324,16 @@ const Login = ({ location }) => { const notify = useNotify() const login = useLogin() const dispatch = useDispatch() + const checkAuth = useCheckAuth() + const history = useHistory() + + // Signed-in users opening #/login go straight to the app; + // replace keeps Back from bouncing. + useEffect(() => { + checkAuth({}, false) + .then(() => history.replace('/')) + .catch(() => {}) + }, [checkAuth, history]) const handleSubmit = useCallback( (auth) => { diff --git a/ui/src/layout/Login.test.jsx b/ui/src/layout/Login.test.jsx new file mode 100644 index 000000000..d2e2e5fc2 --- /dev/null +++ b/ui/src/layout/Login.test.jsx @@ -0,0 +1,70 @@ +import React from 'react' +import { render, screen, waitFor } from '@testing-library/react' +import { describe, it, beforeEach, vi } from 'vitest' +import { Provider } from 'react-redux' +import { createStore } from 'redux' +import { createMuiTheme } from '@material-ui/core/styles' +import Login from './Login' + +const mocks = vi.hoisted(() => ({ + checkAuth: vi.fn(), + push: vi.fn(), + replace: vi.fn(), +})) + +vi.mock('react-admin', () => ({ + createMuiTheme: (theme) => theme, + useCheckAuth: () => mocks.checkAuth, + useLogin: () => vi.fn(() => Promise.resolve()), + useNotify: () => vi.fn(), + useTranslate: () => (x) => x, + useVersion: () => 1, +})) + +vi.mock('react-router-dom', () => ({ + useHistory: () => ({ push: mocks.push, replace: mocks.replace }), +})) + +vi.mock('../themes/useCurrentTheme', () => ({ + default: () => createMuiTheme({}), +})) + +vi.mock('./Notification', () => ({ + default: () =>
, +})) + +const renderLogin = () => + render( + ({}))}> + + , + ) + +describe('', () => { + beforeEach(() => { + mocks.checkAuth.mockReset() + mocks.push.mockReset() + mocks.replace.mockReset() + }) + + it('replaces the login page with the home page when already signed in', async () => { + mocks.checkAuth.mockResolvedValue() + + renderLogin() + + await waitFor(() => expect(mocks.replace).toHaveBeenCalledWith('/')) + expect(mocks.checkAuth).toHaveBeenCalledWith({}, false) + expect(mocks.push).not.toHaveBeenCalled() + }) + + it('shows the login form when the user is not signed in', async () => { + mocks.checkAuth.mockRejectedValue() + + renderLogin() + + await waitFor(() => expect(mocks.checkAuth).toHaveBeenCalledWith({}, false)) + expect(screen.getByText('ra.auth.sign_in')).toBeInTheDocument() + expect(mocks.replace).not.toHaveBeenCalled() + expect(mocks.push).not.toHaveBeenCalled() + }) +})