Skip to content

Commit

Permalink
Logging out means clearing GraphQL cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zaychenko-sergei committed Aug 29, 2023
1 parent 9b2dfe6 commit dbbefb4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/app/auth/logged-user-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ import { AppConfigService } from "../app-config.service";
import { GithubLoginCredentials, PasswordLoginCredentials } from "../api/auth.api.model";

describe("LoggedUserService", () => {
let controller: ApolloTestingController;
let service: LoggedUserService;
let controller: ApolloTestingController;

describe("Main Test Suite", () => {
let navigationService: NavigationService;
let authApi: AuthApi;
let apollo: Apollo;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [AuthApi, Apollo],
imports: [ApolloTestingModule],
});
service = TestBed.inject(LoggedUserService);
apollo = TestBed.inject(Apollo);
navigationService = TestBed.inject(NavigationService);
authApi = TestBed.inject(AuthApi);
controller = TestBed.inject(ApolloTestingController);
Expand Down Expand Up @@ -160,8 +162,9 @@ describe("LoggedUserService", () => {
flush();
}));

it("should check user logout navigates to home page", fakeAsync(() => {
it("should check user logout navigates to home page and clears GraphQL cache", fakeAsync(() => {
const navigationServiceSpy = spyOn(navigationService, "navigateToHome");
const apolloClientSpy = spyOn(apollo.client, "clearStore").and.resolveTo();

attemptSuccessfulLoginViaAccessToken();
tick();
Expand All @@ -170,6 +173,7 @@ describe("LoggedUserService", () => {

expect(service.currentlyLoggedInUser).toBeNull();
expect(navigationServiceSpy).toHaveBeenCalledWith();
expect(apolloClientSpy).toHaveBeenCalledTimes(1);
flush();
}));

Expand Down
10 changes: 9 additions & 1 deletion src/app/auth/logged-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { AuthApi } from "../api/auth.api";
import { AccountDetailsFragment } from "../api/kamu.graphql.interface";
import { UnsubscribeOnDestroyAdapter } from "../common/unsubscribe.ondestroy.adapter";
import { AppConfigLoginInstructions } from "../app-config.model";
import { Apollo } from "apollo-angular";
import { promiseWithCatch } from "../common/app.helpers";

@Injectable({
providedIn: "root",
Expand All @@ -25,6 +27,7 @@ export class LoggedUserService extends UnsubscribeOnDestroyAdapter {
private authApi: AuthApi,
private navigationService: NavigationService,
private appConfigService: AppConfigService,
private apollo: Apollo,
) {
super();

Expand Down Expand Up @@ -63,9 +66,10 @@ export class LoggedUserService extends UnsubscribeOnDestroyAdapter {
this.navigationService.navigateToHome();
}

public terminateSession() {
public terminateSession(): void {
this.changeUser(null);
this.resetAccessToken();
this.clearGraphQLCache();
}

private attemptPreviousAuthentication(): Observable<void> {
Expand All @@ -82,6 +86,10 @@ export class LoggedUserService extends UnsubscribeOnDestroyAdapter {
this.loggedInUserChanges$.next(user);
}

private clearGraphQLCache(): void {
promiseWithCatch(this.apollo.client.clearStore());
}

private resetAccessToken(): void {
localStorage.removeItem(AppValues.LOCAL_STORAGE_ACCESS_TOKEN);
}
Expand Down

0 comments on commit dbbefb4

Please sign in to comment.