Solved

Code to test if the user is logged in


Badge

Hi,

I am developing a customization and got the “Error: You are not currently logged in” exception. I understand the problem, question is, how do I test if the user is logged in or not before I execute my code?

icon

Best answer by rosenjon 26 April 2022, 17:05

View original

17 replies

Userlevel 3
Badge +2

Hi @adriaan17,

 

Could you provide some example as to what it is that you are trying to achieve?

This would help us understand how to better help you.

Badge

Hi @SLavigueur ,

Apologies for the delayed response. I have exception logging routines that includes the user’s current company detail. I use the code instead of CompanyID to accommodate for queries across all logs. It may happen that my logger catches an exception before a user has logged in, or after they have logged out, in which case I would like to log a blank code

Userlevel 3
Badge +2

it depends on the way that you are using in order to connect.

If you are using the classic login request, then you can check to see if you have the proper cookies that would indicate that a session is open. though if the session has expired then you would see the cookies but there would be no valid session and you would still get the error.

If you are using OAuth, the checking that you have a token should be a first, but here as well if the token has expired you would get the error as well. The only way to know in advance here would be to have saved the time where the token was created and compare it to the current time since the token are valid for a period of 1 hour.

In both case if the session or the token have expired, making a request and receiving the :

401 Unauthorized

{
    "message": "You are not logged in."
}

is the only way to confirm that you are not currently logged in.

Badge

Is there no status in a .Net object I can test from within a graph?

I looked at the below objects but cannot see anything:

PXAccess

CommonServiceLocator.ServiceLocator.Current.GetInstance<ICurrentUserInformationProvider>()

PXInstanceHelper

Userlevel 3
Badge +2

Oh,

I think I misunderstood your scenario.

Since most of the time the you are not logged in error comes from Web Services user I had assumed that you were also in that context.

But you are talking about the “You are currently logged off. Would you like to log in?” exception that you are getting in a pop up in the UI. Am I correct?

If that is the case you might want to do a try catch with the following:

catch (PXNotLoggedInException)

This might allow you to add some custom logic in that scenario.

Badge

Thanks for all the help.

I am getting the error when I attempt to read the PXInstanceHelper.CurrentCompany property.

A search of that property in the open-source code has not helped me to avoid the exception, and I’d prefer to not just wrap it in an exception handler, if avoidable.

Userlevel 3
Badge +2

That might be because since you are not logged in, you are not considered inside of any company.

Badge

That is correct, yes, and I’m looking for a way to detect if I am in a company or not before I make the call and get the exception

Userlevel 3
Badge +2

Can you provide the code sample of what you are doing?

Badge

It’s hard to give a code sample that will reproduce my scenario. It is in my exception logging procedure that can be called from many places in our code. The below code will produce the same exception, but it’s not quite what my code does.

This code will clearly break an environment as it will error out on all graphs before the user logs in, but it illustrates the issue. 

 

    public class PXBaseGraph_Extension : PXGraphExtension<PXGraph>
    {
        public static bool IsActive() { return true; }
        
        public override void Initialize()
        {
                //TODO: check if logged in before calling this
                var id = PXInstanceHelper.CurrentCompany;        
        }
    }

 

 

Userlevel 6
Badge +5

There are some other things I can think of that are fancier, but why not just use try/catch on setting the id property, and then do something else in the catch block if it fails?

Userlevel 6
Badge +5

Another comment here though…. if you are putting this in a logging routine, and it has the potential to break your entire site, you are probably doing something wrong. Why not have the user session send the current company to the logs during whatever activity you’re trying to log, and then you don’t have to query it from the logs?

 

Logs are about recording events into a file. The logging routines themselves really shouldn’t be performing queries. You may cause performance issues across the entire site by doing what you’re doing, and it isn’t clear at all that it’s necessary.

Badge

Thanks for the suggestion rosenjon.

Technically that would take a lot more processing power than to check a bit flag for a given state. 

I am doing that currently, but would prefer a more elegant solution.

I would love to hear the other things that you can think of 😀

Userlevel 6
Badge +5

@adriaan17 I think it would be helpful if you could describe in detail what you’re trying to do.

I remember  I worked on a WMS system (homegrown), where the original developer had decided that he needed to log the state of many objects from the barcode scanner C# code in real-time over the wire (Windows Mobile 6...). These objects would all end up in the database as pseudo Gobbledygook. Even if you wanted to reproduce what had happened on the handheld at the time of the error, it was impossible 99% of the time. What this code did do was clog up the wireless network and make the handheld code very slow and unresponsive in many cases.

The answer, of course, was to stop doing that. We setup interactive debugging with a handheld emulator, or when necessary to the device itself, and that allowed us to debug 99.9% of the situations that he was trying to debug remotely via the logs.

Badge

My logging will certainly not break the site, that was a comment about the sample code provided.

I do not do queries in my logging routine, I only extract the company id to include in the logging data. I don’t query the logs at any time during the logging procedure.

My logging is limited to only my routines and will have no impact on the rest of the site.

Data that is included in the logs is controlled by verbosity and level settings, so I make sure I do not log any data that is not wanted.

Userlevel 6
Badge +5

if(PXInstanceHelper != null && PXInstanceHelper.CurrentCompany != null)
{
//...log stuff here
}

I think this is pretty lightweight and should work….absent more context on what you’re trying to achieve. Why not check if the property is null?

Badge

if(PXInstanceHelper != null && PXInstanceHelper.CurrentCompany != null)
{
//...log stuff here
}

I think this is pretty lightweight and should work….absent more context on what you’re trying to achieve. Why not check if the property is null?

Thanks, @rosenjon, that is a good suggestion.

I would really like an event to listen to on any PXInstanceHelper.CurrentCompany changes (or an event downstream from there), but that does not seem to be available.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved