# @vonrehberg.consulting/mgt-element

> Microsoft Graph Toolkit base classes

Latest version **2.10.0** (published 2022-12-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @vonrehberg.consulting/mgt-element
pnpm add @vonrehberg.consulting/mgt-element
yarn add @vonrehberg.consulting/mgt-element
bun add @vonrehberg.consulting/mgt-element
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.10.0 |
| Published | 2022-12-29 |
| First published | 2022-12-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 488.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1068 |
| Author | Microsoft |
| Maintainers | vonrehberg.consulting |

## Links

- npm: https://www.npmjs.com/package/@vonrehberg.consulting/mgt-element
- Repository: https://github.com/microsoftgraph/microsoft-graph-toolkit
- Issues: https://github.com/microsoftgraph/microsoft-graph-toolkit/issues
- npm.io page: https://npm.io/package/@vonrehberg.consulting/mgt-element

## Dependencies (3)

- [idb](https://npm.io/package/idb.md) 6.0.0
- [lit-element](https://npm.io/package/lit-element.md) ^2.4.0
- [@microsoft/microsoft-graph-client](https://npm.io/package/@microsoft/microsoft-graph-client.md) ^2.2.1

## Recent versions

- 2.10.0 (latest) — 2022-12-29
- 2.9.3 — 2022-12-28
- 2.9.2 — 2022-12-28
- 2.9.1 — 2022-12-28
- 2.0.0 — 2022-12-28

## README

# Microsoft Graph Toolkit Base package

[![npm](https://img.shields.io/npm/v/@vonrehberg.consulting/mgt-element?style=for-the-badge)](https://www.npmjs.com/package/@vonrehberg.consulting/mgt-element)

The [Microsoft Graph Toolkit (mgt)](https://aka.ms/mgt) library is a collection of authentication providers and UI components powered by Microsoft Graph. 

The `@vonrehberg.consulting/mgt-element` package contains all base classes that enable the providers and components to work together. Use this package to set the global provider, or to create your own providers and/or components that work with Microsoft Graph.

[See docs for full documentation](https://aka.ms/mgt-docs)

## Set and use the global provider

The `@vonrehberg.consulting/mgt-element` package exposes the `Providers` namespace that enables global usage of the authentication providers across your entire app. 

This example illustrates how to instantiate a new provider (MsalProvider in this case) and use it across your app:

1. Install the packages 

    ```bash
    npm install @vonrehberg.consulting/mgt-element @vonrehberg.consulting/mgt-msal-provider
    ```

1. Create the provider

    ```ts
    import {Providers} from '@vonrehberg.consulting/mgt-element';
    import {MsalProvider} from '@vonrehberg.consulting/mgt-msal-provider';

    // initialize the auth provider globally
    Providers.globalProvider = new MsalProvider({clientId: 'clientId'});
    ```

1. Use the provider to sign in and call the graph:

    ```ts
    import {Providers, ProviderState} from '@vonrehberg.consulting/mgt-element';

    const handleLoginClicked = async () => {
      await Providers.globalProvider.login();

      if (Providers.globalProvider.state === ProviderState.SignedIn) {
        let me = await Provider.globalProvider.graph.client.api('/me').get();
      }
    }
    ```

You can learn more about how to use the providers in the [documentation](https://learn.microsoft.com/graph/toolkit/providers).

The providers work well with the `@vonrehberg.consulting/mgt-components` package and all components use the provider automatically when they need to call Microsoft Graph.

## Create your own provider

In scenarios where you want to use the Providers namespace and/or add Microsoft Graph Toolkit components to an application with pre-existing authentication code, you can create a custom provider that hooks into your authentication mechanism. `@vonrehberg.consulting/mgt-element` enables two ways to create new providers:

### Create a Simple Provider

If you already have a function that returns `accessTokens`, you can use a SimpleProvider to wrap the function:

```ts
import {Providers, SimpleProvider} from '@vonrehberg.consulting/mgt-element';

function getAccessToken(scopes: string[]) {
  // return a promise with accessToken string
}

function login() {
  // login code - optional

  // make sure to set the state when signed in
  Providers.globalProvider.setState(ProviderState.SignedIn)
}

function logout() {
  // logout code - optional

  // make sure to set the state when signed out
  Providers.globalProvider.setState(ProviderState.SignedOut)
}

Provider.globalProvider = new SimpleProvider(getAccessToken, login, logout);
```

### Extend an IProvider

You can extend the IProvider abstract class to create your own provider. The IProvider is similar to the SimpleProvider in that it requires the developer to implement the `getAccessToken()` function.


See the [custom provider documentation](https://learn.microsoft.com/graph/toolkit/providers/custom) for more details on both ways to create custom providers.

## Sea also
* [Microsoft Graph Toolkit docs](https://aka.ms/mgt-docs)
* [Microsoft Graph Toolkit repository](https://aka.ms/mgt)
* [Microsoft Graph Toolkit playground](https://mgt.dev)

---
_Source: https://npm.io/package/@vonrehberg.consulting/mgt-element · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
