From 914743627b4c6584205ebd2cd38e95e8f475bf1a Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Fri, 19 Jan 2024 00:57:00 +0000 Subject: [PATCH] [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 --- InvenTree/users/api.py | 5 +++++ src/frontend/src/functions/auth.tsx | 8 ++++++-- src/frontend/src/pages/Auth/Login.tsx | 6 ++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/InvenTree/users/api.py b/InvenTree/users/api.py index 987eeb7c25..8343962fdd 100644 --- a/InvenTree/users/api.py +++ b/InvenTree/users/api.py @@ -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: diff --git a/src/frontend/src/functions/auth.tsx b/src/frontend/src/functions/auth.tsx index c4c341f5de..7039ea06d6 100644 --- a/src/frontend/src/functions/auth.tsx +++ b/src/frontend/src/functions/auth.tsx @@ -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'); }); } diff --git a/src/frontend/src/pages/Auth/Login.tsx b/src/frontend/src/pages/Auth/Login.tsx index f63b8ebbd1..00abb1f5b0 100644 --- a/src/frontend/src/pages/Auth/Login.tsx +++ b/src/frontend/src/pages/Auth/Login.tsx @@ -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(() => {