[PUI] Fix global login (#6287)

* Global login PUI -> CUI
Fixes #6285

* ensure session is always set

* Check if user is already logged in CUI->PUI

* reduce diff
This commit is contained in:
Matthias Mair 2024-01-19 00:57:00 +00:00 committed by GitHub
parent a0b595de6e
commit 914743627b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 2 deletions

View File

@ -3,6 +3,7 @@
import datetime
import logging
from django.contrib.auth import get_user, login
from django.contrib.auth.models import Group, User
from django.urls import include, path, re_path
@ -242,6 +243,10 @@ class GetAuthToken(APIView):
"Created new API token for user '%s' (name='%s')", user.username, name
)
# Ensure that the users session is logged in (PUI -> CUI login)
if not get_user(request).is_authenticated:
login(request, user)
return Response(data)
else:

View File

@ -120,7 +120,11 @@ export function handleReset(navigate: any, values: { email: string }) {
/**
* Check login state, and redirect the user as required
*/
export function checkLoginState(navigate: any, redirect?: string) {
export function checkLoginState(
navigate: any,
redirect?: string,
no_redirect?: boolean
) {
api
.get(apiUrl(ApiPaths.user_token), {
timeout: 2000,
@ -144,6 +148,6 @@ export function checkLoginState(navigate: any, redirect?: string) {
}
})
.catch(() => {
navigate('/login');
if (!no_redirect) navigate('/login');
});
}

View File

@ -2,12 +2,14 @@ import { t } from '@lingui/macro';
import { Center, Container } from '@mantine/core';
import { useToggle } from '@mantine/hooks';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { setApiDefaults } from '../../App';
import { AuthFormOptions } from '../../components/forms/AuthFormOptions';
import { AuthenticationForm } from '../../components/forms/AuthenticationForm';
import { InstanceOptions } from '../../components/forms/InstanceOptions';
import { defaultHostKey } from '../../defaults/defaultHostList';
import { checkLoginState } from '../../functions/auth';
import { useServerApiState } from '../../states/ApiState';
import { useLocalState } from '../../states/LocalState';
@ -24,6 +26,7 @@ export default function Login() {
const hostname =
hostList[hostKey] === undefined ? t`No selection` : hostList[hostKey]?.name;
const [hostEdit, setHostEdit] = useToggle([false, true] as const);
const navigate = useNavigate();
// Data manipulation functions
function ChangeHost(newHost: string): void {
@ -37,6 +40,9 @@ export default function Login() {
if (hostKey === '') {
ChangeHost(defaultHostKey);
}
// check if user is logged in in PUI
checkLoginState(navigate, undefined, true);
}, []);
// Fetch server data on mount if no server data is present
useEffect(() => {