Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Infinited loop with login #2039

Merged
Merged
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions xinference/web/ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ function App() {
})
} else {
res.json().then((data) => {
if (!data.auth && cookie.token !== 'no_auth') {
if (!data.auth) {
setCookie('token', 'no_auth', { path: '/' })
} else if (data.auth && !sessionStorage.getItem('token')) {
sessionStorage.setItem('token', 'no_auth')
} else if (
data.auth &&
sessionStorage.getItem('token') === 'no_auth'
) {
removeCookie('token', { path: '/' })
} else if (!data.auth && sessionStorage.getItem('token')) {
sessionStorage.removeItem('token')
}
sessionStorage.setItem('auth', String(data.auth)) // sessionStorage only can set string value
})
}
})
}, [])
}, [cookie])

const handleClose = (event, reason) => {
if (reason === 'clickaway') {
Expand Down
4 changes: 3 additions & 1 deletion xinference/web/ui/src/components/Title.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Title = ({ title }) => {
const handleLogout = () => {
removeCookie('token', { path: '/' })
sessionStorage.removeItem('token')
sessionStorage.removeItem('auth')
sessionStorage.removeItem('modelType')
sessionStorage.removeItem('lastActiveUrl')
sessionStorage.removeItem('runningModelType')
Expand All @@ -31,7 +32,8 @@ const Title = ({ title }) => {
>
{title}
</Typography>
{isValidBearerToken(cookie.token) && (
{(isValidBearerToken(cookie.token) ||
isValidBearerToken(sessionStorage.getItem('token'))) && (
<Button
variant="outlined"
size="large"
Expand Down
8 changes: 8 additions & 0 deletions xinference/web/ui/src/components/authAlertDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export default function AuthAlertDialog() {

const handleAuthStatus = () => {
const status = localStorage.getItem('authStatus')
if (status === '401') {
removeCookie('token', { path: '/' })
localStorage.removeItem('authStatus')
sessionStorage.removeItem('token')
navigate('/login', { replace: true })
return
}
if (status) {
setAuthStatus(status)
} else {
Expand All @@ -41,6 +48,7 @@ export default function AuthAlertDialog() {
setAuthStatus('')
if (code === '401') {
removeCookie('token', { path: '/' })
sessionStorage.removeItem('token')
navigate('/login', { replace: true })
}
}
Expand Down
20 changes: 16 additions & 4 deletions xinference/web/ui/src/components/fetchWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const fetchWrapper = {
'Content-Type': 'application/json',
...config.headers,
}
if (cookies.get('token') !== 'no_auth') {
if (
cookies.get('token') !== 'no_auth' &&
sessionStorage.getItem('token') !== 'no_auth'
) {
headers.Authorization = 'Bearer ' + sessionStorage.getItem('token')
}
const response = await fetch(url, {
Expand All @@ -40,7 +43,10 @@ const fetchWrapper = {
'Content-Type': 'application/json',
...config.headers,
}
if (cookies.get('token') !== 'no_auth') {
if (
cookies.get('token') !== 'no_auth' &&
sessionStorage.getItem('token') !== 'no_auth'
) {
headers.Authorization = 'Bearer ' + sessionStorage.getItem('token')
}
const response = await fetch(url, {
Expand All @@ -59,7 +65,10 @@ const fetchWrapper = {
'Content-Type': 'application/json',
...config.headers,
}
if (cookies.get('token') !== 'no_auth') {
if (
cookies.get('token') !== 'no_auth' &&
sessionStorage.getItem('token') !== 'no_auth'
) {
headers.Authorization = 'Bearer ' + sessionStorage.getItem('token')
}
const response = await fetch(url, {
Expand All @@ -78,7 +87,10 @@ const fetchWrapper = {
'Content-Type': 'application/json',
...config.headers,
}
if (cookies.get('token') !== 'no_auth') {
if (
cookies.get('token') !== 'no_auth' &&
sessionStorage.getItem('token') !== 'no_auth'
) {
headers.Authorization = 'Bearer ' + sessionStorage.getItem('token')
}
const response = await fetch(url, {
Expand Down
5 changes: 4 additions & 1 deletion xinference/web/ui/src/components/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const cookies = new Cookies()

const updateOptions = (url, options) => {
const update = { ...options }
if (cookies.get('token') !== 'no_auth') {
if (
cookies.get('token') !== 'no_auth' &&
sessionStorage.getItem('token') !== 'no_auth'
) {
update.headers = {
...update.headers,
Authorization: 'Bearer ' + sessionStorage.getItem('token'),
Expand Down
1 change: 1 addition & 0 deletions xinference/web/ui/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const LoginAuth = () => {
if (res.ok) {
res.json().then((data) => {
setAuthority(data.auth)
sessionStorage.setItem('auth', String(data.auth)) // sessionStorage only can set string value
})
}
})
Expand Down
7 changes: 4 additions & 3 deletions xinference/web/ui/src/scenes/cluster_info/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useNavigate } from 'react-router-dom'
import { ApiContext } from '../../components/apiContext'
import TableTitle from '../../components/tableTitle'
import Title from '../../components/Title'
import { isValidBearerToken } from '../../components/utils'
import NodeInfo from './nodeInfo'

const ClusterInfo = () => {
Expand All @@ -17,9 +18,9 @@ const ClusterInfo = () => {

useEffect(() => {
if (
cookie.token === '' ||
cookie.token === undefined ||
(cookie.token !== 'no_auth' && !sessionStorage.getItem('token'))
sessionStorage.getItem('auth') === 'true' &&
!isValidBearerToken(sessionStorage.getItem('token')) &&
!isValidBearerToken(cookie.token)
) {
navigate('/login', { replace: true })
}
Expand Down
12 changes: 6 additions & 6 deletions xinference/web/ui/src/scenes/launch_model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ApiContext } from '../../components/apiContext'
import ErrorMessageSnackBar from '../../components/errorMessageSnackBar'
import fetchWrapper from '../../components/fetchWrapper'
import Title from '../../components/Title'
import { isValidBearerToken } from '../../components/utils'
import LaunchCustom from './launchCustom'
import LaunchLLM from './launchLLM'
import LaunchModelComponent from './LaunchModelComponent'
Expand All @@ -34,13 +35,12 @@ const LaunchModel = () => {
}

useEffect(() => {
if (cookie.token === '' || cookie.token === undefined) {
if (
sessionStorage.getItem('auth') === 'true' &&
!isValidBearerToken(sessionStorage.getItem('token')) &&
!isValidBearerToken(cookie.token)
) {
navigate('/login', { replace: true })
return
}
if (cookie.token !== 'no_auth' && !sessionStorage.getItem('token')) {
navigate('/login', { replace: true })
return
}

if (gpuAvailable === -1) {
Expand Down
12 changes: 6 additions & 6 deletions xinference/web/ui/src/scenes/register_model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useNavigate } from 'react-router-dom'

import ErrorMessageSnackBar from '../../components/errorMessageSnackBar'
import Title from '../../components/Title'
import { isValidBearerToken } from '../../components/utils'
import RegisterModelComponent from './registerModel'

const RegisterModel = () => {
Expand All @@ -18,13 +19,12 @@ const RegisterModel = () => {
const navigate = useNavigate()

useEffect(() => {
if (cookie.token === '' || cookie.token === undefined) {
if (
sessionStorage.getItem('auth') === 'true' &&
!isValidBearerToken(sessionStorage.getItem('token')) &&
!isValidBearerToken(cookie.token)
) {
navigate('/login', { replace: true })
return
}
if (cookie.token !== 'no_auth' && !sessionStorage.getItem('token')) {
navigate('/login', { replace: true })
return
}
}, [cookie.token])

Expand Down
11 changes: 6 additions & 5 deletions xinference/web/ui/src/scenes/register_model/registerModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useNavigate, useParams } from 'react-router-dom'
import { ApiContext } from '../../components/apiContext'
import CopyComponent from '../../components/copyComponent/copyComponent'
import fetchWrapper from '../../components/fetchWrapper'
import { isValidBearerToken } from '../../components/utils'
import AddControlnet from './components/addControlnet'
import AddModelSpecs from './components/addModelSpecs'
import languages from './data/languages'
Expand Down Expand Up @@ -208,11 +209,11 @@ const RegisterModelComponent = ({ modelType, customData }) => {
}, [model_name])

useEffect(() => {
if (cookie.token === '' || cookie.token === undefined) {
navigate('/login', { replace: true })
return
}
if (cookie.token !== 'no_auth' && !sessionStorage.getItem('token')) {
if (
sessionStorage.getItem('auth') === 'true' &&
!isValidBearerToken(sessionStorage.getItem('token')) &&
!isValidBearerToken(cookie.token)
) {
navigate('/login', { replace: true })
return
}
Expand Down
11 changes: 6 additions & 5 deletions xinference/web/ui/src/scenes/running_models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ErrorMessageSnackBar from '../../components/errorMessageSnackBar'
import fetcher from '../../components/fetcher'
import fetchWrapper from '../../components/fetchWrapper'
import Title from '../../components/Title'
import { isValidBearerToken } from '../../components/utils'

const RunningModels = () => {
const [tabValue, setTabValue] = React.useState(
Expand All @@ -37,11 +38,11 @@ const RunningModels = () => {
}

const update = (isCallingApi) => {
if (cookie.token === '' || cookie.token === undefined) {
navigate('/login', { replace: true })
return
}
if (cookie.token !== 'no_auth' && !sessionStorage.getItem('token')) {
if (
sessionStorage.getItem('auth') === 'true' &&
!isValidBearerToken(sessionStorage.getItem('token')) &&
!isValidBearerToken(cookie.token)
) {
navigate('/login', { replace: true })
return
}
Expand Down
Loading