fix(ui): skip the login form when already signed in (#6459)

* 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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
Co-authored-by: Deluan Quintão <deluan@navidrome.org>
This commit is contained in:
Kris Anderson 2026-11-13 17:16:40 -08:00 committed by GitHub
parent 3a40f29aa5
commit 88adc02f74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 1 deletions

View File

@ -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) => {

View File

@ -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: () => <div />,
}))
const renderLogin = () =>
render(
<Provider store={createStore(() => ({}))}>
<Login location={{}} />
</Provider>,
)
describe('<Login />', () => {
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()
})
})