mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
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:
parent
3a40f29aa5
commit
88adc02f74
@ -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) => {
|
||||
|
||||
70
ui/src/layout/Login.test.jsx
Normal file
70
ui/src/layout/Login.test.jsx
Normal 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()
|
||||
})
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user