-
Notifications
You must be signed in to change notification settings - Fork 235
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
ITelemetryContext.user is sometimes null - setAuthenticatedUserContext throws #1541
Comments
You should NOT EVER be replacing or assigning the The User class has actually ALWAYS implemented (and required) these functions Here is the version from 2018, it's just that now the types are correctly declaring them to properly support TS upgrades and previously the Looking at the code the user object is created at the point of construction and only if the runtime environment has a window. The correct way to set the authenticated user details is via the Summary
Don't!
It should not ever be AFTER successful initialization, if initialization fails it may not have been set.
Correct, and it wont be after initialization which creates it here, the only exception is the above for window-less environments. Several bugs have also been fixed so the correct context is now exposed (when initialized via the snippet).
The Initialization is not (generally) asynchronously, this may depend on your environment and how the NPM packages chunk the code and require() usage. The actual SDK (by default) will perform the initialization synchronously.
Yes, it should be... |
@MSNev I'm sorry, I found the problem and the issue was actually on my side. To my excuse - until recently, your TypeScript typings did not expose any other way to set the user context to my knowledge.
Since I encourage you to tighten up the typings a bit and add Thanks for your help! |
Agree, there is a bunch of things that are slowly being addressed including the previously missing
Will need to check this as it "should be" as it's defined here, it's probably got to do with the dynamic nature of the snippet as it can be 1 of 3 different implementations. I'll create am issue for the typings... |
Created #1546 |
@MSNev Great, thanks 👍😃 |
v2.6.3 is now fully deployed |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
-Package: @microsoft/applicationinsights-web
-Package version: 2.6.1
-Installed using npm, built with webpack
-Using TypeScript
Just upgraded @microsoft/applicationinsights-web from version 2.5.8 to version 2.6.1.
I needed to update our code when I found that
ITelemetryContext.user
(IUserContext
) has added two new methods;setAuthenticatedUserContext
andclearAuthenticatedUserContext
.Up till now we have just set
ai.context.user
to an object literal that implementsIUserContext
when a user logs in (and set it tonull
when the user logs out) which worked fine.Since the
IUserContext
interface now also requires these two new methods, I changed the code so that instead of setting/clearing theai.context.user
property, I rather call these two new methodsai.context.user.setAuthenticatedUserContext
and.clearAuthenticatedUserContext
when users log in/out.The problem I faced is that
ai.context.user
sometimes isnull
, and then an exception is thrown.I believe that
ai.context.user
is supposed to never be null even if the user has not yet logged in, because how are you then supposed to set the authenticated user context?It looks to me as if the
ai.context.user
is initialized asynchronously a bit too late.It would expect this to initialized to something other than
null
immediately afterai.loadAppInsights()
has been called.The text was updated successfully, but these errors were encountered: