# The identity context

Upon page initial load, `frontend-workstation` fetches user data before loading any modules.
That includes feature flags, feature controls and vendor enabled features.
As soon as `frontend-workation` is initialized and has this info, it creates the `identity` context which gets passed down to child components.
That context includes functions which will allow checking if the user has the proper permissions without the need for any api calls.

## `identity.hasPermission`

Check user roles and privileges.

```ts
interface Permission {
    permission: string,
    privilege?: string
}

hasPermission( permission: Permission ) : boolean
hasPermission( permissions: Permission[] ) : boolean
```

## `identity.hasFeatureControl`

Check vendor restricted features.

```ts
hasFeatureControl( feature: string ) : boolean
hasFeatureControl( features: string[] ) : boolean
```

## `identity.hasFeatureFlag`

Check user feature flags.

```ts
hasFeatureFlag( flag: string, variant = 'enabled' ) : boolean
```

### Example usage

### Class and prop-types

```jsx
import React from 'react';
import PropTypes from 'prop-types';

import NewFeatureComponent from './new';
import CurrentFeatureComponent from './current';

export default class FeatureComponent extends React.Component {
    static contextTypes = {
        identity: PropTypes.object,
    };

    render() {
        const { identity } = this.context;

        if (identity.hasFeatureFlag('some_new_feature'))
            return <NewFeatureComponent />;

        return <CurrentFeatureComponent />;
    }
}
```

### Hooks

```jsx
import React from 'react';
import PropTypes from 'prop-types';
import { IdentityContext } from '@orchard/frontend-workstation';

import NewFeatureComponent from './new';
import CurrentFeatureComponent from './current';

const FeatureComponent = () => {
    const { identity } = React.useContext(IdentityContext);

    if (identity.hasFeatureFlag('some_new_feature'))
        return <NewFeatureComponent />;

    return <CurrentFeatureComponent />;
};
```

### Class definition

```tsx
class Identity {
    accountId: string;
    accountName: string;
    accountType: string;
    id: number;
    language: string;
    numberFormat: string;
    vendorId: number;
    subaccountId?: number;

    isAnonymous(): boolean;
    hasFeatureFlag(featureFlag: string, variant: FeatureFlagVariant): boolean;
    hasFeatureFlag(featureFlag: string): boolean;
    hasFeatureControl(features: string | string[]): boolean;
    hasPermission(perms: Permission | Permission[]): boolean;
}
```
