mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	[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:
		@@ -3,6 +3,7 @@
 | 
				
			|||||||
import datetime
 | 
					import datetime
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from django.contrib.auth import get_user, login
 | 
				
			||||||
from django.contrib.auth.models import Group, User
 | 
					from django.contrib.auth.models import Group, User
 | 
				
			||||||
from django.urls import include, path, re_path
 | 
					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
 | 
					                "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)
 | 
					            return Response(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -120,7 +120,11 @@ export function handleReset(navigate: any, values: { email: string }) {
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * Check login state, and redirect the user as required
 | 
					 * 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
 | 
					  api
 | 
				
			||||||
    .get(apiUrl(ApiPaths.user_token), {
 | 
					    .get(apiUrl(ApiPaths.user_token), {
 | 
				
			||||||
      timeout: 2000,
 | 
					      timeout: 2000,
 | 
				
			||||||
@@ -144,6 +148,6 @@ export function checkLoginState(navigate: any, redirect?: string) {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    .catch(() => {
 | 
					    .catch(() => {
 | 
				
			||||||
      navigate('/login');
 | 
					      if (!no_redirect) navigate('/login');
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,12 +2,14 @@ import { t } from '@lingui/macro';
 | 
				
			|||||||
import { Center, Container } from '@mantine/core';
 | 
					import { Center, Container } from '@mantine/core';
 | 
				
			||||||
import { useToggle } from '@mantine/hooks';
 | 
					import { useToggle } from '@mantine/hooks';
 | 
				
			||||||
import { useEffect } from 'react';
 | 
					import { useEffect } from 'react';
 | 
				
			||||||
 | 
					import { useNavigate } from 'react-router-dom';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { setApiDefaults } from '../../App';
 | 
					import { setApiDefaults } from '../../App';
 | 
				
			||||||
import { AuthFormOptions } from '../../components/forms/AuthFormOptions';
 | 
					import { AuthFormOptions } from '../../components/forms/AuthFormOptions';
 | 
				
			||||||
import { AuthenticationForm } from '../../components/forms/AuthenticationForm';
 | 
					import { AuthenticationForm } from '../../components/forms/AuthenticationForm';
 | 
				
			||||||
import { InstanceOptions } from '../../components/forms/InstanceOptions';
 | 
					import { InstanceOptions } from '../../components/forms/InstanceOptions';
 | 
				
			||||||
import { defaultHostKey } from '../../defaults/defaultHostList';
 | 
					import { defaultHostKey } from '../../defaults/defaultHostList';
 | 
				
			||||||
 | 
					import { checkLoginState } from '../../functions/auth';
 | 
				
			||||||
import { useServerApiState } from '../../states/ApiState';
 | 
					import { useServerApiState } from '../../states/ApiState';
 | 
				
			||||||
import { useLocalState } from '../../states/LocalState';
 | 
					import { useLocalState } from '../../states/LocalState';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -24,6 +26,7 @@ export default function Login() {
 | 
				
			|||||||
  const hostname =
 | 
					  const hostname =
 | 
				
			||||||
    hostList[hostKey] === undefined ? t`No selection` : hostList[hostKey]?.name;
 | 
					    hostList[hostKey] === undefined ? t`No selection` : hostList[hostKey]?.name;
 | 
				
			||||||
  const [hostEdit, setHostEdit] = useToggle([false, true] as const);
 | 
					  const [hostEdit, setHostEdit] = useToggle([false, true] as const);
 | 
				
			||||||
 | 
					  const navigate = useNavigate();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Data manipulation functions
 | 
					  // Data manipulation functions
 | 
				
			||||||
  function ChangeHost(newHost: string): void {
 | 
					  function ChangeHost(newHost: string): void {
 | 
				
			||||||
@@ -37,6 +40,9 @@ export default function Login() {
 | 
				
			|||||||
    if (hostKey === '') {
 | 
					    if (hostKey === '') {
 | 
				
			||||||
      ChangeHost(defaultHostKey);
 | 
					      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
 | 
					  // Fetch server data on mount if no server data is present
 | 
				
			||||||
  useEffect(() => {
 | 
					  useEffect(() => {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user