Skip to content

user logout -dependency not registering. #392

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

Closed
thanoofayoob opened this issue Nov 17, 2024 · 34 comments
Closed

user logout -dependency not registering. #392

thanoofayoob opened this issue Nov 17, 2024 · 34 comments

Comments

@thanoofayoob
Copy link

Issue Summary:
I am encountering an issue with the GetIt package in my Flutter application related to the lifecycle of a GraphQLClient instance. Specifically, when a user logs out, I attempt to unregister or isolate the GraphQLClient to avoid carrying over the previous user's data. However, even after introducing scoped dependencies and unregistering the old client, the new user still interacts with the previous user's GraphQL client instance or its remnants, leading to unexpected behavior such as accessing stale data or using outdated headers.

Steps to Reproduce:

A user logs in, and the AppGraphQLClient is registered using:

injector.registerSingleton(() => AppGraphQLClient());
The client includes headers with the user's token for authentication.

On user logout, I attempted two approaches:

Directly unregistering the GraphQLClient instance:

injector.unregister();
Introducing scoped dependencies using:

injector.pushNewScope(
scopeName: "NewUser",
init: (getIt) {
injector.registerSingleton(() => AppGraphQLClient());
},
);
The scope is dropped on logout using:

injector.popScope();
A new user logs in, triggering a re-registration of the AppGraphQLClient for the new session.

Despite these efforts, the new user still interacts with the old client's instance or configuration, including outdated headers like the previous user's token.

Expected Behavior:
After a user logs out:

The previous AppGraphQLClient instance should be completely destroyed, along with any cached data or retained headers.
On new user login, a completely fresh AppGraphQLClient instance should be initialized with the new user's token and other required configurations.
Observed Behavior:
Even after unregistering the previous client or using scoped dependencies with pushNewScope, the new user still interacts with the previous user's instance or data. This leads to stale headers or incorrect token usage.

Additional Notes:

I do not reset the entire GetIt container on logout, as I want to preserve other dependencies

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

Nop . There was only one pop was pushed after successful login from the user and poped when user logout.

I will try with popscopeuntil.?

in one of the issue you answered like instead of lazy singleton use normal one. when i tried that it was throwing error saying it is AppGraphqlClient is not regiseterd with get it.

Is there something else i can use to solve the issue.?

@thanoofayoob
Copy link
Author

Bad state: Tried to read a provider that threw during the creation of its value.
The exception occurred during the creation of type DashBoardCubit.

══╡ EXCEPTION CAUGHT BY PROVIDER ╞═══════════════════════════════
The following StateError was thrown:
Bad state: GetIt: Object/factory with type AppGraphQLClient is
not registered inside GetIt.
(Did you accidentally do GetIt sl=GetIt.instance(); instead of
GetIt sl=GetIt.instance;
Did you forget to register it?)

This was the issue when i tried with injector.pushNewScope(scopeName: "NewUser",init: (getIt) {
injector.registerSingleton(() => AppGraphQLClient());
},);

@thanoofayoob
Copy link
Author

tried with popscopuntil still same issue.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

What i tried initially was resetting all dependencies using injector.reset() that function was resetting all dependencies . but the issue was MyAuthController bloc was also registered in get it ,using the stream controller in that bloc i was managing the auth state of my APP. when i reset() ,it stopped emitting stream.

so what the best way to handle logout and when a new user logs in how to clear the previous user data.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

do you have a example repo which handle logout . ?

creating a new repo to demo my issue will take more time. and im running out of time . if it is must i will tries to create one.

@thanoofayoob
Copy link
Author

is this is the right way ,like using the scope to handle user sessions and logout.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

ho sorry. its my company repo . i don't have admin privilege to it.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

thanoofayoob commented Nov 17, 2024

#108

i have tried this answer of yours . you are also mentioning this right.?

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

no i'm not storing token locally. and i haven't killed the app before the next user login. if the next user is using after killing the app session there is no issue.!

and as i have already mentioned it was working when i reset the injector on logout. but by doing that my bloc was unable to listen stream controller since it was disposed.

@thanoofayoob
Copy link
Author

yeah inspected it shows
AppLogger().debug("${injector.isRegistered()}");

    // injector.unregister<AppGraphQLClient>();

    // injector.pushNewScope(scopeName: "NewUser");
    await injector.popScope();

    AppLogger().info("logout");

    AppLogger().debug("${injector.isRegistered<AppGraphQLClient>()}");

after logout it is returning false.

@thanoofayoob
Copy link
Author

and i have another qn whats the issue with injector.reset()? , even after it is initialising the dependencies why i was unable to listen to streamcontroller changes.?

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

Okay. the thing is that im unable to find any other issues.

i have one more qn is there any workaround with injector.reset().

and after that registering all the dependencies again.?

@thanoofayoob
Copy link
Author

#112

this is almost same issue im facing ,freewebwithme last comment.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

every other dependencies worked. but as i said my authrepo contain's a streamcontroller which manages authstate of my app.
which will be disposed on reset. after that my streamcontroller is not working.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

okay. can you please what happens when injector.reset() happens?.

and if you have example repo or github issue to describe manage user logout ,share that also.

and thanks a lot for your time and replies.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

as i said i have cross checked all the block part it is working fine.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

one more doubt resetting all dependencies and initialising it again , is there any problem with that.?

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 17, 2024 via email

@thanoofayoob
Copy link
Author

i solved this issue by implementing interceptors on graphql client. Thanks for the replies and your timw @escamoteur .

Got a lot of ideas how get_it
works.

@escamoteur
Copy link
Collaborator

escamoteur commented Nov 18, 2024 via email

@thanoofayoob
Copy link
Author

thanoofayoob commented Nov 18, 2024

flutter graphql client had no documentation about adding interceptors. that why when i get new a token i had to reinitialise the client again , that why i had to reset to unregister the get_it dependency.

yesterday i found a way to add interceptor to my client , So when a new token arrives it will be handled by my interceptors.

@thanoofayoob
Copy link
Author

class AuthLink extends Link {
@OverRide
Stream request(Request request, [NextLink? forward]) async* {

// Clone the request to add the authorization header
final req = request.updateContextEntry<HttpLinkHeaders>(
  (headers) => HttpLinkHeaders(
    headers: {
      ...headers?.headers ?? {},
      'Authorization': 'Bearer $token'        },
  ),
);

// Forward the modified request
if (forward != null) {
  yield* forward(req);

    }
  }
}

late GraphQLClient _graphQLClient;
final loggingLink = LoggingLink();
final authLink = AuthLink();

// final link = Link.from([errorLink, httpLink, logLiNK, authLink]);
final link =
    errorLink.concat(authLink).concat(loggingLink).concat(httpLink);

_graphQLClient = GraphQLClient(
    cache: GraphQLCache(),
    alwaysRebroadcast: true,
    link: link,
    queryRequestTimeout: GflConstants.apiTimeout,
    defaultPolicies: DefaultPolicies(
      query: Policies(fetch: FetchPolicy.networkOnly),

        ));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants