From 88adc02f747c01ea9060b65f2e85a8bad3b0cdb3 Mon Sep 17 00:00:00 2001 From: Kris Anderson <1562427+kris-anderson@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:16:40 -0800 Subject: [PATCH] fix(ui): skip the login form when already signed in (#6459) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(ui): skip the login form when already signed in Opening #/login while signed in always showed the login form, so a bookmarked or browser-autocompleted login URL asked for the password on every visit although the stored session was still valid. Redirect to the app when checkAuth succeeds, as react-admin's default login page does. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Kris Anderson <1562427+kris-anderson@users.noreply.github.com> * fix(ui): replace the login history entry on redirect Pushing / left #/login one step back in history, so pressing Back landed on the login page, which redirected forward again and kept the user from returning to the previous page. Replace the entry instead. Raised in review by Codex. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Kris Anderson <1562427+kris-anderson@users.noreply.github.com> * chore(ui): shorten the login redirect comment Background moved here from the code comment: react-admin's default login page sends an already signed-in user to the app, and Navidrome's custom Login.jsx had dropped that check. Opening #/login from a bookmark or the browser's autocomplete therefore showed the password form although the stored session was still valid. The redirect uses replace so Back returns to the previous page instead of landing on #/login again. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Kris Anderson <1562427+kris-anderson@users.noreply.github.com> --------- Signed-off-by: Kris Anderson <1562427+kris-anderson@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 Co-authored-by: Deluan Quintão --- ui/src/layout/Login.jsx | 14 +++++++- ui/src/layout/Login.test.jsx | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 ui/src/layout/Login.test.jsx 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() + }) +})