# LogoutManager

Logs out based on fetch responses. By default this is triggered by [401 (Unauthorized)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401) status responses.

> **Info: implements**
>
> `LogoutManager` implements [Manager](https://dataclient.io/vue/api/Manager.md)

## Usage

```ts title="main.ts"
import { createApp } from 'vue';
import {
  DataClientPlugin,
  LogoutManager,
  getDefaultManagers,
} from '@data-client/vue';
import App from './App.vue';

const managers = [new LogoutManager(), ...getDefaultManagers()];

const app = createApp(App);
app.use(DataClientPlugin, { managers });
app.mount('#app');
```

### Custom logout handler

```ts
import { unAuth } from '../authentication';

const managers = [
  new LogoutManager({
    handleLogout(controller) {
      // call custom unAuth function we defined
      unAuth();
      // still reset the store
      controller.resetEntireStore();
    },
  }),
  ...getDefaultManagers(),
];
```

> **Tip**
>
> Use [controller.invalidateAll](https://dataclient.io/vue/api/Controller.md#invalidateAll) to only clear part of the cache.
>
> ```ts
> import { unAuth } from '../authentication';
>
> const myDomain = 'http://test.com';
> const testKey = (key: string) => key.startsWith(`GET ${myDomain}`);
>
> const managers = [
>   new LogoutManager({
>     handleLogout(controller) {
>       // call custom unAuth function we defined
>       unAuth();
>       // still reset the store
>       controller.invalidateAll({ testKey });
>     },
>   }),
>   ...getDefaultManagers(),
> ];
> ```

## Members

### handleLogout(controller)

By default simply calls [controller.resetEntireStore()](https://dataclient.io/vue/api/Controller.md#resetEntireStore)

This should be sufficient if login state is determined by a user entity existence in the Reactive Data Client store. However,
you can override this method via inheritance if more should be done.

### shouldLogout(error)

```ts
protected shouldLogout(error: UnknownError) {
  // 401 indicates reauthorization is needed
  return error.status === 401;
}
```
